Optimizing dynamic ui text
I am creating a starmap, so obviously i want to have the names of stars next to the stars as shown below.
The problem is that even when drawing as little as 20 names, my performance crawls to a halt.(from ~80 fps to ~ 7 fps) .
Every Update(), the code loops through an array of all the objects containing the stardata and the corresponding gameobject. Then it instantiates a Text object in the canvas if it doesn't exist yet and is within a certain distance of the camera object. If it does exist, it updates the position based on the stars position, or destroys the text object if the star is not visible or outside the cutoff distance. Is there anything obviously resource intensive in my code or am I approaching it wrong entirely?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InterfaceController : MonoBehaviour
{
public Maincontroller mainController;
public StarData SelectedStar;
public float distanceScaling = 1.0f;
public int distanceCutoff = 50;
public Text starlabel;
public GameObject cameraFocus;
private Dictionary<GameObject, Text> drawnStarLabels = new Dictionary<GameObject, Text>();
public int starLabelOffsetX = 0;
private StarData[] starDataArray;
void Update()
{
//look throug the stardata objects for the gameobjects
for (int i = 0; i < starDataArray.Length; i++)
{
DrawStarLabel(starDataArray[i].StarObject, starDataArray[i].displayName);
}
Debug.Log("stars labeled: " + drawnStarLabels.Count);
}
private void DrawStarLabel(GameObject star, string text)
{
//determine distance between the camera focus point and the star
float distance = Vector3.Distance(cameraFocus.transform.position, star.transform.position);
//determine the position of the textlabel on the screen
Vector3 position = Camera.main.WorldToScreenPoint(star.transform.position) + new Vector3(starLabelOffsetX,0,0);
if (!drawnStarLabels.TryGetValue(star, out Text label))
{
if (distance < distanceCutoff && star.GetComponent<Renderer>().isVisible)
{
//Vector2 size = GUI.skin.label.CalcSize(new GUIContent(text)) * distanceScaling * distance;
//GUI.Label(new Rect(position.x, Screen.height - position.y, size.x, size.y), text);
//Text label = gameObject.AddComponent<Text>();
//add label to the canvas
Text newLabel = Instantiate(starlabel, transform);
newLabel.GetComponent<Text>().text = text;
drawnStarLabels.Add(star, newLabel);
}
}
else
{
//destroy or reposition the label
if((distance > distanceCutoff)|| (!star.GetComponent<Renderer>().isVisible))
{
Destroy(label);
drawnStarLabels.Remove(star);
}
else
{
label.transform.position = position;
}
}
}
}
}
Hello.
You can use a Corutonine (Iennumerator) that executes every X seconds, and not use the update that is execued several times every second.
This can increase sooooo much your performance.
Bye!
Answer by Hellium · May 02, 2019 at 01:21 PM
I see several issues with your code indeed.
The first and worst one, you are instantiating and destroying objects continuously... Instead, create once and enable/disable as needed.
Then, you are doing unecessary computations, and calling Camera.main
multiples times per frame instead of caching it once.
I've reworked your code below. It's just a very first step for further optimisation, but I guess it's a good start:
public Maincontroller mainController;
public StarData SelectedStar;
public float distanceScaling = 1.0f;
public int distanceCutoff = 50;
public Text starlabel;
public GameObject cameraFocus;
public int starLabelOffsetX = 0;
private StarData[] starDataArray;
private Text[] labels;
private Camera camera;
void Start()
{
camera = Camera.main;
if(camera == null)
camera = FindObjectOfType<Camera>();
labels = new Text[starDataArray.Length];
for (int i = 0; i < starDataArray.Length; i++)
{
labels[i] = Instantiate(starlabel, transform);
labels[i].text = starDataArray[i].displayName;
labels[i].gameObject.SetActive( false ) ;
}
}
void Update()
{
//look throug the stardata objects for the gameobjects
for (int i = 0; i < starDataArray.Length; i++)
{
DrawStarLabel(starDataArray[i].StarObject, labels[i]);
}
Debug.Log("stars labeled: " + drawnStarLabels.Count);
}
private void DrawStarLabel(GameObject star, Text label)
{
//determine distance between the camera focus point and the star
float sqrDistance = (cameraFocus.transform.position - star.transform.position).sqrMagnitude;
// determine if visible or not
// Adding a reference to the Renderer into the StarData class could save more processing time
bool visible = sqrDistance < distanceCutoff * distanceCutoff && star.GetComponent<Renderer>().isVisible;
label.gameObject.SetActive( visible );
//determine the position of the textlabel on the screen
if( visible )
label.transform.position = camera.WorldToScreenPoint(star.transform.position) + new Vector3(starLabelOffsetX,0,0);
}
Your answer
Follow this Question
Related Questions
GUI/Text Asset for Unity? 0 Answers
ScrollRect.LateUpdate Performance Spike Optimization Help 0 Answers
UI performance. FPS drops. Overdraw. 0 Answers
Separating UI from data 1 Answer
uGUI Text draw calls 1 Answer