Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by danielstegeman99 · May 02, 2019 at 11:46 AM · uioptimization

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. alt text

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;
             }
         }
     }
     
     }
 }
     
 
 

starmap.png (48.3 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · May 02, 2019 at 11:45 AM 0
Share

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!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

243 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges