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 ExtremePowers · Jan 26, 2015 at 04:39 PM · optimizationupdateprofiler

2.5ms update

I have a update function which takes up 2.5ms (10-20%), the function looks like this, and should require that much to run. Why does it require that much to do such simple update function?

 function Update() {
     if (Network.isServer || Network.isClient) {
         var cam = GameObject.Find("Menu Camera");
         if (cam) {
             Destroy(cam);
         }
         if (Input.GetKeyDown(KeyCode.F1)) {
             ShowInfo = !ShowInfo;
             Game.SetMouse(ShowInfo);
         }
         if (Input.GetKeyDown(KeyCode.Escape)) {
             ShowPauseMenu = !ShowPauseMenu;
             ShowPMenu = ShowPauseMenu;
             UpdateMouse = true;
             Game.SetMouse(ShowPauseMenu);
             if (!ShowPauseMenu) {
                 ShowGraphicsMenu = false;
                 ShowAudioMenu = false;
                 ShowControlsMenu = false;
                 ShowOptionsMenu = false;
                 ShowGameplay = false;
                 ShowCredits = false;
             }
         }
         if (Game.player == null && !allowQuit) {
             //This function is only called 1 time each respawn.
             if (!ShowLoading) {
                 ShowRespawnScreen = true;
             }
             if (Game.gameobject.GetComponent(ProceduralTerrain).GeneratedTiles == 1) {
                 Game.player = SP(SpawnPos,Respawn);
                 AstarPath.active.astarData.gridGraph.center = Vector3(Game.player.transform.position.x,Game.gameobject.GetComponent(ProceduralTerrain).terrainHeight,Game.player.transform.position.z);
                 AstarPath.active.astarData.gridGraph.GenerateMatrix();
                 AstarPath.active.Scan();
                 ShowLoading = false;
             }
         }
     }
 }
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 meat5000 ♦ · Jan 26, 2015 at 04:46 PM 0
Share

Thread time of 2.5ms equates to 400fps...

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DaDonik · Jan 26, 2015 at 04:42 PM

You do that every Update O.o

 GameObject.Find("Menu Camera");

This is your problem right here...

Comment
Add comment · Show 4 · 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
avatar image ExtremePowers · Jan 26, 2015 at 04:43 PM 0
Share

So, moving that line to my start function would fix it?

avatar image DaDonik · Jan 26, 2015 at 04:45 PM 0
Share

Yeah, caching the camera is the way to go, just like meat5000 says.

avatar image tanoshimi · Jan 26, 2015 at 04:57 PM 0
Share

While you're at it, don't do this every frame either:

 Game.gameobject.GetComponent(ProceduralTerrain)

And what are the following functions doing? Are you recalculating an A* path every frame?

 AstarPath.active.astarData.gridGraph.Generate$$anonymous$$atrix();
 AstarPath.active.Scan();
avatar image ExtremePowers · Jan 26, 2015 at 04:59 PM 0
Share
  AstarPath.active.astarData.gridGraph.Generate$$anonymous$$atrix();
  AstarPath.active.Scan();

As the comment says, they are only called once when the player respawns (The scene doesn't reload) Since I would have to recreate the Terrain again then.

avatar image
2

Answer by meat5000 · Jan 26, 2015 at 04:42 PM

 var cam = GameObject.Find("Menu Camera");

Dont do this in Update. Find it once before update begins and store it.

I see you find it to destroy it. Once its destroyed you still keep looking for it. This will take longer than actually finding it! Make a one-shot bool to switch this off once it has been performed.

I'd do it this way assuming that this step needs to be performed where you've placed it. Start may not cut it.

 var myFlag : boolean = false;
 
 function Update()
 {
    if(!myFlag)
    {
        Find Cam
        Destroy Cam
        myFlag = true
    }
 }

You get the idea.

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

21 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

Related Questions

EventSystem.Update() takes up 90% of CPU - Profiler Data Help 0 Answers

Unity profiler Gfx.WaitForPresentOnGfxThread 2 Answers

Andriod (Oculus Quest 2 app) locked at 30 fps 1 Answer

'function Update' vs 'function Start {while(true)}'? 3 Answers

Physics.Simulate taking a long time 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