Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Setting the binding path to a value inside a struct?

Discussion in 'UI Toolkit' started by Buretto, Mar 3, 2020.

  1. Buretto

    Buretto

    Joined:
    Mar 23, 2015
    Posts:
    42
    I'm trying to figure out how to use the new UI elements to display a struct and some of its contained properties.

    Like for example, in the old IMGUI or w.e. you could use
    SerializedProperty.FindPropertyRelative()
    . Is there a way to set the binding path to do this? Maybe I've missed something.
     
  2. alexandred_unity

    alexandred_unity

    Unity Technologies

    Joined:
    Dec 12, 2018
    Posts:
    12
    I can look at https://docs.unity3d.com/2019.3/Documentation/Manual/UIE-Binding.html

    Here is a small example:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4. using UnityEditor.UIElements;
    5.  
    6. public class BindableObject : ScriptableObject
    7. {
    8.     public int intField = 5;
    9.     public bool boolField = true;
    10.     public float floatField = 0.6f;
    11.     public string stringField = "some text.";
    12.     public Color colorField = Color.green;
    13. }
    14.  
    15. public class MyBoundWindow : EditorWindow
    16. {
    17.     [MenuItem("UIElements/MyBoundWindow")]
    18.     public static void ShowWindow()
    19.     {
    20.         MyBoundWindow wnd = GetWindow<MyBoundWindow>();
    21.         wnd.titleContent = new GUIContent("MyBoundWindow");
    22.     }
    23.  
    24.     public BindableObject m_BindableObject = null;
    25.  
    26.     public void OnEnable()
    27.     {
    28.         m_BindableObject = ScriptableObject.CreateInstance<BindableObject>();
    29.         var serializedObject = new SerializedObject(m_BindableObject);
    30.  
    31.         // Each editor window contains a root VisualElement object
    32.         VisualElement root = rootVisualElement;
    33.  
    34.         // Loop through properties and create one field for each top level property.
    35.         SerializedProperty property = serializedObject.GetIterator();
    36.  
    37.         property.NextVisible(true);
    38.         do
    39.         {
    40.             // Create the UIElements PropertyField.
    41.             var uieDefaultProperty = new PropertyField(property);
    42.  
    43.             root.Add(uieDefaultProperty);
    44.         }
    45.         while (property.NextVisible(false));
    46.  
    47.         // Also
    48.  
    49.         property = serializedObject.FindProperty("intField");
    50.         root.Add(new PropertyField(property));
    51.  
    52.         // Also
    53.  
    54.         root.Add(new FloatField() {bindingPath = "floatField" });
    55.  
    56.         // Bind the entire window.
    57.         root.Bind(serializedObject);
    58.     }
    59. }
    60.  
     
unityunity