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 john-essy · Nov 05, 2011 at 10:43 AM · performancemenupause game

Optimise Menu

When i look in the profiler and eneble my pauseGame script the fps drops from 1000+ to 30-60 now something is obviously really wrong and i think it is the amount of variables i have and considering OnGUI is run every frame. All i am asking is for some advice on to optimise it. here is the code and it is a big one! but works very well i just want as much performance as possible.

 // Static variables
 static var paused : boolean = false;
 
 var mouseOrbit;
 var dynamite : Texture2D;
 var pauseGUI : GUIStyle;
 var resumeGUI : GUIStyle;
 var optionsGUI : GUIStyle;
 var weaponsGUI : GUIStyle;
 var quitGUI : GUIStyle;
 var tipsGUI : GUIStyle;
 var pressed : boolean = false;
 var backGUI : GUIStyle;
 var dynamiteGUI : GUIStyle;
 var resume : boolean = false;
 var weapons : boolean = false;
 var quit : boolean = false;
 var tips : boolean = false;
 var options : boolean = false;
 var graphics : boolean = false;
 var sound : boolean = false;
 var controls : boolean = false;
 var stats : boolean = false;
 var electroDrillMenu : boolean = false;
 
 // Weapons GUI Goes Here
 var electroDrillBack : GUIStyle;
 var electroDrillBack2 : GUIStyle;
 var electroDrillUpgradeScreen : boolean = false;
 var electroDrillUpgradeButton : GUIStyle;
 var electroDrillBullet : GUIStyle;
 var electroDrillForce : GUIStyle;
 var electroDrillRate : GUIStyle;
 var electroDrillDamage : GUIStyle;
 var electroDrillAmmo : GUIStyle;
 var backTexture : GUITexture;
 var none : Texture2D;
 
 // Options GUI Goes Here
 var graphicsGUI : GUIStyle;
 var soundGUI : GUIStyle;
 var controlsGUI : GUIStyle;
 var statsGUI : GUIStyle;
 
 // Graphics GUI Goes here;
 var fastestGUI : GUIStyle;
 var fastGUI : GUIStyle;
 var simpleGUI : GUIStyle;
 var goodGUI : GUIStyle;
 var beautifulGUI : GUIStyle;
 var fantasticGUI : GUIStyle;
 
 // for controls menu
 var controlGUI : GUIStyle;
 
 // For menu Scripts
 var electroDrill : boolean = false;
 
 
 function Update ()
 {
     if(Input.GetKeyUp(KeyCode.Escape) || Input.GetKeyUp(KeyCode.JoystickButton7))
     {
         if(!paused)
         {
             Time.timeScale = 0;
             paused = true;
             mouseOrbit = gameObject.Find("Main Camera").GetComponent("CameraOrbitZoom").enabled = false;
             mouseOrbit = gameObject.Find("Camera").GetComponent("MouseLook").enabled = false;
         }
         else
         {
             Time.timeScale = 1;
             paused = false;
             weapons = false;
             pressed = false;
             options = false;
             controls = false;
             graphics = false;
             electroDrill = false;
             electroDrillUpgradeScreen = false;
             backTexture.enabled = false;
             mouseOrbit = gameObject.Find("Main Camera").GetComponent("CameraOrbitZoom").enabled = true;
             mouseOrbit = gameObject.Find("Camera").GetComponent("MouseLook").enabled = true;
         }
     }
     Debug.Log("Weapons is " +weapons);
     Debug.Log("electroDrill is" + electroDrill);
 }
 
 
 function OnGUI()
 {
     if(paused == true)
     {
         if(pressed == false)
         {
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 160,256,64),dynamite,resumeGUI))
             {
                 paused = false;
                 Time.timeScale = 1.0;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 100,256,64),dynamite,optionsGUI))
             {
                 options = true;
                 pressed = true;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 40,256,64),dynamite,weaponsGUI))
             {
                 weapons = true;
                 pressed = true;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 + 20,256,64),dynamite,tipsGUI))
             {
                 tips = true;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 +80,256,64),dynamite,quitGUI))
             {
                 paused = false;
                 weapons = false;
                 options = false;
                 graphics = false;
                 pressed = false;
                 Time.timeScale = 1.0;
                 Application.LoadLevel(0);
             }
         }
         
         if(weapons == true)
         {
             if(GUI.Button(Rect(Screen.width / 2 - 380 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
             {
                 electroDrill = true;
                 //paused = false;
                 weapons = false;
                 Debug.Log("We preesed the electroDrill button we should now see the electro drill");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 250 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
             {
                 
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 120 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
             {
                 
             }
             
             if(GUI.Button(Rect(Screen.width / 2 + 10 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 +140 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 +270 , Screen.height / 2 - 120 ,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             
             // this is the second row 
             if(GUI.Button(Rect(Screen.width / 2 - 380 , Screen.height / 2 +10 ,128,128),dynamite,dynamiteGUI))
             {
                 GunsAmmo.electroDrillAmmo += 30;
                 GameStats.parts -= 500;
                 Debug.Log(GameStats.parts);
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 250 , Screen.height / 2 +10 ,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 120 , Screen.height / 2 +10 ,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 +10 , Screen.height / 2 +10,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 +140 , Screen.height / 2 +10,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 +270 , Screen.height / 2 +10 ,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 120 , Screen.height / 2 +140 ,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 +10 , Screen.height / 2 +140,128,128),dynamite,dynamiteGUI))
             {
                 print("You are the best 2nd button pressed");
             }
             
             if(GUI.Button(Rect(Screen.width / 2 -390 , Screen.height / 2 +200,128,64),dynamite,backGUI))
             {
                 weapons = false;
                 pressed = false;
             }
         }
 
         if(options == true)
         {
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 160,256,64),dynamite,graphicsGUI))
             {
                 graphics = true;
                 options = false;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 100,256,64),dynamite,controlsGUI))
             {
                 controls = true;
                 options = false;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 40,256,64),dynamite,statsGUI))
             {
                 
             }
              
             if(GUI.Button(Rect(Screen.width / 2 - 390, Screen.height / 2 +200,128,64),dynamite,backGUI))
             {
                 options = false;
                 pressed = false;
             }
         }// with options 
         
         if(graphics == true)
         {
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 160,256,64),dynamite,fastestGUI))
             {
                 QualitySettings.currentLevel = QualityLevel.Fastest;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 100,256,64),dynamite,fastGUI))
             {
                 QualitySettings.currentLevel = QualityLevel.Fast;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 - 40,256,64),dynamite,simpleGUI))
             {
                 QualitySettings.currentLevel = QualityLevel.Simple;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 + 20,256,64),dynamite,goodGUI))
             {
                 QualitySettings.currentLevel = QualityLevel.Good;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 + 80,256,64),dynamite,beautifulGUI))
             {
                 QualitySettings.currentLevel = QualityLevel.Beautiful;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 128, Screen.height / 2 + 140,256,64),dynamite,fantasticGUI))
             {
                 QualitySettings.currentLevel = QualityLevel.Fantastic;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 390, Screen.height / 2 +200,128,64),dynamite,backGUI))
             {
                 graphics = false;
                 options = true;
             }
         }
         
         if(controls == true)
         {
             if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 - 150,512,512),dynamite,controlGUI))
             {
                 
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 390, Screen.height / 2 +200,128,64),dynamite,backGUI))
             {
                 controls = false;
                 options = true;
             }
         }
         
         if(electroDrill == true)
         {
             if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 - 256,512,512),none,electroDrillBack))
             {
                 electroDrillUpgradeScreen = true;
                 electroDrill = false;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 -390 , Screen.height / 2 +200,128,64),none,backGUI))
             {
                 electroDrill = false;
                 weapons = true;
             }
         }
         
         if(electroDrillUpgradeScreen == true)
         {
             if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +10,512,32),none,electroDrillBullet))
             {
                 electroDrillUpgradeScreen = true;
                 electroDrill = false;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +60,512,32),none,electroDrillRate))
             {
                 electroDrillUpgradeScreen = true;
                 electroDrill = false;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +110,512,32),none,electroDrillForce))
             {
                 electroDrillUpgradeScreen = true;
                 electroDrill = false;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +160,512,32),none,electroDrillAmmo))
             {
                 electroDrillUpgradeScreen = true;
                 electroDrill = false;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 - 256, Screen.height / 2 +210,512,32),none,electroDrillDamage))
             {
                 electroDrillUpgradeScreen = true;
                 electroDrill = false;
             }
             
             if(GUI.Button(Rect(Screen.width / 2 -390 , Screen.height / 2 +200,128,64),none,backGUI))
             {
                 electroDrill = true;
                 backTexture.enabled = false;
                 electroDrillUpgradeScreen = false;
             }
             
             backTexture.enabled = true;
             
             if(electroDrillUpgradeScreen == false)
             {
                 backTexture.enabled = false;
             }
         }
     }
 }
