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

Garbage collector spikes because of GUI

Discussion in 'Editor & General Support' started by BearishSun, Sep 13, 2010.

  1. BearishSun

    BearishSun

    Joined:
    Dec 1, 2008
    Posts:
    175
    Our application seems to be suffering from serious garbage collector spikes in the profiler every few frames. The spikes are definitely being caused by OnGUI functions.

    As long as there is a single OnGUI overload in the scene, the spikes will appear. They will drop the fps sometimes by 90% for a single frame. There's no noticeable difference if there is something inside the OnGUI method, or if it's empty. As long as it's there the spikes happen.

    Adding more scripts with overloaded OnGUI will cause spikes to happen more often.

    Most of the time spent during those spikes are GC.Collect calls (generally from GUI.Repaint->BeginGUI).

    This happens with both Unity 2 or 3.

    Simple experiment
    Scene with 1 cube and 1 camera, with the following script attached to the cube GameObject:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class OnGUITest : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.     }
    14.  
    15.     void OnGUI()
    16.     {
    17.     }
    18. }
    19.  
    Profiler result:


    Now lets try that same scene, but with the OnGUI function commented out:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class OnGUITest : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.     }
    14.  
    15.     //void OnGUI()
    16.     //{
    17.     //}
    18. }
    19.  
    Profiler result:


    You can see how this can cause a lot of problems in a larger scene where we try to maintain an fps of 60, but the spikes happen every few frames and lower it to 15-20, which is very noticeable. (And if you have multiple overridden OnGUI methods, they are even more common)

    I understand that GC needs to do its job, but correct me if I'm wrong, such huge spikes shouldn't be caused by an empty overload of OnGUI function.

    So my question is: Any way to get around this? Other than using 3rd party GUI system.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You seem to forget one elemental part in the whole thing:

    Start, Update, LateUpdate, FixedUpdate, OnGUI etc are not functions. They are SendMessage targets (thats how these functions are called basically by the engine, though a bit more optimized) as such they create an overhead.
    In case of OnGUI, this overhead though is quite a bit larger, because OnGUI gets called once per event and potentially allocating the event as the "OnGUI phase" starts.
    Also it has to initialize the extra state within the engine (gui happens distinctly after all the regular rendering has passed etc) which has an additional overhead (thats likely the BeginGUI part you see)

    General rule is: don't have ongui, update, lateupdate and fixedupdate in if you don't use them.
    NEVER have them in with an "if( bla ) return" or direct return cause the major overhead has been payed at that point already!


    that the whole stuff shows up as spike is likely caused by the fact that it is about the only thing that causes alloc and dealloc so there is no other stuff going on that would make the spikes become just minimals differences, showing them "out of scale" at the moment
     
    rahulk1991 and magic9cube like this.
  3. BearishSun

    BearishSun

    Joined:
    Dec 1, 2008
    Posts:
    175
    I know what you mean dreamora and it's no because of that. The source of my problems is a much larger scene with almost 2000 draw calls, dozens of scripts and pretty much an entire game. And these spikes still occur and are very noticeable.

    They disappear immediately after I disable every last script that overloads OnGUI. (Even if all that is remaining is a single lone script with empty OnGUI overload)



    Edit:
    And just for comparison here's that same scene running at nice 120 fps with all OnGUI scripts disabled.



    If I add that "empty" script from my first post, I immediately end up with something like the first image from this post, with FPS being around 60 and spikes to 15.
     
  4. BearishSun

    BearishSun

    Joined:
    Dec 1, 2008
    Posts:
    175
    Nobody else has this problem? It definitely isn't isolated to my PC as my colleagues have the same issue.
     
  5. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    I have the same problem.

    "General rule is: don't have ongui, update, lateupdate and fixedupdate in if you don't use them.
    NEVER have them in with an "if( bla ) return" or direct return cause the major overhead has been payed at that point already!"

    I assume you mean that this would be bad:
    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.    if (thing_is_off || some_other_condition)
    5.       return;
    6.    else
    7.    {
    8.      do_something_interesting.....
    9.    }
    10. }
    11.  
    ??
    If so, how would you go about it?
     
  6. BearishSun

    BearishSun

    Joined:
    Dec 1, 2008
    Posts:
    175
    He probably meant by using enabled = false instead, however that's not an option when you need e.g. LateUpdate but don't need Update.
     
  7. jorge_machin

    jorge_machin

    Joined:
    Dec 9, 2009
    Posts:
    26
    I have the same problem.

    Any code examples are welcome! :)
     
  8. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    I have the same problem, and yes, it's OnGUI fault too.
    I'm implementing what i have done in another way , but from what I can see the solution is to go around the problem, and implement your stuff in another way.
    I don't know if this is a bug or not but, can't you implement what you have in OnGui somewhere else ?
     
  9. BearishSun

    BearishSun

    Joined:
    Dec 1, 2008
    Posts:
    175
    Well we're extending some of the community sprite GUI managers to get avoid this problem.

    But if we can use those to achieve the same functionality as OnGUI, without spikes, then i don't see why Unity can't also. So I like to consider this a bug.
     
  10. hypnoslave

    hypnoslave

    Joined:
    Sep 8, 2009
    Posts:
    436
    BUMP!

    any update on this? I'm having the same problem.

    For me though, I get steady framerates, but sometimes seemingly at random this will start happening to me.

    I have several scripts with an OnGUI function..... in fact, I have a whole list of by default disabled scripts that are enabled and disabled by an Overlay Manager...
     
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I don't understand... how CAN you avoid using update, lateupdate and so on. Aren't these the only way you can have any code running?!
     
    rahulk1991 likes this.
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can merge related code to be called from a manager. For AI for example thats normal, you don't want the ai think to tick each frame so you have a manager that knows when to tick what and can also facilitate "team related ai" for example
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No; you can use things like coroutines and InvokeRepeating.

    Code (csharp):
    1. function Start () {
    2.     while (true) {
    3.         transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * 10.0 * Time.deltaTime);
    4.         yield;
    5.     }
    6. }
    --Eric
     
  14. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Seems pretty horrific.
     
    rahulk1991 likes this.
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, that was essentially just a replacement for Update. Real usage would be something more like, e.g. to put in a delay after firing a shot (from here):

    Code (csharp):
    1. var shotDelay = .5;
    2.  
    3. function Start () {
    4.     while (true) {
    5.         while (!Input.GetButtonDown("Fire")) yield;
    6.         // Fire bullet here
    7.         yield WaitForSeconds(shotDelay);
    8.     }
    9. }
    Update is for stuff that runs every frame, and it turns out you don't often actually want stuff running every single frame, so I rarely use Update for much, since it's a lot easier to schedule events with coroutines.

    --Eric
     
  16. MarkGX

    MarkGX

    Joined:
    Dec 18, 2010
    Posts:
    30
    Hey guys i know there are some experienced bloggers in this forum talking about this. But when can we wise up that a product like unity offering a GUI which Fxxcksss up the FPS so badly due to garbage collection issues must be seen as very inefficient/bad architecture model under the hood. I shelled out for the pro license and require it for MMO gaming which needs inventory windows, chat windows. Where do i go from here, can someone tell Unity to put a "Please dont purchase if you want to do this in a game" on their buy screen"" frankly i'm frustrated since i will have to start going elsewhere for a work around. GUI is like Err Duhhh if im not going crazy an essential component of any dev environment these days!!! Wake up guys
     
  17. x46085

    x46085

    Joined:
    Sep 18, 2010
    Posts:
    12
    Well this is a happy collection of programmers. Just wanted to throw my "yep this happens to me too" in with the lot, and its a game ending issue for me. So, I've already purchased Above and Beyond's Sprite Manager 2 and EZ Gui systems, and will send the next week attempting to check and see if that helps any. If anyone has already tried this, please speak up, but if not I'll report back in a week or so.
     
  18. MarkGX

    MarkGX

    Joined:
    Dec 18, 2010
    Posts:
    30
    I've gone down the Flash Unity Integration path with much success. It simply means i can avoid the dreaded OnGUI() which is causing all the pain. In the process i discovered that Flash GUI components are so much easier to work with (e.g. mx.tree component) than building with Unity GUI.

    The solution is not for everyone though

    1. Must be using the Unity Web Player and Flash Plugin Player/ or browser javascript in a browser.
    2. You can design your app such that the GUI Interfaces remain outside the unity web player.

    If anyone interested i might be able to get some useful links and sample code of this working.
    I know there are links around showing this...
     
  19. kingoftaurus

    kingoftaurus

    Joined:
    Jan 3, 2011
    Posts:
    104
    Indeed this happens to me as well. Im glad i stumbled on this thread. I just thought it was me and that unity overall was "twitchy" even though i have constant framerates of 300+

    OnGUI really seems to be the issue and perhaps coding in a way to even need garbage collection is bad i suppose. Like say, im getting these spikes just from having a custom cursor on the screen.

    Interesting.
     
  20. x46085

    x46085

    Joined:
    Sep 18, 2010
    Posts:
    12
    Victory is mine. Using EZGui seems to avoid the GC spike, and after looking at how EZ Gui does thier implementation, you can do something similar without spending any money. They basically just set up a system to use regular game objects as their GUI widgets, operating everything without a call to OnGUI. So, if you just use game objects for you GUI and lock them to the camera, you could probably do the same thing without too much difficulty.
     
  21. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Actually already using GUIX gets rid of the spikes and it performs several worlds better than OnGUI out of the box.
    Just in case you would like to use the full gui and skins etc
     
  22. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I'm seeing this as well. My 5 line OnGUI is causing spikes and are eating up 93%+ of the CPU operations. Guess it's time I look into GUIX or EZGUI as well.

    @Dreamora - I just tried the GUIX demo and I'm seeing the same spikes in the profiler as my own code. So far I've just tried the sample GUI that comes with the demo, but it's showing GUI related spikes in the Profiler. Is that just because it's the demo?
     
    Last edited: Jan 31, 2011
  23. KevS

    KevS

    Joined:
    Apr 21, 2010
    Posts:
    51
    Same problem here :( I have these ****ing spikes, and i try to fix them since 2 days...When I read this post the first time, i cleaned and refactored a bit my code to be sure that there were no unnecessary OnGUI functions.

    Now i have only ONE script in my scene with the OnGUI() function :

    Code (csharp):
    1.  
    2. public void OnGUI()
    3.     {
    4.         if (displayHud)
    5.         {
    6.             Hud();
    7.         }
    8.         if (displayInGameMenu)
    9.         {
    10.             InGameMenu();
    11.         }
    12.     }
    13.  
    14.     public void Hud()
    15.     {
    16.         //draw the reticule
    17.         GUI.DrawTexture(new Rect(Screen.width / 2 - (reticule.width * 0.5f), Screen.height / 2 - (reticule.height * 0.5f), reticule.width, reticule.height), reticule);
    18.  
    19.         //draw target health
    20.         if (isTargetEnemy)
    21.         {
    22.             GUI.Label(new Rect(Screen.width / 2 + (reticule.width * 0.5f) + 10f, Screen.height / 2 - 10, 40, 20), targetHealth + "%");
    23.         }
    24.  
    25.         //draw controls help
    26.         GUI.BeginGroup(new Rect(5, 5, 170, 200));
    27.         GUI.Box(new Rect(0, 0, 170, 200), "Controls");
    28.         GUI.Label(new Rect(10, 20, 150, 20), "Move : Q,Z,S,D");
    29.         GUI.Label(new Rect(10, 50, 150, 20), "Brake : Space");
    30.         GUI.Label(new Rect(10, 80, 150, 20), "Reset car : R");
    31.         GUI.Label(new Rect(10, 110, 150, 20), "Lock/Unlock cursor : C");
    32.         GUI.Label(new Rect(10, 140, 150, 20), "Mouse Sensitivity :");
    33.         sensitivityX = sensitivityY = GUI.HorizontalSlider(new Rect(10, 170, 150, 50), sensitivityX, 200, 600);
    34.         GUI.EndGroup();
    35.         //Display the equiped weapons
    36.         GUI.BeginGroup(new Rect(Screen.width * 0.75f, Screen.height - 100f, Screen.width * 0.25f, 100f));
    37.         GUI.Box(new Rect(0, 0, Screen.width * 0.25f, 100), "Weapons");
    38.         GUI.Label(new Rect(10, 20, Screen.width * 0.25f - 20, 30), "Left click : " + playerWeaponsController.primaryWeapon.weaponName);
    39.         GUI.Label(new Rect(10, 50, Screen.width * 0.25f - 20, 30), "Right click : " + playerWeaponsController.secondaryWeapon.weaponName);
    40.         GUI.EndGroup();
    41.  
    42.         //speed km/h
    43.         int speed = (int)(playerTransform.rigidbody.velocity.magnitude * 3.6f);
    44.         GUI.Box(new Rect(10, Screen.height - 60, 80, 20), speed.ToString() + " km/h");
    45.  
    46.         // draw the background:
    47.         GUI.BeginGroup(new Rect(10, Screen.height - healthBarSize.y - 10, healthBarSize.x, healthBarSize.y));
    48.         GUI.DrawTexture(new Rect(0, 0, healthBarSize.x, healthBarSize.y), progressBarEmpty);
    49.  
    50.         // draw the filled-in part:
    51.         GUI.BeginGroup(new Rect(0, 0, healthBarSize.x * playerHealthManager.health / playerHealthManager.maxHealth, healthBarSize.y));
    52.         GUI.DrawTexture(new Rect(0, 0, healthBarSize.x, healthBarSize.y), progressBarFull);
    53.         GUI.EndGroup();
    54.         GUI.EndGroup();
    55.  
    56.         //Display scores
    57.         GUI.BeginGroup(new Rect(Screen.width - 150, 10, 145, 200));
    58.         GUI.Box(new Rect(0, 0, 145, 200), "Scores");
    59.  
    60.         float yOffset = 15;
    61.         float y = 10;
    62.         foreach (KeyValuePair<string, PlayerScore> score in gameHandler.scores.scoresData)
    63.         {
    64.             y = y + yOffset;
    65.             GUI.Label(new Rect(5, y, 140, 20), score.Key + ": " + score.Value.nbKill + " | " + score.Value.nbDead);
    66.         }
    67.         GUI.EndGroup();
    68.     }
    69.  
    70.     public void InGameMenu()
    71.     {
    72.         GUI.BeginGroup(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 200, 300, 400));
    73.         GUI.Box(new Rect(0, 0, 300, 400), "Game Menu");
    74.  
    75.         if (GUI.Button(new Rect(100, 85, 100, 30), "Back to Game"))
    76.         {
    77.             EnableUserInput(true);
    78.             playerTransform.SendMessage("EnableUserInput", true);
    79.             Screen.lockCursor = true;
    80.             displayInGameMenu = false;
    81.             displayHud = true;
    82.         }
    83.         if (GUI.Button(new Rect(100, 185, 100, 30), "Main Menu"))
    84.         {
    85.             Network.Disconnect();
    86.         }
    87.  
    88.         if (GUI.Button(new Rect(100, 285, 100, 30), "Quit Game"))
    89.         {
    90.             Application.Quit();
    91.         }
    92.  
    93.         GUI.EndGroup();
    94.     }
    95.  
    As you can see, I don't think that I do complicated things. If you see I big mistake, tell me ^^ But basically I only draw value in labels ! How it could be more simple than that?! :(
    The only part which could really allocate memory is maybe this one :
    Code (csharp):
    1.  
    2.  foreach (KeyValuePair<string, PlayerScore> score in gameHandler.scores.scoresData)
    3.         {
    4.             y = y + yOffset;
    5.             GUI.Label(new Rect(5, y, 140, 20), score.Key + ": " + score.Value.nbKill + " | " + score.Value.nbDead);
    6.         }
    7.  
    But in my tests, there is only one item in the Dictionnary...

    I despair...
     

    Attached Files:

  24. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Same here, had spikes and got rid of all OnGUI() functions to resolve the problem. Luckily my intend was never to use OnGUI for the game as I am using EZGUI to handle all my GUI needs. The OnGui() functions was just to do some debugging and testing.
     
  25. tomnullpointer

    tomnullpointer

    Joined:
    Sep 20, 2010
    Posts:
    142
    Im getting up to 80% hog from the garbage collector cleaning up gui stuff
    (and thats with alot of it turned off)
    IMO this really needs to be addressed or at least made clear up front that ongui isnt viable for anything performancewise.
     

    Attached Files:

    Last edited: Mar 17, 2011
  26. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
  27. Foam Sleeve

    Foam Sleeve

    Joined:
    Oct 5, 2010
    Posts:
    14
    I too have had this issue come and go to much dismay, with seemingly reasonable GUI usage. Did research and consolidated some code, but had little luck. Then I seemed to notice it was less of an issue, if at all, when I had Maya running in the background. Was wondering if it could also relate to graphics card features that are turned on/off via Maya when it boots.
     
    Last edited: Mar 23, 2011
  28. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I have this problem too... I didn't know if unity gui was the right choice but this damn well makes me choose ANYTHING else!

    Unity Technologies, please use my $4.5k to fix this problem.

    Thank you.



    grumble grumble grumble
     
  29. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
  30. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I did, with 3 votes
     
  31. Vandash

    Vandash

    Joined:
    May 12, 2011
    Posts:
    11
  32. Orange_Mako

    Orange_Mako

    Joined:
    Mar 29, 2011
    Posts:
    1
    i was having the exact same problem, i removed one print call from a OnGui() call and problem fixed i am not sure if this is the same problem every one else is having but it fixed mine
     
  33. CHPedersen

    CHPedersen

    Joined:
    Mar 2, 2011
    Posts:
    63
    I'm seeing these spikes as well and will probably move to another GUI system, but one quick question:

    Since the spikes are caused by calls to GC.Collect, can we fix them by implementing IDisposable and calling GC.SuppressFinalize to prevent the expensive calls? (Obviously, this assumes we know how to clean up our resources manually.)
     
  34. meeshoo

    meeshoo

    Joined:
    Jan 26, 2012
    Posts:
    15
    I have the same spikes too, however I think I'll be able to go around it somehow. All my menu "views" (like options, credits, instructions, main menu) are separate scenes, and are also separate from the game running scenes. So I think I could use normal OnGUI on menu scenes, as there is no problem with those, but draw the HUD and in-game menu using normal game objects. I think that should solve the issue for now.
     
  35. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Hi,

    Sorry to up this thread, but i have this issue on Unity 3.5.

    I saw the release note of Unity 4 and i see "GUI system entierly rewrited", i say "perfect, no huge spike".

    I download Unity 4, convert my 3.5 project into 4, and ... i got the same issue, and the worse thing is a second issue with GC.Collect() appear on "OnMouseEvent()" function now. I just feel depressed. My game is a musical game, the spike caused by OnGUI just destroy my gameplay.

    Since one year, is someone found a solution for this huge GC spike from OnGUI ?

    I already remove all the "Destroy" call from my code, and many of the "new" into GUI function. I really don't know what to do.
     
  36. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    From what I can tell, the only way to completely eradicate the spikes is to not have a single script using OnGUI. The spikes seem to exist even if you aren't doing anything and OnGUI is sitting there empty.
     
  37. JDMulti

    JDMulti

    Joined:
    Jan 4, 2012
    Posts:
    384
    If you got any game object in the scene having a script with OnGUI in it. Just disable the gameobject or component if it isn't used at all.
    For example, if you have a script checking for an if and continue or stops halfway trough the OnGUI to not show the interface. Make something you just don't activate that component at all. I did this by creating parts of my interface script and place them on multiple game objects and called them controllers. For example, I have a minimap script on minimapController, but if the map isn't visible at all, I just SetActive(false) the minimapController game object and it won't load anything at all. When I need to I set it active and let the rest handle by the script.

    Using events, you can make a interface close, and soon it closed, set the game object inactive. And using a button in the main interface to activate the part of the interface again. Using an event system this reduces the amount of Updates and OnGUI's being executed at all.
    It needs a bit of organizing, but it pays off when using the default OnGUI function in Unity. With events, it's really easy.
    In c# I use delegates and events to send messages between interface scripts if they show, hide or have any interaction inside it.
    Only one main controller keeps tracks of all these objects active or not.
     
  38. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    I think i'm gonna buy NGUI because the basic OnGUI just make me crazy.

    But i have a problem : On my scene i remove every OnGUI method call (i've got only one). And ... still in the inspector, the GUI.Repaint call is still here. And the spike is still here too.
    I make a deep profile on my game, and it's just crazy... on my scene, i got NO OnGUI() call, but the GUI.Repaint still doing GC Alloc up to 1.4Kb EVERY frame. What the hell...

    Do i have to remove EVERY OnGUI function on ALL my project ? Not only in the problematic scene (where i need to have a perfect framerate) ?

    Thanks
     
  39. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    I really need an answer to this question :/
     
  40. MHD-Salka

    MHD-Salka

    Joined:
    Mar 19, 2013
    Posts:
    72
    Unity 4.3....still spikes...
     
  41. Brainswitch

    Brainswitch

    Joined:
    Apr 24, 2013
    Posts:
    270
    Can it be that GUI.Repaint is being used by the Editor?
     
  42. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    Unity should really look into fixing this ASAP, you're getting a lot of bad press with the KSP people because KSP is unplayable because of this: http://forum.kerbalspaceprogram.com/threads/41900-Audio-skips-KSP-otherwise-jittery/page15

    This is one of the best games that I've played made with Unity - and there's a significant number of people who experience this garbage collection spike problem and it severely interferes with gameplay. I don't know enough about the garbage collector to know whether or not it's bad programming is KSP or whether Unity truly is to blame - but when you have a game that seems to be as popular as KSP made on your engine and it has flaws like this - it spells trouble for the devs and the engine maker.
     
    Last edited: Dec 12, 2013
  43. iamsteele

    iamsteele

    Joined:
    Mar 25, 2014
    Posts:
    29
    Wonder if any of this is being addressed in unity 4.6 due sometime this summer.. (its july already!) releasing the new GUI update. If thats the case I think I'm blessed that I've spent all my time doing everything EXCEPT For GUI work in my game. (placeholder functionality crap for now.)
     
  44. VulcanStorm98

    VulcanStorm98

    Joined:
    Apr 10, 2013
    Posts:
    1
    Ok, sorry to bump up this thread, but i'll give my experiences with GUI.

    I've encountered spikes because of OnGUI, but I believe what is causing them is the constant creation of new Rect structures, not the GUI Method itself as such.

    I used to have problems when I created a new Rect each OnGUI call. Now I just create an array of Rects at the Start and use these for positioning. Much faster now with this method.
     
  45. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Rects are structs and are allocated on the stack; hence no garbage collection. It's true that it does take a bit of time to declare a new Rect though.

    --Eric
     
  46. taimur_azhar

    taimur_azhar

    Joined:
    Mar 19, 2015
    Posts:
    7
    well its quite interesting threat, I have GC value 384B on GUI.repaint ? any clue ?
     
  47. Deleted User

    Deleted User

    Guest

    Hi Guys,

    I don't have OnGUI() anywhere in my entire solution and used Unity Text & Image and nothing else. I am using Unity version 2017.3.0f3. Any help would greatly be appreciated

    Thanks & Regards,
    Bhanu
     
  48. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    789
    If you are profiling in the editor and are seeing GUI.Repaint calls, it's likely just the inspector / other unity windows. You really need to profile a build to be sure.
     
  49. taimur_azhar

    taimur_azhar

    Joined:
    Mar 19, 2015
    Posts:
    7
    Well, How do you profile a build beside logcat ?
     
  50. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    789
    taimur_azhar likes this.