- Home /
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;
}
}
}
}
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...
@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
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.
@ 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
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
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
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.
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).
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
Regarding 1.: The GameObject.Find()
etc. are only called on keypresses, not every frame and thus shouldn't be the problem.
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.
@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.
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.
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.
No, he has two unconditional Debug.Log's in Update at the end....