diff --git a/Assets/Examples/Scripts/BindTest_BindFields.cs b/Assets/Examples/Scripts/BindTest_BindFields.cs index 45035d1..186042e 100644 --- a/Assets/Examples/Scripts/BindTest_BindFields.cs +++ b/Assets/Examples/Scripts/BindTest_BindFields.cs @@ -6,11 +6,13 @@ public partial class BindTest : MonoBehaviour { public UnityEngine.UIElements.Label Title { get; private set; } public UnityEngine.UIElements.Button Button1 { get; private set; } + public UnityEngine.UIElements.TextField TextField { get; private set; } public void Bind() { VisualElement root = gameObject.GetComponent().rootVisualElement; Title = root.Q("Title"); Button1 = root.Q("Button1"); + TextField = root.Q("TextField"); } } diff --git a/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBinding.cs b/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBinding.cs index 65d369d..f7a872b 100644 --- a/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBinding.cs +++ b/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBinding.cs @@ -14,7 +14,7 @@ namespace com.shaunfong.UIToolkitFieldBinding.editor public class FieldBinding { - public int ID { get; private set; } + public string VisualTreeAssetPath { get; private set; } public ElementInfoVisibilityState m_ElementInfoVisibillityState = ElementInfoVisibilityState.All ^ ElementInfoVisibilityState.HideWithoutName ^ ElementInfoVisibilityState.HideSelected ^ ElementInfoVisibilityState.HideNotSelected; private List m_LoadedFieldData = new List(); public List DisplayFields { get; private set; } = new List(); @@ -25,9 +25,8 @@ public class FieldBinding private VisualTreeAsset m_TargetVisualTreeAsset; private FieldBindingDataManager m_FieldBindingDataManager; - public FieldBinding(int id, VisualElement root) + public FieldBinding(VisualElement root) { - ID = id; m_FieldBindingVisualElement = root; //Load VisibilityState Setting @@ -44,6 +43,7 @@ public FieldBinding(int id, VisualElement root) internal void LoadFieldsData(VisualTreeAsset targetVisualTreeAsset) { + VisualTreeAssetPath = AssetDatabase.GetAssetPath(targetVisualTreeAsset); m_TargetVisualTreeAsset = targetVisualTreeAsset; if (targetVisualTreeAsset == null) @@ -87,7 +87,7 @@ internal void RefreshFieldsList(bool force = false) } DisplayFields.Clear(); - FieldBindingData storageBindingData = m_FieldBindingDataManager.GetFieldBindingData(ID); + FieldBindingData storageBindingData = m_FieldBindingDataManager.GetFieldBindingData(VisualTreeAssetPath); for (int i = 0; i < m_LoadedFieldData.Count; i++) { if (string.IsNullOrEmpty(m_SearchString) == false && m_LoadedFieldData[i].FieldName.Contains(m_SearchString) == false && m_LoadedFieldData[i].FieldDisplayType.Contains(m_SearchString) == false) diff --git a/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBindingData.cs b/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBindingData.cs index 1de0faa..e5c4d82 100644 --- a/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBindingData.cs +++ b/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBindingData.cs @@ -8,13 +8,13 @@ namespace com.shaunfong.UIToolkitFieldBinding.editor [System.Serializable] public class FieldBindingData { - public int Id; + public string AssetPath; public string ScriptPath; public List FieldDatas = new List(); - public FieldBindingData(int id) + public FieldBindingData(string assetPath) { - Id = id; + AssetPath = assetPath; } } } \ No newline at end of file diff --git a/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBindingDataManager.cs b/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBindingDataManager.cs index db26525..61a463a 100644 --- a/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBindingDataManager.cs +++ b/Packages/com.shaunfong.uitoolkit-binding/Editor/FieldBinding/FieldBindingDataManager.cs @@ -14,24 +14,24 @@ public class FieldBindingDataManager : ScriptableObject [SerializeField] public List Data = new List(); - public bool Exist(int id) + public bool Exist(string AssetPath) { - return Data.FindAll((x) => x.Id == id).Count != 0; + return Data.FindAll((x) => x.AssetPath == AssetPath).Count != 0; } public void Add(FieldBindingData data) { - if (Exist(data.Id) == false) + if (Exist(data.AssetPath) == false) { Data.Add(data); } } - public bool Remove(int id) + public bool Remove(string AssetPath) { for (int i = 0; i < Data.Count; i++) { - if (Data[i].Id == id) + if (Data[i].AssetPath == AssetPath) { Data.RemoveAt(i); return true; @@ -40,15 +40,15 @@ public bool Remove(int id) return false; } - public FieldBindingData GetFieldBindingData(int id) + internal FieldBindingData GetFieldBindingData(string visualTreeAssetPath) { - return Data.Find((x) => x.Id == id); ; + return Data.Find((x) => x.AssetPath == visualTreeAssetPath); } public static FieldBindingDataManager GetInstance() { - if(ScriptableObjectUtility.CheckExist() == false) + if (ScriptableObjectUtility.CheckExist() == false) { ScriptableObjectUtility.CreateNewOne(); } @@ -59,14 +59,14 @@ public static FieldBindingDataManager GetInstance() internal void Save(FieldBinding fieldBinding) { - if (Exist(fieldBinding.ID) == false) + if (Exist(fieldBinding.VisualTreeAssetPath) == false) { SaveAs(fieldBinding); return; } // Save Field Data To Disk. - FieldBindingData data = GetFieldBindingData(fieldBinding.ID); + FieldBindingData data = GetFieldBindingData(fieldBinding.VisualTreeAssetPath); data.FieldDatas.Clear(); foreach (var field in fieldBinding.DisplayFields) { @@ -82,9 +82,9 @@ internal void Save(FieldBinding fieldBinding) internal void SaveAs(FieldBinding fieldBinding) { //Remove Exist Data. - Remove(fieldBinding.ID); + Remove(fieldBinding.VisualTreeAssetPath); - FieldBindingData data = new FieldBindingData(fieldBinding.ID); + FieldBindingData data = new FieldBindingData(fieldBinding.VisualTreeAssetPath); //Save Field Data To Disk. foreach (var field in fieldBinding.DisplayFields) @@ -104,9 +104,9 @@ internal void SaveAs(FieldBinding fieldBinding) internal void Generate(FieldBinding fieldBinding) { - FieldBindingData data = GetFieldBindingData(fieldBinding.ID); + FieldBindingData data = GetFieldBindingData(fieldBinding.VisualTreeAssetPath); - if (data == null) + if (data == null || File.Exists(data.ScriptPath.GetGlobalPath()) == false) { SaveAs(fieldBinding); return; diff --git a/Packages/com.shaunfong.uitoolkit-binding/Editor/Inspector/UIDocumentInspector.cs b/Packages/com.shaunfong.uitoolkit-binding/Editor/Inspector/UIDocumentInspector.cs index 09cd071..dd06bb4 100644 --- a/Packages/com.shaunfong.uitoolkit-binding/Editor/Inspector/UIDocumentInspector.cs +++ b/Packages/com.shaunfong.uitoolkit-binding/Editor/Inspector/UIDocumentInspector.cs @@ -37,8 +37,8 @@ public override VisualElement CreateInspectorGUI() fieldBindingVisualElement = new VisualElement(); rootVisualElement.Add(fieldBindingVisualElement); - m_FieldBinding = new FieldBinding(document.GetInstanceID(), fieldBindingVisualElement); - m_FieldBinding.LoadFieldsData(((UIDocument)target).visualTreeAsset); + m_FieldBinding = new FieldBinding(fieldBindingVisualElement); + m_FieldBinding.LoadFieldsData(document.visualTreeAsset); m_FieldBinding.RegisterCallbacks(); DrawDefaultProperties(rootVisualElement); diff --git a/Packages/com.shaunfong.uitoolkit-binding/package.json b/Packages/com.shaunfong.uitoolkit-binding/package.json index 24a0ecf..8f31194 100644 --- a/Packages/com.shaunfong.uitoolkit-binding/package.json +++ b/Packages/com.shaunfong.uitoolkit-binding/package.json @@ -1,6 +1,6 @@ { "name": "com.shaunfong.uitoolkit-fieldbinding", - "version": "1.2.0", + "version": "1.3.0", "displayName": "UIToolkit-fieldbinding", "description": "Code generation for ui toolkit fields.", "unity": "2021.3", diff --git a/ProjectSettings/Packages/com.shaunfong.uitoolkit-fieldbinding/uitoolkit-fieldbinding.json b/ProjectSettings/Packages/com.shaunfong.uitoolkit-fieldbinding/uitoolkit-fieldbinding.json index eb971e3..ce31ad6 100644 --- a/ProjectSettings/Packages/com.shaunfong.uitoolkit-fieldbinding/uitoolkit-fieldbinding.json +++ b/ProjectSettings/Packages/com.shaunfong.uitoolkit-fieldbinding/uitoolkit-fieldbinding.json @@ -1 +1 @@ -{"MonoBehaviour":{"m_Enabled":true,"m_EditorHideFlags":0,"m_Name":"","m_EditorClassIdentifier":"","Data":[{"Id":22382,"ScriptPath":"Assets/Examples/Scripts/BindTest.cs","FieldDatas":[{"FieldName":"Title","FieldDisplayType":"Label","FieldType":"UnityEngine.UIElements.Label","FieldSelected":true},{"FieldName":"Button1","FieldDisplayType":"Button","FieldType":"UnityEngine.UIElements.Button","FieldSelected":true},{"FieldName":"unity-checkmark","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"TextField","FieldDisplayType":"TextField","FieldType":"UnityEngine.UIElements.TextField","FieldSelected":false},{"FieldName":"unity-text-input","FieldDisplayType":"TextInput","FieldType":"UnityEngine.UIElements.TextField.TextInput","FieldSelected":false},{"FieldName":"unity-drag-container","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-tracker","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger-border","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-drag-container","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-tracker","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger-border","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-tracker","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-thumb-min","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-thumb-max","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-progress-bar","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-low-button","FieldDisplayType":"RepeatButton","FieldType":"UnityEngine.UIElements.RepeatButton","FieldSelected":false},{"FieldName":"unity-high-button","FieldDisplayType":"RepeatButton","FieldType":"UnityEngine.UIElements.RepeatButton","FieldSelected":false},{"FieldName":"unity-slider","FieldDisplayType":"Slider","FieldType":"UnityEngine.UIElements.Slider","FieldSelected":false},{"FieldName":"unity-drag-container","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-tracker","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger-border","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false}]}]}} \ No newline at end of file +{"MonoBehaviour":{"m_Enabled":true,"m_EditorHideFlags":0,"m_Name":"","m_EditorClassIdentifier":"","Data":[{"AssetPath":"Assets/Examples/Test.uxml","ScriptPath":"Assets/Examples/Scripts/BindTest.cs","FieldDatas":[{"FieldName":"Title","FieldDisplayType":"Label","FieldType":"UnityEngine.UIElements.Label","FieldSelected":true},{"FieldName":"Button1","FieldDisplayType":"Button","FieldType":"UnityEngine.UIElements.Button","FieldSelected":true},{"FieldName":"unity-checkmark","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"TextField","FieldDisplayType":"TextField","FieldType":"UnityEngine.UIElements.TextField","FieldSelected":true},{"FieldName":"unity-text-input","FieldDisplayType":"TextInput","FieldType":"UnityEngine.UIElements.TextField.TextInput","FieldSelected":false},{"FieldName":"unity-drag-container","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-tracker","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger-border","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-drag-container","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-tracker","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger-border","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-tracker","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-thumb-min","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-thumb-max","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-progress-bar","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-low-button","FieldDisplayType":"RepeatButton","FieldType":"UnityEngine.UIElements.RepeatButton","FieldSelected":false},{"FieldName":"unity-high-button","FieldDisplayType":"RepeatButton","FieldType":"UnityEngine.UIElements.RepeatButton","FieldSelected":false},{"FieldName":"unity-slider","FieldDisplayType":"Slider","FieldType":"UnityEngine.UIElements.Slider","FieldSelected":false},{"FieldName":"unity-drag-container","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-tracker","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger-border","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false},{"FieldName":"unity-dragger","FieldDisplayType":"VisualElement","FieldType":"UnityEngine.UIElements.VisualElement","FieldSelected":false}]}]}} \ No newline at end of file