Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Azmodii · Sep 12, 2013 at 02:15 PM · c#guiperformancethreads

WorldToScreenPoint in Update/OnGUI: Bad Performance

Hey all,

I have a simple script which takes the position of the current game object and overlays a 2D texture in its Screen point position.

The issue I am having is it is chewing the main thread! I'm seeing a normal thread time of 12.1ms without the WorldToScreenPoint. With it, it can jump as high as 127ms, but idles around 78ms.

Any suggestion for an alternative?

My Code:

This can be in either Update () or OnGUI () - But OnGUI () results in much worse performance (as expected).

 screenPos = Camera.main.WorldToScreenPoint(transform.position);
 screenPos.y = Screen.height - screenPos.y;
 screenRect = new Rect
             (
             screenPos.x - (prefabManager.planetGUIWindowTexture.width / 2) - (Camera.main.transform.position.y),
             screenPos.y - (Camera.main.transform.position.y), 
             prefabManager.planetGUIWindowTexture.width, 
             prefabManager.planetGUIWindowTexture.height
             );


Here is the code creating the texture:

 void OnGUI () {
         GUI.DrawTexture(screenRect, prefabManager.planetGUIWindowTexture);        
 }
Comment
Add comment · Show 6
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 roojerry · Sep 12, 2013 at 02:19 PM 0
Share

Try updating only when the object is actually seen by the camera and the objects position has changed or the camera has moved.

avatar image Azmodii · Sep 12, 2013 at 02:25 PM 0
Share

I have tried that by using IsVisibleFrom but the performance hit is actually less than running that particular script. I tagged all the "planets", grabbed all the objects with that tag and ran them through this script.

Is there a better way of doing this?

BTW, my CPU is an i5 3770k, so I dont think CPU is really the issue (just in case that was next :P)

EDIT: I should note, that I cached the list, and then checked if the planet was a member before drawing the texture (not loaded a list for every planet this was attached to lol)

avatar image roojerry · Sep 12, 2013 at 02:46 PM 0
Share

Did you try only updating if something moved? Unless all your objects or camera are moving every frame, you won't need to be recalculating your screenRect every frame.

avatar image Azmodii · Sep 12, 2013 at 02:55 PM 0
Share

I've updated my code in Update () to:

 if (child.renderer.IsVisibleFrom(Camera.main)) {
             screenPos = Camera.main.WorldToScreenPoint(transform.position);
             screenPos.y = Screen.height - screenPos.y;
             screenRect = new Rect
                 (
                 screenPos.x,
                 screenPos.y, 
                 prefab$$anonymous$$anager.planetGUIWindowTexture.width, 
                 prefab$$anonymous$$anager.planetGUIWindowTexture.height
                 );
         }

And in OnGUI:

 void OnGUI () {
         if (child.renderer.IsVisibleFrom(Camera.main)) {
             GUI.Label (screenRect, "TEst");
         }
 }

Still adding at least 10ms to the thread even when no planets are being rendered. Heres a screeny of profiler.

alt text

screen shot 2013-09-13 at 12.48.49 am.png (409.7 kB)
avatar image roojerry · Sep 12, 2013 at 03:06 PM 0
Share

Those are not cheap functions that you have added for checking if on screen. Again, you will not want to be calling that each frame (especially not in both Update and OnGUI on every object).

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Azmodii · Sep 12, 2013 at 03:45 PM

I managed to solve this.

 isVisible = child.renderer.IsVisibleFrom(Camera.main);

         if (isVisible) {
         screenPos = Camera.main.WorldToScreenPoint(transform.position);
         screenPos.y = Screen.height - screenPos.y;
         screenRect = new Rect
             (
             screenPos.x,
             screenPos.y, 
             prefabManager.planetGUIWindowTexture.width, 
             prefabManager.planetGUIWindowTexture.height
             );
     }
 }

Storing the result of IsVisibleFrom in a cached variable solved the extra CPU time. Seems the functions were executing asynchronously and causing leaks.

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

18 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

Related Questions

Erroe when resizing GUITextures in C# file 1 Answer

Is there any easy way to keep buttons inside of box? (c#) 1 Answer

Create a Button Scrollview 0 Answers

C# GUI Destroy or Disable? 1 Answer

Draw more EditorGUILayout components? 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