Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnUpdate()/OnGUI() hampering my framerate

Discussion in 'Scripting' started by Cratthorax, Jan 7, 2021.

  1. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Good morning,

    using this code:

    Code (csharp):
    1. void OnGUI(){
    2.        
    3.         //detetect object under mouse pointer code. KEEP IT HERE!!!
    4.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.         if (Physics.Raycast (ray, out hit) == this.GetComponent<system> ()) {
    6.             //main.Log (hit.collider.name.ToString());
    7.             GUI.skin = systemUI;
    8.             if (hit.collider != null && this.GetComponent<system> () != null && hit.collider == this.GetComponent<Collider> ()) {
    9.                 GUILayout.BeginArea (new Rect (Input.mousePosition.x + 5, main.cam.pixelHeight - Input.mousePosition.y - 20, 200, 75));
    10.                 if (main.currentMode != 7) {
    11.                     GUILayout.Box ("<b>1st Name</b>: " + this.systemName.ToString () + "<b>\n2nd Name</b>: " + this.secondName.ToString (), main.nameStyle);
    12.                 } else if (main.currentMode == 7) {
    13.                     if (this.connectedLines > this.suggestedLines) {
    14.                         GUILayout.Label ("<b>Name</b>: " + this.systemName + " " + ("\n<color=red><b>Hyperlanes</b>:" + this.connectedLines + "</color>" + "/" + this.suggestedLines +
    15.                             "<b>\n2nd Name</b>: " + this.secondName.ToString ()), main.nameStyle);
    16.                     } else {
    17.                         GUILayout.Label ("<b>Name</b>: " + this.systemName + " " + ("\n<b>Hyperlanes</b>:" + this.connectedLines + "" + "/" + this.suggestedLines +
    18.                             "<b>\n2nd Name</b>: " + this.secondName.ToString ()), main.nameStyle);
    19.                     }
    20.                 }
    21.                 GUILayout.EndArea ();
    22.             }
    23.         } else {
    24.             return;
    25.         }
    26.     }

    I'm getting huge negative impact on my framerate, if I go beyond 600-700 gameobjects utilizing this OnGUI() block. I was trying to use a corountine, InvokeRepeat, LateUpdate, and FixedUpdate to get better performance, but unfortunately I can use this only from inside OnGUI(). Isn't there another way to create less taxation?

    I get it, 600 objects are lot for the engine to handle, even if they are low poly/low textured.

    Utilizing the stated methods for the Update() block, renders some of the functions broken. So what I really need is a "controlable" update interval, that exactly let's me control the taxation for every individual function.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Bad idea to do the raycasting here. This means you will do one raycast for every object with this script. Instead, do a single raycast and then do the GUI operation in question for the single object you hit.

    Also the way you are checking if this object was the one hit is not correct.
     
    Joe-Censored and Bunny83 like this.
  3. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Thanks for the replay.

    I've removed the Raycast from the system script entirely, and call for it only from inside my main script. I also added tons of conditions to the scripts, so most of the visual FX elements are FOV based, and turn themself off when zooming far out. However, I only got slight improvement by about 4-5 frames.

    This is really giving me headache, since I checked on my last released version, and there was no performance hit whatsoever, AND I was already using most of the elements I'm using right now.

    I can't trace the bottleneck.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  5. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Free version. But the framerate counter speaks clear words.
     
  6. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,160
    The Profiler isn't a Pro-version only tool. Not since a veeery long time anymore at least.
     
    mopthrow and Bunny83 like this.
  7. payalzariya07

    payalzariya07

    Joined:
    Oct 5, 2018
    Posts:
    85
    I understand. The Update function is called even for empty object, and there's some overhead for marashaling from Unity internal to mono C#. You are doing something a little similar to this

    https://github.com/sschmid/Entitas-CSharp

    There's a lot more things in the system to do, like order of updates, threading etc. If it is just simple looping, there's a lot of improve.
     
  8. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    I'm supposed not to say that, but I'm on version 4.7.1. I know there's no support. Still some users were generous enough to give help. I promise, I will upgrade soonish.

    I think I figured the problem. First and foremost: I did not pay a lot attention to performance before, since I'm working on an editor for a game. Not a game per se. So performance was neglectable. However, the newest update I'm readying up has a ****load of performance hampering new FX elements, one in particular I've created from this Tutorial.

    So take into account using a particle effect with up to 500 particles per object and frame, on up to 1000 spheric objects. And of those particle systems I was using two per (Sun)object, which equals something like 500x2x1000=1000000 per frame. Ontop of that I'm using self illuminating diffuse texture for all of the suns, and all of its multi system (Sun)components. So there was a self illuminating texture thingy going on, for up to 5000 spheric objects at once.

    You think it ends here? Wrong! I didn't mention the GUI thing going on I've posted code of upside, and as final "cream" ontop of a raw cut diamond, I've added lights for all objects with halos. Someone screaming overkill? Sure. But I'm a layman on a badass computer, how I'm supposed to know what the engine can take, as it took anything from any game before?

    I improved performance greatly by adding various conditions, that will recognize if the objects are in sight of the main camera(something Unity is supposed to do with culling/occlusion?), apply FX only with low FOV aspects, and rendering basic FX when zooming far out etc. I will spend today to figure new enhancing methods. One in particular over here did not produce any results whatsoever. That's when I started thinking:"Maybe you've gone to far". Because something of that tutorial was supposed to show effect, right?

    So, I'll stay with hope those performance issues are obsolete when updating to latest Unity. I will probably have a hard time converting, which has me thinking it's probably more easy, to just start all over again. There's not an awful lot of objects in the hierarchy and assets I'm using, and the scipt parts are manageable. If any script issues happen due to outdated code, I can fix it on the fly, one at a time. Something I already figured wouldn't work easily, when just trying to force a conversion to latest Unity.

    However, there's one last thing I can't get out of my head: At one point I was disabling any of the newly imported FX elements. I was returning to the most basic textures. No fancy light, no shadows(completely disabled for all elements), and yet the engine refused to return to its old glory from my last version. I also tripple checked on the new code snippets, also trying to disable them in Update(), OnGUI(). Nothing, absolutely nothing worked, even when I was deleting everything in a backed up project. So there must have gone something completely wrong I can't see.

    I remember having similar issues when I was starting the project, and imported asset packages. I had to completely uninstall and reinstall the whole of Unity to fix it the last time. But it didn't do anything this time. One thing I did not try was returning to use Input.GetKeyDown(Keycode.blabla), instead of using Input.GetButtonDown("blabla"). I wasn't using the default key setup menu before, but reimported it for the newest version. Could this be responsible, and if so why?

    So for a last shot in the blue. What exactly happens if I'm using PlayerRef to save data. Is there a limit of data to save to the PlayerRef. Should I use it at all? It's siting inside the registry, right?

    Edit: added a screenshot to show of even on most basic FX, the editor performs at 8-9fps.


     
    Last edited: Jan 9, 2021
  9. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Also, why your forum keeps telling me I'd post spam, if I'm trying to wrap the image in spoilers, which actually qualifies for the opposite of spam???