Skip to content

Commit

Permalink
fix: fixed script tracking.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun-Fong committed Mar 11, 2023
1 parent 921662e commit 07fad4d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Assets/Examples/Scripts/BindTest_BindFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UIDocument>().rootVisualElement;
Title = root.Q<UnityEngine.UIElements.Label>("Title");
Button1 = root.Q<UnityEngine.UIElements.Button>("Button1");
TextField = root.Q<UnityEngine.UIElements.TextField>("TextField");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldSelection> m_LoadedFieldData = new List<FieldSelection>();
public List<FieldSelection> DisplayFields { get; private set; } = new List<FieldSelection>();
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldSelection> FieldDatas = new List<FieldSelection>();

public FieldBindingData(int id)
public FieldBindingData(string assetPath)
{
Id = id;
AssetPath = assetPath;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ public class FieldBindingDataManager : ScriptableObject
[SerializeField]
public List<FieldBindingData> Data = new List<FieldBindingData>();

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;
Expand All @@ -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();
}
Expand All @@ -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)
{
Expand All @@ -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)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Packages/com.shaunfong.uitoolkit-binding/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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}]}]}}
{"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}]}]}}

0 comments on commit 07fad4d

Please sign in to comment.