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. [137416-starmap.png*_|137416]

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_.StarObject, starDataArray*.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().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();

//add label to the canvas
Text newLabel = Instantiate(starlabel, transform);
newLabel.GetComponent().text = text;
drawnStarLabels.Add(star, newLabel);
}
}
else
{
//destroy or reposition the label
if((distance > distanceCutoff)|| (!star.GetComponent().isVisible))
{
Destroy(label);
drawnStarLabels.Remove(star);

}
else
{
label.transform.position = position;
}
}
}

}
}

_*

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 *= Instantiate(starlabel, transform);*

labels_.text = starDataArray*.displayName;_
_labels.gameObject.SetActive( false ) ;
}
}
void Update()
{*_

//look throug the stardata objects for the gameobjects
for (int i = 0; i < starDataArray.Length; i++)
{
DrawStarLabel(starDataArray_.StarObject, labels*);
}*

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().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);
}