Skip to content

Commit

Permalink
refactor: change Type.FullName to generate type string.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun-Fong committed Feb 7, 2023
1 parent f96ba23 commit b80e198
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 79 deletions.
2 changes: 2 additions & 0 deletions Assets/BindTest_BindFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UIDocument>().rootVisualElement;
Title = root.Q<UnityEngine.UIElements.Label>("Title");
Button1 = root.Q<UnityEngine.UIElements.Button>("Button1");
TextField = root.Q<UnityEngine.UIElements.TextField>("TextField");
}
}
11 changes: 1 addition & 10 deletions Assets/Editor/FieldBindingData.asset
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
109 changes: 48 additions & 61 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
2 changes: 1 addition & 1 deletion Assets/Test.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ui:Label text="Label" display-tooltip-when-elided="true" name="Title" />
<ui:Button text="Button1" display-tooltip-when-elided="true" name="Button1" />
<ui:Toggle label="Toggle" />
<ui:TextField picking-mode="Ignore" label="Text Field" value="filler text" text="filler text" />
<ui:TextField picking-mode="Ignore" label="Text Field" value="filler text" text="filler text" name="TextField" />
<ui:Slider picking-mode="Ignore" label="Slider" value="21.7" high-value="100" />
<ui:SliderInt picking-mode="Ignore" label="SliderInt" value="66" high-value="100" />
<ui:MinMaxSlider picking-mode="Ignore" label="Min/Max Slider" min-value="10" max-value="12" low-limit="-10" high-limit="40" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
Expand Down Expand Up @@ -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
});
}
Expand All @@ -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;
Expand Down Expand Up @@ -217,15 +219,15 @@ private void BindItem(FieldSelectionVisualElement e, int i)
if (m_ElementInfoVisibillityState.HasFlag(ElementInfoVisibilityState.Name | ElementInfoVisibilityState.Type))
{
name.text = $"<color=#93B3F8>{data.FieldName}</color>";
type.text = $"<color=#AAAA5B>{data.FieldType}</color>";
type.text = $"<color=#AAAA5B>{data.FieldDisplayType}</color>";
}
else if (m_ElementInfoVisibillityState.HasFlag(ElementInfoVisibilityState.Name))
{
name.text = $"<color=#93B3F8>{data.FieldName}</color>";
}
else if (m_ElementInfoVisibillityState.HasFlag(ElementInfoVisibilityState.Type))
{
type.text = $"<color=#AAAA5B>{data.FieldType}</color>";
type.text = $"<color=#AAAA5B>{data.FieldDisplayType}</color>";
}

toggle.value = data.FieldSelected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace com.shaunfong.UIToolkitFieldBinding.editor
public class FieldSelection
{
public string FieldName;
public string FieldDisplayType;
public string FieldType;
public bool FieldSelected;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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<UnityEngine.UIElements.{m_Data.FieldDatas[i].FieldType}>(\"{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.
Expand Down
14 changes: 14 additions & 0 deletions Packages/com.shaunfong.uitoolkit-binder/Editor/Utility/Utility.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b80e198

Please sign in to comment.