diff --git a/Assets/BindTest_BindFields.cs b/Assets/BindTest_BindFields.cs index 17f4145..0b70bb5 100644 --- a/Assets/BindTest_BindFields.cs +++ b/Assets/BindTest_BindFields.cs @@ -7,10 +7,12 @@ 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/Assets/Editor/FieldBindingData.asset b/Assets/Editor/FieldBindingData.asset index 91e0425..63db27c 100644 --- a/Assets/Editor/FieldBindingData.asset +++ b/Assets/Editor/FieldBindingData.asset @@ -12,13 +12,4 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 6b2bfab490a6c5d4d8a19fdd3c0ebee1, type: 3} m_Name: FieldBindingData m_EditorClassIdentifier: - Data: - - Id: 22356 - ScriptPath: Assets/BindTest.cs - FieldDatas: - - FieldName: Title - FieldType: Label - FieldSelected: 1 - - FieldName: Button1 - FieldType: Button - FieldSelected: 1 + Data: [] diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 953ade4..1d751c6 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -123,67 +123,6 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} ---- !u!1 &680266220 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 680266222} - - component: {fileID: 680266223} - - component: {fileID: 680266224} - m_Layer: 0 - m_Name: UIDocument - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!4 &680266222 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 680266220} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 2 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &680266223 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 680266220} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 19102, guid: 0000000000000000e000000000000000, type: 0} - m_Name: - m_EditorClassIdentifier: - m_PanelSettings: {fileID: 11400000, guid: 8571b6b39eea8004eb24cc399ba4e5cc, type: 2} - m_ParentUI: {fileID: 0} - sourceAsset: {fileID: 9197481963319205126, guid: 7a1f76b2da568f44c82116343cf8a525, type: 3} - m_SortingOrder: 0 ---- !u!114 &680266224 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 680266220} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: e6e84114291c0384485210997429a585, type: 3} - m_Name: - m_EditorClassIdentifier: --- !u!1 &705507993 GameObject: m_ObjectHideFlags: 0 @@ -362,3 +301,51 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1913364998 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1913365000} + - component: {fileID: 1913364999} + m_Layer: 0 + m_Name: GameObject + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1913364999 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1913364998} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 19102, guid: 0000000000000000e000000000000000, type: 0} + m_Name: + m_EditorClassIdentifier: + m_PanelSettings: {fileID: 11400000, guid: 8571b6b39eea8004eb24cc399ba4e5cc, type: 2} + m_ParentUI: {fileID: 0} + sourceAsset: {fileID: 9197481963319205126, guid: 7a1f76b2da568f44c82116343cf8a525, type: 3} + m_SortingOrder: 0 +--- !u!4 &1913365000 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1913364998} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 2 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Test.uxml b/Assets/Test.uxml index 56b362c..37c68cb 100644 --- a/Assets/Test.uxml +++ b/Assets/Test.uxml @@ -4,7 +4,7 @@ - + diff --git a/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/FieldBinding.cs b/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/FieldBinding.cs index 507e792..e6d5d85 100644 --- a/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/FieldBinding.cs +++ b/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/FieldBinding.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Reflection; using UnityEditor; using UnityEditor.UIElements; using UnityEngine; @@ -64,7 +65,8 @@ internal void LoadFieldsData(VisualTreeAsset targetVisualTreeAsset) m_LoadedFieldData.Add(new FieldSelection() { FieldName = element.name, - FieldType = element.GetType().Name, + FieldDisplayType = element.GetType().Name, + FieldType = element.GetType().GetTypeFullName(), FieldSelected = (string.IsNullOrEmpty(element.name) || FieldUtility.ValidFieldName(element.name) == false) ? false : true }); } @@ -88,14 +90,14 @@ internal void RefreshFieldsList(bool force = false) FieldBindingData storageBindingData = m_FieldBindingDataManager.GetFieldBindingData(ID); 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].FieldType.Contains(m_SearchString) == false) + if (string.IsNullOrEmpty(m_SearchString) == false && m_LoadedFieldData[i].FieldName.Contains(m_SearchString) == false && m_LoadedFieldData[i].FieldDisplayType.Contains(m_SearchString) == false) continue; if (force == false && storageBindingData != null) { foreach (var fieldSelection in storageBindingData.FieldDatas) { - if (m_LoadedFieldData[i].FieldName == fieldSelection.FieldName && m_LoadedFieldData[i].FieldType == fieldSelection.FieldType) + if (m_LoadedFieldData[i].FieldName == fieldSelection.FieldName && m_LoadedFieldData[i].FieldDisplayType == fieldSelection.FieldDisplayType) { m_LoadedFieldData[i].FieldSelected = fieldSelection.FieldSelected; break; @@ -217,7 +219,7 @@ private void BindItem(FieldSelectionVisualElement e, int i) if (m_ElementInfoVisibillityState.HasFlag(ElementInfoVisibilityState.Name | ElementInfoVisibilityState.Type)) { name.text = $"{data.FieldName}"; - type.text = $"{data.FieldType}"; + type.text = $"{data.FieldDisplayType}"; } else if (m_ElementInfoVisibillityState.HasFlag(ElementInfoVisibilityState.Name)) { @@ -225,7 +227,7 @@ private void BindItem(FieldSelectionVisualElement e, int i) } else if (m_ElementInfoVisibillityState.HasFlag(ElementInfoVisibilityState.Type)) { - type.text = $"{data.FieldType}"; + type.text = $"{data.FieldDisplayType}"; } toggle.value = data.FieldSelected; diff --git a/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/FieldSelection.cs b/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/FieldSelection.cs index 578df10..2ce8995 100644 --- a/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/FieldSelection.cs +++ b/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/FieldSelection.cs @@ -9,6 +9,7 @@ namespace com.shaunfong.UIToolkitFieldBinding.editor public class FieldSelection { public string FieldName; + public string FieldDisplayType; public string FieldType; public bool FieldSelected; } diff --git a/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/ScriptGenerator.cs b/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/ScriptGenerator.cs index 5c9b86f..02ef994 100644 --- a/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/ScriptGenerator.cs +++ b/Packages/com.shaunfong.uitoolkit-binder/Editor/FieldBinding/ScriptGenerator.cs @@ -71,7 +71,7 @@ internal static string GetScriptPreview(FieldBindingData m_Data, string classNam if (m_Data.FieldDatas[i].FieldSelected == false) continue; content += (string.IsNullOrEmpty(classNameSpace) ? " " : " ") + - $"public UnityEngine.UIElements.{m_Data.FieldDatas[i].FieldType} {m_Data.FieldDatas[i].FieldName}" + " { get; private set; }" + + $"public {m_Data.FieldDatas[i].FieldType} {m_Data.FieldDatas[i].FieldName}" + " { get; private set; }" + (i == m_Data.FieldDatas.Count - 1 ? "" : "\n"); } @@ -81,7 +81,7 @@ internal static string GetScriptPreview(FieldBindingData m_Data, string classNam if (m_Data.FieldDatas[i].FieldSelected == false) continue; binding += (string.IsNullOrEmpty(classNameSpace) ? " " : " ") + - $"{m_Data.FieldDatas[i].FieldName} = root.Q(\"{m_Data.FieldDatas[i].FieldName}\");\r\n"; + $"{m_Data.FieldDatas[i].FieldName} = root.Q<{m_Data.FieldDatas[i].FieldType}>(\"{m_Data.FieldDatas[i].FieldName}\");\r\n"; } result = result. diff --git a/Packages/com.shaunfong.uitoolkit-binder/Editor/Utility/Utility.cs b/Packages/com.shaunfong.uitoolkit-binder/Editor/Utility/Utility.cs index 357c739..c9ab8c1 100644 --- a/Packages/com.shaunfong.uitoolkit-binder/Editor/Utility/Utility.cs +++ b/Packages/com.shaunfong.uitoolkit-binder/Editor/Utility/Utility.cs @@ -1,5 +1,7 @@ +using System; using System.Collections.Generic; using System.IO; +using System.Reflection; using System.Text.RegularExpressions; using UnityEditor; using UnityEngine; @@ -65,6 +67,18 @@ public static string GetGlobalPath(this string path) { return (Application.dataPath.Replace("Assets", "") + path).Replace("\\", "/"); } + + public static string GetTypeFullName(this Type type) + { + if (type.GetTypeInfo().DeclaringType == null) + { + return type.GetTypeInfo().FullName; + } + else + { + return GetTypeFullName(type.GetTypeInfo().DeclaringType) + "." + type.Name; + } + } } internal static class SettingUtility