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 Thewhiteaura · Mar 27, 2015 at 05:57 AM · optimizationtogglemanageroptimizeoptimizing

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)

Comment
Add comment
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

1 Reply

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

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
 }
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

2 People are following this question.

avatar image avatar image

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


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