Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

UIElements AssetReference field

Discussion in 'UI Toolkit' started by ruszem, Sep 7, 2019.

  1. ruszem

    ruszem

    Joined:
    Nov 1, 2012
    Posts:
    1
    Hi there!

    I'm stuck with creating AssetRefrence (Unity Addressable Assets) field with new ui elements system.
    How can i do that?
     
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
  3. morpheusk

    morpheusk

    Joined:
    May 18, 2014
    Posts:
    4
    I found myself with the same issue.
    The only solution I came with is to use an IMGUIContainer to draw this specific type of field.

    Code (CSharp):
    1.  
    2. class AddressableField : VisualElement
    3. {
    4.     SerializedProperty m_serializedProperty = null;
    5.     public SerializedProperty serializedProperty
    6.     {
    7.         get { return m_serializedProperty; }
    8.         set { m_serializedProperty = value; }
    9.     }
    10.     IMGUIContainer m_container = null;
    11.  
    12.     public AddressableField()
    13.     {
    14.         m_container = new IMGUIContainer();
    15.         m_container.onGUIHandler += OnGUI;
    16.         m_container.style.flexGrow = 1.0f;
    17.         Add(m_container);
    18.     }
    19.  
    20.     void OnGUI()
    21.     {
    22.         if (m_serializedProperty != null)
    23.         {
    24.             EditorGUILayout.PropertyField(m_serializedProperty);
    25.         }
    26.     }
    27.  
    28.     public new class UxmlFactory : UxmlFactory<AddressableField> { }
    29. }
    30.  
    I use this AddressableField in uxml, and in c#, i pass the serialized property.
    I hope Unity will come with their version of this type of field.
     
    Soraphis likes this.