From adde7fd6c8b109dd78ae517bf46e72954a86de75 Mon Sep 17 00:00:00 2001 From: Naukri Date: Tue, 14 Nov 2023 02:06:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=AA=94=20:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E8=87=B3=202.3.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com.naukri.inspector-maid/CHANGELOG.md | 12 +++ .../Documentation/Introduction.md | 96 +++++++++++-------- .../com.naukri.inspector-maid/package.json | 2 +- 3 files changed, 70 insertions(+), 40 deletions(-) diff --git a/Packages/com.naukri.inspector-maid/CHANGELOG.md b/Packages/com.naukri.inspector-maid/CHANGELOG.md index eaabe23..6099287 100644 --- a/Packages/com.naukri.inspector-maid/CHANGELOG.md +++ b/Packages/com.naukri.inspector-maid/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## [2.3.0] - 2023-11-14 + +### 新增 + +- `IfScpoe` 系列小部件新增預設比較方法,減少產生不必要的判斷函式。 + +### 重構 + +- 簡化與快速反射相關的底層 API。 +- 取消綁定約定以減少設計限制。 +- 部分 `Widget` 使用 `Compose` 模組重寫,使其更具結構性。 + ## [2.2.0] - 2023-11-12 ### 新增 diff --git a/Packages/com.naukri.inspector-maid/Documentation/Introduction.md b/Packages/com.naukri.inspector-maid/Documentation/Introduction.md index 1cbefe0..bfc82f7 100644 --- a/Packages/com.naukri.inspector-maid/Documentation/Introduction.md +++ b/Packages/com.naukri.inspector-maid/Documentation/Introduction.md @@ -292,8 +292,6 @@ public void MyMethod() public string message = "Hello World!"; ``` -- 如果要綁定物件本身請使用預先定義的關鍵字 `"this"` - - 依據綁定的成員類型不同,會有不同的回傳邏輯 1. 欄位:該欄位的數值。 @@ -301,20 +299,13 @@ public void MyMethod() 3. 函式:該函式調用後的回傳值,如果該函式有參數則需使用 `args` 定義參數。 ```cs - // 如果只有一個參數,可以利用 params 關鍵字的特性,省略 new object[] { ... } - [HelpBox(binding: nameof(HelloMessage), args: "world")] - // 如果有多個參數,則必須使用 new object[] { ... } 來包裹 - [HelpBox(binding: nameof(HelloTwoMessage), args: new object[] { "world", "you" })] + // 如果目標是帶參數函式,使用 new object[] { ... } 來包裹參數 + [HelpBox(binding: nameof(HelloTwoMessage), args: new object[] { "Hello", "world" })] public string message = ""; - - public string HelloMessage(string message) - { - return $"Hello {message}!"; - } - - public string HelloTwoMessage(string message1, string message2) + + public string CombineMessage(string message1, string message2) { - return $"Hello {message1} and {message2}!"; + return $"{message1} {message2}!"; } ``` @@ -377,31 +368,6 @@ public void MyMethod() ``` ![optional-variable-compare](Images/optional-variable-compare.png) - -4. 如果想要支援資料綁定,你需要讓該 `WidgetAttribute` 繼承 `IBindable` 介面。為了統一綁定邏輯,我們約定永遠使 `binding` 及 `args` 作為最後兩個參數且 `args` 需使用 `params` 關鍵字。 - - ```cs - public class MyItemAttribute : ItemAttribute, IBindable - { - public MyItemAttribute( - string myString, - string binding = null, - params object[] args - ) - { - this.myString = myString; - this.binding = binding; - this.args = args; - } - - public readonly string myString; - - public string binding { get; } - - public object[] args { get; } - } - ``` - ### 建立 `Widget` 根據 `WidgetAttribute` 的不同,我們需要繼承的類別也有所不同 @@ -486,6 +452,58 @@ public class MyStylerWidget : StylerWidgetOf #### 內置 `Receiver` - `IContextAttachedReceiver`:當該 `Widget` 的 `Context` 被附加到 `Context Tree` 時。 + +### 資料綁定 + +如果想要支援資料綁定,你可以在 `WidgetAttribute` 上實作 `IBindable` 介面使 `Widget` 可以使用包裝好的綁定函式,或者你也可以在 `Widget` 上直接使用 `GetValue()` 等函式來存取目標成員。 + +`MyItemAttribute.cs` +```cs +public class MyItemAttribute : ItemAttribute, IBindable +{ + public MyItemAttribute( + string myString, + string binding = null, + params object[] args + ) + { + this.myString = myString; + this.binding = binding; + this.args = args; + } + + public readonly string myString; + + public string binding { get; } + + public object[] args { get; } +} +``` + +`MyItemWidget.cs` +```cs + public class MyItemWidget : ItemWidgetOf + { + public override VisualElement Build(IBuildContext context) + { + // 獲取綁定成員的值 + var bindingValue = context.GetBindingValue(); + // 監聽綁定成員的值 + context.ListenBindingValue(value => + { + // Do something on value changed + }); + + // 你也可以直接指定成員名稱來存取 + var a = context.GetValue("memberName"); + context.ListenValue("memberName" ,value => + { + // Do something + }); + } + } +``` + ## 内置小部件 你可以在 package 中的 Sample 中找到所有内置小部件的 demo 以及詳細的說明。 diff --git a/Packages/com.naukri.inspector-maid/package.json b/Packages/com.naukri.inspector-maid/package.json index 689b8ff..edb757d 100644 --- a/Packages/com.naukri.inspector-maid/package.json +++ b/Packages/com.naukri.inspector-maid/package.json @@ -2,7 +2,7 @@ "name": "com.naukri.inspector-maid", "author": "Naukri", "displayName": "Inspector Maid", - "version": "2.2.0", + "version": "2.3.0", "unity": "2022.3", "description": "This is a Unity Inspector UI customization feature.\r\n You can use it to create some simple interactive functionalities for your Component without the need to write a dedicated Editor. It has several key features:\r\n\r\n\u2022Uses UIElements for rendering.\r\n\r\n\u2022Allows direct access to properties and functions in the Inspector.\r\n\r\n\u2022Supports adding multiple widgets to the same target.\r\n\r\n\u2022Allows customization of custom widgets.\r\n\r\n\u2022Allows dynamic content modification through binding.", "samples": [