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

Foldable Nested Lists in Custom Editor

Discussion in 'Scripting' started by Ghujelk, Feb 23, 2020.

  1. Ghujelk

    Ghujelk

    Joined:
    May 21, 2015
    Posts:
    15
    I've just started looking into editor scripting, to make my tower defense project a bit easier to set up.
    What I'm trying to achieve is a tool where I can Add/Remove a new wave to a list, and be able to toggle the display of that waves contents, on a wave by wave basis. So I could have wave one displayed, but wave 2 & 3 collapsed. I can display that in the default Unity Inspector. Utilizing System.Serializable, and Serialize Field where appropriate. But it's a bit cumbersome and I was hoping to learn some new skills by tackling this. Following some of the information from this Unite video;

    I've been able to get a list with add/remove functionality just fine, It's the show.hide portion that is presenting a challenge. Most working examples I've found have a single local bool that the EditorGUILayout.Foldout() function modifies. An if statement checks said bool, and if true, displays the defined content. However, with only one bool being referenced, I can't have a per wave value obviously, and toggling it expands all displayed content or collapses all content. The next thing I tried was to create a list of bools and have each wave reference it's corresponding bool with the same index but nothing seems to happen. Is there some reason this doesn't work with a list? Do I need to use an array instead? Or is there some other quirk of the editor scripting that only allows certain datatypes? Here's a pastebin link with all the relevant classes and scripts: https://pastebin.com/UuPgNESf
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    I've only glanced your code but it seems to be proper. You're just need getting used to IMGUI, which is admittedly a very weird system. The solution is very simple: you need to make local bool fields in your custom editor that keep EditorGUILayout.Foldout states between the calls.

    [edit]
    here, check out this example, it should help you
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    ohnoes, you did it exactly like that :D

    ok ok, you need to save the state for each foldout you're using. otherwise, they'll all repeat each other states, which is what you've observed.

    also, making lists is best done by making a custom drawer, because the inspector code is repeated at every cascade (in case of a list of lists), and you don't want to keep track of all of that, in just one editor, obviously.

    my ramblings about it:
    however, I don't find the whole custom editor thing very usable or intuitive, at least if you don't have a multidisciplinary team of developers who need to tackle with the internals and technicalities in a UI friendly manner.

    it always turns up incredibly messy and maintenance-unfriendly, not to mention the undocumented stuff, and it's only really feasible to do in slow dev cycles (which is completely counterintuitive when you think about it, I need UI editors to be quick and agile, and to automate stuff). finally, I gave up completely, what a waste of time. I ultimately use only some of its features, and [ExecuteInEditMode] works wonders for quick prototypes and basic interaction anyway. I hacked my way into capturing mouse as well, and I rarely need drawers for anything, but they have their uses.

    IMGUI is a very weird solution, and I come from the beauty of ActionScript (had to abandon it unfortunately a long time ago). I don't know if you've ever experienced how a proper UI system should feel like and be set up for a programmer to feel really creative, but this couldn't be any farther from such a solution. thank god they're moving to UIElements.

    what I'm trying to say, it's not indie-friendly at all. when you have a regular paycheck and consider building custom editors a task like any other, then it's something else probably. but they should really streamline this, or else only big companies can contribute with 3rd party tools, which is a shame. I want my editors be done in less than one afternoon, not developed in weeks like a product they're not supposed to be.

    this is just my opinion, don't feel hindered by it.
     
    Last edited: Feb 23, 2020
    Nwy140 likes this.
  4. Ghujelk

    Ghujelk

    Joined:
    May 21, 2015
    Posts:
    15
    I figured it out, I misunderstood how often OnInspectorGUI was called. Adding a Debug.Log let me see that it's sort of like an Update method. I was clearing and recreating the bool list multiple times and since they were always being created with a value of true the foldouts never would collapse. I added a check so that I'd only clear and remake the bool list if the bool list and wave list had different counts, and now it works just as expected!
     
    orionsyndrome likes this.