Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Reading the List<> and make a new Toggle in Unity Editor

Discussion in 'Extensions & OnGUI' started by NieWychylamm, May 28, 2018.

  1. NieWychylamm

    NieWychylamm

    Joined:
    Mar 12, 2018
    Posts:
    2
    I want make something like this:
    Code (CSharp):
    1.  
    2. using UnityEditor;
    3.  
    4. []
    5.  
    6. public string ChapterField;
    7. public List<string> ChapterName = new List<string>();
    8.  
    9.  
    10. private void OnGUI()
    11.     {
    12. ChapterField = EditorGUILayout.TextField(ChapterField);
    13.  
    14.  
    15. if (GUILayout.Button("Add Chapter")) { ChapterName.Add(ChapterField); }
    16. AddChapter();
    17. }
    18.  
    19.  
    20.  
    21. public void AddChapter()
    22.     {
    23.    
    24. if (ChapterName.Count != 0)
    25. {
    26.             for (int i = 0; i <ChapterName.Count; i++)
    27.         {
    28.                 string controlChapterName;
    29.                 controlChapterName = ChapterName[i];
    30.                  GUILayout.Toggle(false, controlChapterName);
    31.          }
    32. }
    33.        }
    It's reading the list and making a new Toggle with name from list.
    This code work but i feel it's very bad code, anyway took to much memory. How i can make this more correctly and optimally it's possible? Thanks in advance
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    450
    Looks fine to me. Not sure why you're thinking it takes too much memory.
     
    NieWychylamm likes this.
  3. NieWychylamm

    NieWychylamm

    Joined:
    Mar 12, 2018
    Posts:
    2
    I think it takes too much memory because when I add more than about 15 elements to the List<> only window of my editor script start lagging and responds slower :( I have medium computer but i have intel i7 5th generation and it's not a computer problem
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    450
    That wouldn't be a memory problem. Sounds like for some reason your window is doing more work than it should be. You can profile the editor using the Profiler window. I'd suggest you check it out to see what's taking so long in your code.
     
    NieWychylamm likes this.
  5. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    133
    Unity sucks with arrays anyway, if you expand an array with several thousand elements (or millions ( don't ask. ) ) and each of those elements is a default value (null). it will grind the editor to a halt! which means its very difficult to correct the size of the array
     
    NieWychylamm likes this.