Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question UXML changes from inspector not being saved

Discussion in 'UI Toolkit' started by Artheus, Dec 6, 2022.

  1. Artheus

    Artheus

    Joined:
    Aug 29, 2013
    Posts:
    9
    Hi,

    I'm using a toggle to enable/disable various parts of my custom inspector using ToggleInClassList which is working fine, but does not stay persistent after refresh. I can see the class being added in the debugger, so why is the UXML change not persistent?
     
  2. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    That's because ToggleInClassList is not modifying your UXML asset; it's just modifying a visual element that was instantiated from this asset.

    You can use either EditorPrefs or SessionState to store editor-only values. EditorPrefs values will survive even after you close Unity, SessionState values will not.

    Here's an example. You can substitute EditorPrefs for SessionState if you want.
    Code (CSharp):
    1. // Get a previously stored bool from EditorPrefs like this.
    2. // The first parameter is a unique string to identify the value.
    3. // The second parameter is the default value in case the value hasn't been set yet.
    4. bool useMyUssClass = EditorPrefs.GetBool("Use My USS class", false);
    5.  
    6. // Change the value of that bool like this.
    7. // The first parameter is a unique string to identify the value.
    8. // The second parameter is the actual value to set.
    9. EditorPrefs.SetBool("Use My USS class", true).
    10.  
     
  3. Artheus

    Artheus

    Joined:
    Aug 29, 2013
    Posts:
    9
    Thanks! It's weird the UI Toolkit documentation doesn't mention this. I guess the way they want you to do is to reapply the uxml changes every time you open the inspector.