Search Unity

Bug Making a custom VisualElement [System.Serializable] kills the editor

Discussion in 'UI Toolkit' started by LudiKha, Sep 10, 2020.

  1. LudiKha

    LudiKha

    Joined:
    Feb 15, 2014
    Posts:
    140
    See title.

    Exposing the element in an inspector will interfere with the serialization, freeze compilation, crash the editor and give a black screen on subsequent startups.

    How to reproduce:
    - Create custom visual element that is [System.Serializable]
    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class View : BindableElement
    4.   {}
    - Expose one of these in an inspector
    Code (CSharp):
    1.  
    2. [SerializeField] View defaultView;
    3. [SerializeField] List<View> views = new List<View>();
    Cheers

    - K
     
  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    700
    Hello @Ragoo , unfortunately VisualElements are non serializable by nature and you shouldn't declare a custom visual element that introduces any kind of serialization, or Unity will have trouble indeed :(

    It shouldn't crash, of course, and we should address this under this bug (https://issuetracker.unity3d.com/is...ference-to-a-serializable-visualelement-class) but serializing VisualElement and its extensions is not something that we support or are planning on supporting.

    Is there something specific you need to expose in the inspector that we could help you find alternatives, though? Let us know :)
     
    LudiKha likes this.
  3. LudiKha

    LudiKha

    Joined:
    Feb 15, 2014
    Posts:
    140
    Hi @JuliaP_Unity , thanks for the quick reply. I only serialized out of curiosity, so won't need them for any practical purposes, but thanks for the offer!
     
  4. FoodFish_

    FoodFish_

    Joined:
    May 3, 2018
    Posts:
    58
    It would be good to know how to expose SerializeField to the VisualElement's inspector. Any added serializefield added to my class do not appear.
    upload_2022-7-17_17-42-34.png

    upload_2022-7-17_17-41-56.png
     
  5. FoodFish_

    FoodFish_

    Joined:
    May 3, 2018
    Posts:
    58
    Doing more reading.. What about UxmlObjectAttributeDescription? Why is it marked as internal?
     
  6. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    480
    Hey @FoodFish_

    This attribute is about being able to set UnityEngine.Object asset references as attributes in the Uxml. This will allow you for example to define a Uxml attribute for a Texture2D.

    It's internal at the moment because we have started working on it, but it's not quite ready yet. Before we make it public, we have to ensure that the Uxml files containing an asset reference as an attribute:
    • will be properly re-imported when the asset is moved/renamed/deleted,
    • will include the referenced asset automatically in a build if the Uxml file is,
    • has proper support in the UI Builder.
    Hope this helps!