Comment
Add comment · Show 5
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 Tseng · Nov 06, 2011 at 05:05 AM 0
Share

For sake of god, don't post half of your project and reduce it to the $$anonymous$$imum. No one is going to read this whole piece of code, it's badly formated and several pages long...

avatar image Bunny83 · Nov 06, 2011 at 05:14 AM 0
Share

@Tseng: I totally agree!
@johnessy012: And why do you have an empty line after each line of code? Is it really that hard to format the code properly?

  1. Insert your code (without millions of empty lines).

  2. Select the whole text-block which is code.

  3. Press the "101010" button.

avatar image john-essy · Nov 06, 2011 at 10:26 AM 0
Share

@Bunny83 I did use the 101010 button but for some reason it adds these emtpy lines, my code is nothing like this i thought it was unity answers that does this but if not does someone have a solution to why this is happening. it happens everytime

avatar image Bunny83 · Nov 06, 2011 at 12:40 PM 0
Share

I've reformatted your code and removed empty and commented lines which are absolutely unnecessary / irrelevant for your question. After formatting the script i inserted the text, selected it and pressed the button. I've written hundreds af answers and it never added empty lines in between. I've seen those empty lines a couple of times so it woulc be interesting what code editor and browser you use.

The 101010 button actually just adds a single empty line in front and after the selected textblock (needed to seperate it from other text) and indent every line within this block by 4.

