- Home /
Toggle Script Optimization
I would like to thank all the people who ask and answer questions on this site as I would not have been able to get anywhere near to the state of completion that I am with my first game in unity, and ironically, this is the first question I have asked. I have been looking around for script optimization techniques and anything really that can help speed up performance and playability and have had lots of success, However as I am nearing completion of this game a couple of my scripts have become quite long and I'm sure there has to be a more.. optimized way of doing this. The manager script is 596 lines of code (about 50 being ignored by the compiler), is that a normal amount for a manager or is that considered very long? My real question though is about 3 Toggle groups within that script. I managed to get the game to save the toggle state, and allow the toggle state to modify certain behaviours within the game. One looks like this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UManager : MonoBehaviour
public Toggle speed1;
public Toggle speed2;
public Toggle speed3;
public Toggle speed4;
public Toggle speed5;
private void Awake()
{
switch (Speed)
{
case 1:
speed1.isOn = true;
break;
case 2:
speed2.isOn = true;
break;
case 3:
speed3.isOn = true;
break;
case 4:
speed4.isOn = true;
break;
case 5:
speed5.isOn = true;
break;
default:
return;
}
public void SpeedToggle()
{
if (speed1.isOn == true)
{
Speed = 1;
print("Speed level 1");
//Save
}
if (speed2.isOn == true)
{
Speed = 2;
print("Speed level 2");
//Save
}
if (speed3.isOn == true)
{
Speed = 3;
print("Speed level 3");
//Save
}
if (speed4.isOn == true)
{
Speed = 4;
print("Speed level 4");
//Save
}
if (speed5.isOn == true)
{
Speed = 5;
print("Speed level 5");
//Save
}
}
EDIT: I forgot to mention that in order for this to work I had to go to the toggles, turn all of them to off, add them to the Manager object in the scene (the first 5 lines of code) and in each of the toggles OnValueChanged function, add the Manager Object and set the function to Manager.SpeedToggle (And in Unity 5)
As you can see, this can get pretty long winded, everything is working fine, I'm just wondering if there was a way to shorten this to have less of an impact on resources, the speed variable + saving are referenced from outside of this script, if that helps at all. I have done research on whether or not to use switch or if else statements and most would say that switch statements are faster, however I have noticed better performance with if statements for smaller groups. Thank you in advance to any/all that answer. (I just started turning variables private as I finish messing around with them and getting them to work with each other, that's why one is private and one is public)
Answer by Thewhiteaura · Apr 02, 2015 at 06:15 PM
well, to answer my own question, if you have issues with performance and use something like this, try taking the 64 lines of code out of awake (or start, or update), and stuff them into a user defined function, then write the function name instead. Eg.
private void Awake()
{
SpeedSwitch();
}
private void SpeedSwitch()
{
//write out all the code here
}
Your answer
Follow this Question
Related Questions
Power consumption optimization 2 Answers
How to reduce Mixamo animation size? 0 Answers
What most make the APK size big? 2 Answers
Basic question about optimization (store variable vs accessing) 1 Answer
Is it worth making tons of similiar methods or create big one that connects things together? 2 Answers