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): public void DrawItemESP() { if (_lootItemsToDraw == null) return; foreach (LootItem lootItem in _lootItemsToDraw) { if (lootItem == null) continue; float distance = Vector3.Distance(Camera.main.transform.position, lootItem.transform.position); Vector3 boundingVector = Camera.main.WorldToScreenPoint(lootItem.transform.position); if (boundingVector.z > 0.01 && distance <= Settings.DrawLootItemsDistance) { string text = $"{lootItem.name} - [{(int)distance}]m"; GUI.color = Color.yellow; GUI.Label(new Rect(boundingVector.x - 50f, Screen.height - boundingVector.y, 100f, 50f), text); } } 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?
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.
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.
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?
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)
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.