Search Unity

How to go display dictionary values only once in OnGUI.I'm having - 20 fps.

Discussion in 'Scripting' started by Reedex, Nov 7, 2018.

  1. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    this is a snippet of a panel in OnGUI.it only goes when i'm in skill tab.profiler goes like this:
    (closed skilltab) : __,,,__,_ open skill tab : _-----------.
    Code (CSharp):
    1. int j = 0;
    2.  
    3.  
    4.         foreach (KeyValuePair<Skill_NAMES_MIGHTaMAGIC,Skill_RANKS_MIGHTaMAGIC> keyValue in SkillMANAGER.skillsAndranks) {
    5.  
    6.             Skill_NAMES_MIGHTaMAGIC key = keyValue.Key;
    7.             GUI.Label (new Rect (10, 0 + j, 120, 28), keyValue.Value.ToString (), "SkillButton");//keyValue.Key.ToString() +
    8.             j += 28;
    9.         }
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    hmm.. either you have a lot of skills or the ToString() method is doing crazy stuff...
    maybe you can post both information.

    Edit:
    I would recommend not to use the old UI framework for your game. Use uGUI or maybe the experimental UIElements or a UI Engine from the Asset Store...
    Almost everything is better than the old UI system. It is known for being super slow and hard to use...
     
    Reedex likes this.
  3. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389

    YES my profiler goes like this ______(i open ongui inventory)------------ NO KIDDING!!from nothing to half full like snap. from 2.5ms to 6-8 and with those 22 skills tab open, 7-8.5


    I'll go check that out immediately thanks
    ps what you mean post both info?
     
  4. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    1. how many items are in the dictionary
    2. what code is in the
      ToString()
      method
    Post this information, if the performance is still bad with a different UI system.
    (BTW: if you use UGUI, which is included in Unity, you will most likely want to put all your items as childs in a
    Vertical Layout Group
    )
     
  5. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    dictionary is 22 skils and 7 ranks

    code in method(if thats what you want,not sure)
    Code (CSharp):
    1. private void DisplayRealSkills()
    2.     {
    3.         GUI.BeginGroup (new Rect (30, 120, _CharacterWindowRect.width - (offset * 2), _CharacterWindowRect.height - 50));
    4.  
    5.         GUI.Label (new Rect (Screen.width / 3,60,360,30),"Level                            " + xps.CurrLevel.ToString(),"SkillButton");
    6.  
    7.         if(xps.currentXP >= xps.nextLevelUp)
    8.             GUI.Label (new Rect (Screen.width / 3,90,360,30),"<color=lime> Current Experience : </color> " + xps.currentXP.ToString(),"SkillButton");
    9.         else
    10.             GUI.Label (new Rect (Screen.width / 3,90,360,30),"Current Experience : " + xps.currentXP.ToString(),"SkillButton");
    11.        
    12.         GUI.Label (new Rect (Screen.width / 3, 120, 360, 30), "Current Next Level : " + xps.nextLevelUp.ToString (), "SkillButton");
    13.  
    14.         GUI.Label (new Rect (Screen.width / 3 ,0, 360,30),"Skill Points : " + PC.Instance.skillpoints.ToString(),"SkillButton");
    15.    
    16.  
    17.  
    18.         //        ↓        ↓        ↓THINGIE TOO MUCH OF A SLOWDOWN    /although gotta be there later on/
    19.  
    20.         int j = 0;
    21.  
    22.         foreach (KeyValuePair<Skill_NAMES_MIGHTaMAGIC,Skill_RANKS_MIGHTaMAGIC> keyValue in SkillMANAGER.skillsAndranks) {
    23.             GUI.Label (new Rect (10, 0 + j, 120, 28), keyValue.Value.ToString (), "SkillButton");//keyValue.Key.ToString() +
    24.             j += 28;
    25.         }
    26.  
    27.        
    28.         //        ↑        ↑        ↑THINGIE TOO MUCH OF A SLOWDOWN /although gotta be there later on/
    29.    
    30.    
    31.  
    32.         for (int i = 0; i < PC.Instance.realSkills.Length; i++)
    33.         {
    34.  
    35.             if (GUI.Button (new Rect (120, i * 28, 210, 28), (Skill_NAMES_MIGHTaMAGIC)i + s + "\t" + PC.Instance.GetSkillMIGHTaMagic (i).AdjustedBaseValue.ToString (), "SkillButton"))
    36.  
    37.             {
    38.                 if (Event.current.button == 0)
    39.                 {
    40.                     if (PC.Instance.skillpoints >= PC.Instance.GetSkillMIGHTaMagic (i).AdjustedBaseValue + 1)
    41.                     {
    42.                         source.PlayOneShot (Click, 0.8f);
    43.                         PC.Instance.skillpoints -= PC.Instance.GetSkillMIGHTaMagic (i).AdjustedBaseValue + 1;
    44.                         PC.Instance.GetSkillMIGHTaMagic (i).BuffValue++;
    45.                     }
    46.                 }
    47.             }
    48.         }
    49.  
    50.         GUI.EndGroup();
    51.     }
     
  6. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    no, I wanted to know what is inside
    Skill_RANKS_MIGHTaMAGIC.ToString()
    .
     
  7. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    ohhhh,. Skill_RANKS_MIGHTaMAGIC is enum with {novice ,skillfull etc} as well as Skill_NAMES_MIGHTaMAGIC{sword,mace etc}
     
  8. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    ah, ok. Then the ToString() method is for sure not the bottleneck :D
     
    Reedex likes this.
  9. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    i am now sure its just the GUI i'm using because just standing a i have 250 fps, and with opened any tab ,skills,stats,inventory i go to 150
    but i have a spellbook done as canvas panel..and i go from 250 to 220 max so, its gotta be the ongui as a whole

    I thought of somehow caching the ranks instead of that foreach in ongui,cant figure out how though,because i really would be ok if it updates if i press " I " since i open inventory that way...
     
  10. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,518
    Turn on the profiler, set it to deep profile, then see where the most time is consumed.
     
  11. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Do I still need to convince you to go for a different UI framework?

    Okay: As far as I know, the old UI System calls OnGUI several times per frame to be "sure" not to miss any user input. That's not just a stupid hack, it is also slowing down the UI by a factor (dunno how often though).
    Maybe the UI performance would be acceptable if it would draw it only once per frame.
    But still: It is so hard to create the UI and in most cases it doesn't look very good in the end...
    So, please switch. ;)
     
  12. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    gui repaint 60%
     
  13. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,518
    You can dig much deeper and identify specific things eating time.
     
  14. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    well the gui repaint with its callwindowdelegate and then the : displayrealskills(as see above) with its 57% with its string concat 16% and enum.tostring with 13%.
     
  15. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,518
    Well there you go. Working with strings is expensive.
     
  16. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    hmm... i cannot see any concat operation in the code you posted.
    anyways: It is only expensive because the UI system runs the code several times per frame. With another UI solution you would run such code once on loading time...
     
  17. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I'm with the others here. Leave the old system alone and switch to an alternative. It saves time to set things up, requires less code, easier to design and on top of all, it's more performant.

    He's got lots of them (the + operator).
     
    xVergilx likes this.
  18. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Ah, the second posted source code... I didn't look into that too deep because Reedex sayed it would peak in the code of the first posted source code.
     
  19. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389

    ok, so i switch, but to what?can you link me?
     
  20. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The old GUI system is now specifically not recommended by Unity for anything but developer output to be removed in a final build. If you're not using it for an FPS meter, or displaying some metrics useful only to QA, then you're doing it wrong.

    When you go to the Unity manual it is that big section called "UI". Hard to miss, but I'll link to it below anyway. The old GUI is now called IMGUI by the way, since you'll see it referenced in that section as well.

    https://docs.unity3d.com/Manual/UISystem.html

    If you're new to Unity, I'd recommend becoming familiar with all the major features discussed in the top level pages in the manual, so that things like this don't come as a surprise in the future.
     
    Last edited: Nov 9, 2018
    Hosnkobf likes this.