Search Unity

Help optimizing script.

Discussion in 'Scripting' started by JoakimCarlsson, Mar 4, 2020.

  1. JoakimCarlsson

    JoakimCarlsson

    Joined:
    Aug 27, 2014
    Posts:
    65
    I'm working on a offline survival game.
    And I want too make a cheat trainer in the game. So I tried too make some kind of wallhack for my items that spawns in. And in order to do this I use the following code.
    Code (CSharp):
    1. public void DrawItemESP()
    2.         {
    3.             if (_lootItemsToDraw == null)
    4.                 return;
    5.  
    6.             foreach (LootItem lootItem in _lootItemsToDraw)
    7.             {
    8.                 if (lootItem == null)
    9.                     continue;
    10.  
    11.                 float distance = Vector3.Distance(Camera.main.transform.position, lootItem.transform.position);
    12.                 Vector3 boundingVector = Camera.main.WorldToScreenPoint(lootItem.transform.position);
    13.  
    14.                 if (boundingVector.z > 0.01 && distance <= Settings.DrawLootItemsDistance)
    15.                 {
    16.                     string text = $"{lootItem.name} - [{(int)distance}]m";
    17.  
    18.                     GUI.color = Color.yellow;
    19.                     GUI.Label(new Rect(boundingVector.x - 50f, Screen.height - boundingVector.y, 100f, 50f), text);
    20.                 }
    21.             }
    I call that method in my OnGUI()


    I then attach the script too an empty Game Object. How ever when I run the code I get some performance issues. Even if I would just loop over 1 item I still get 10-15 fps decrease.

    Can I somehow optimize this or a some guidance for another approach?
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    post screenshot of game setting
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    You sould post the entire code. But if you call DrawItemESP() each time in Update, then that's where you should start. A single change of a single item on the canvas will cascade into a full redraw. Only Change items if and when they Change.
     
  4. JoakimCarlsson

    JoakimCarlsson

    Joined:
    Aug 27, 2014
    Posts:
    65
    I'm calling it in OnGUI since I can't use GUI labels outside it if I understand correctly.

    And I'm drawing the distance too the object so it changes all the time.
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    OnGUI is called every Frame. You may want to re-think your design and use a Canvas instead
     
    Joe-Censored likes this.
  6. JoakimCarlsson

    JoakimCarlsson

    Joined:
    Aug 27, 2014
    Posts:
    65
    I'll have too look up canvas cheers! But since I sitll need too render text too the screen won't I still need too use OnGUI?
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
    check with Deep Profiler.
    also Camera.main calls FindGameObjectsWithTag(), so do that in start only.

    but yeah, using the new UI system should be better. (can use text, images, etc)
     
    Joe-Censored likes this.
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    No. The Canvas-based UI is a complete replacement for OnGUI. OnGUI is old and deprecated. In fact, the biggest reason it was deprecated is because it was too slow. ANYTHING in your project that uses OnGUI would be better off being replaced by the Canvas system.
     
    Ryiah and Joe-Censored like this.
  9. JoakimCarlsson

    JoakimCarlsson

    Joined:
    Aug 27, 2014
    Posts:
    65
    Cheers for all the replys. I got it working now with Canvas.