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

Inspector For Prefab Resetting

Discussion in 'Immediate Mode GUI (IMGUI)' started by Nyarlathothep, Feb 25, 2020.

  1. Nyarlathothep

    Nyarlathothep

    Joined:
    Jun 13, 2013
    Posts:
    398
    I have an inspector for a MonoBehaviour which stores some ephemeral state (e.g. foldout state, text box input). Here's a simplified version of it:

    Code (CSharp):
    1. private bool _showDetails;
    2.  
    3. public void DrawInspectorGui()
    4. {
    5.     _showDetails = EditorGUILayout.Foldout(_showDetails, "Details");
    6.      if (!_showDetails)
    7.          return;
    8.  
    9.      // Draw details
    10. }
    When inspecting a gameObject in the scene this works just fine.

    However, when inspecting a prefab this doesn't work at all - clicking the foldout does not expand the details. A debug print in the constructor shows that every interaction with the inspector causes a new instance to be constructed, losing all state!

    So what is the correct way to store this kind of inspector state, since it obviously can't be stored in the inspector instance itself?

    Edit: I should note that if I "Open" the prefab the editor works as expected. It's only when inspecting a prefab without opening it that this is a problem.
     
    Last edited: Feb 25, 2020