avatar image john-essy · Nov 06, 2011 at 07:06 PM 0
Share

@ Bunny83 Thanks i am not just saying this but that is how my code looks and the browser i was using was chrome i think i will stop with chrome. Also when i removed the Debug's the fps increased by at least 10 so removed all prints and everything and now the code is so much more optimised and runs extremly well. But if any of you have an advice then please share as i would love to know

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Tseng · Nov 06, 2011 at 05:12 AM

Well, I did ONLY a quick view of your code.

You have commited some of the capital sin of programming in your code

  1. Never use GameObject.Find inside of Update! Same goes for GetComponent. Cache it in a variable, and access the variable instead. .Find/.GetComponent are SLOW

  2. Similar to rule one: NEVER call Debug.Log on each Update/FixedUpdate! It's HORRIBLY SLOW, even if it only appears in debug builds & Editor, it will have even bigger impact with Profiler enabled.

  3. Unity GUI in general is pretty slow, especially on mobile phones. Each GUI object will cause a draw call, and having more than 50 draw calls on a smartphone lowers the performance drastically. On PC it's not that worse (1000-1500 draw calls or more are no problem with modern PCs).

Comment
Add comment · Show 7 · 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 john-essy · Nov 06, 2011 at 10:40 AM 0
Share

@Tseng

I have tried this but it does not seem to work for me (Again you are the pro and i am confident i am doing it wrong) Example

var player;

function Start()

{

player = GameObject.Find("$$anonymous$$adDoc").GetComponent.renderer.enabled = false;

}

function Update() { if(Input.GetButtonDown(Fire1))

   {
           player.renderer.enabled = true;
   }

}

But this wont work (I dont need this to work just an example) how would i do this correctly? EDIT: This is an example not what i usually do all i was asking is how would you inistialize the variable then call it by the variable name like i did in the Input

avatar image gfr · Nov 06, 2011 at 12:35 PM 1
Share

Regarding 1.: The GameObject.Find() etc. are only called on keypresses, not every frame and thus shouldn't be the problem.

avatar image Tseng · Nov 06, 2011 at 07:21 PM 0
Share

In your example you're getting the component and assing the value to it. If you assign it to player, you won't "assign" the Component, but the true or false value.

You don't get an error, because you're not using UnityScript with #pragma strict. After you do

 player = GameObject.Find("$$anonymous$$adDoc").GetComponent.renderer.enabled = false;

player will have the value "false" and NOT a reference to the Renderer game component.

You have to either do is first get the reference to the component, then set it.

 player = GameObject.Find("$$anonymous$$adDoc").GetComponent(Renderer);
 player.enable = false;

You can't do both in the same line! And learn to use #pragma strict to prevent such newbie errors.

avatar image john-essy · Nov 06, 2011 at 07:34 PM 0
Share

Thanks Tseng will read up on pragma

avatar image Bunny83 · Nov 10, 2011 at 12:11 PM 0
Share

@Tseng: Just for the record, i don't think pragma strict would complain here. It's legit to assign a value to multiple objects. Of course in this case it doesn't make any sense to assign false to player but it can be done in one line ;)

For example if you want to store the renderer in a variable and enable it you can do:

 var myRenderer : Renderer;
 (myRenderer = GameObject.Find("$$anonymous$$adDoc").GetComponent(Renderer)).enabled = true;

assignments are processed from right to left but with brackets you can control the order. The result of an assignment is the assigned value, in this example the Renderer component.

I wouldn'T recommend such code because it's hard to read / understand. Always try to write easy-to-read code.

Show more comments
avatar image
2

Answer by Extrakun · Nov 10, 2011 at 02:26 PM

You should consider caching the various numbers used in your calculation in variables. For example, you are working a lot with height/2 and width/2. You can store those in a variable to avoid re-calculating those values.

You can also use OnGUI to display debug messages, instead of using Debug.Log.

As a way to improve the organization, you are using one big GUI script to handle all your screens. Rather, you can split those screens into three different gameobject and only enable the one which is active.

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
avatar image
1

Answer by Bunny83 · Nov 06, 2011 at 05:18 AM

Without reading through this mess i can almost for certain tell you that your Debug.Log's will cause this fps-drop. Debug.Log() (or print()) is THE fps killer inside the editor. In builds it doesn't seem to drop that much since the text isn't displayed but still written to the logfile.

Comment
Add comment · Show 2 · 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 Bunny83 · Nov 06, 2011 at 12:45 PM 0
Share

No, he has two unconditional Debug.Log's in Update at the end....

avatar image gfr · Nov 06, 2011 at 12:47 PM 1
Share

Right, consider me blind ;)

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

6 People are following this question.

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

Related Questions

Unity 4.6 UI Lags on android 0 Answers

how to pause the mouselook script during pause? 1 Answer

Pause Menu Buttons Not Working on First Attempt 2 Answers

Pausing the game after player object gets destroyed 1 Answer

GUI Menu Major Performance Issues 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