Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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:
    308
    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:
    308
    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:
    126
    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.