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 LunaArgenteus · Nov 14, 2013 at 10:54 PM · inspectoroptimizationefficiency

How to optimize code with one time use booleans

Hi, so I've written a class which may or may not need to have certain components re initialized or updated in the various built-in unity MonoBehaviour functions. On an object by object basis, I may want the components updated every update, only once, or just every time the object is enabled (Start, Update, and OnEnable).

I will NEVER have need to reassign what functions go where once the program starts, but it would be nice to be able to set which function my methods would get called from in the inspector. (So if there's some mystic magical way to manipulate preprocessor directives from the inspector on an object by object basis that I don't know about, please let me know!)

If I want to shuffle around where the different functions are being called, is there any way to do it in a SINGLE CLASS without a long if-else chain or switch statement existing inside Start, Update, and OnEnable?

If there's not, I'll just write a variety of wrapper classes that handle the updating in different combinations of Start, Update, and OnEnable, but I wanted to see if someone could show me a different (and cleaner) way.

Comment
Add comment · Show 3
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 ThePunisher · Nov 15, 2013 at 12:25 AM 0
Share

I'm not entirely sure what you're asking or trying to do. Can you give me an example? $$anonymous$$aybe a little bit of pseudo code?

avatar image LunaArgenteus · Nov 15, 2013 at 01:19 AM 0
Share

What I'm getting at is that I have a set of booleans and enumerations that deter$$anonymous$$e how the object should act, but all I'm doing is setting them in the inspector: once the game starts I don't need to be checking the values every update, nor do I want to.

Let's say I have an enumeration that is supposed to specify whether I should execute a function in Start, Update, or OnEnable, but only one of those. How can I accomplish this without doing this:

 void Start()
 {
     if(updatePolicy == START_ONLY)
     {
         CustomFunction();
     }
 }
 void Update()
 {
     if(updatePolicy == UDATE_ONLY)
     {
         CustomFunction();
     }
 }
 void OnEnable()
 {
     if(updatePolicy == ENABLE_ONLY)
     {
         CustomFunction();
     }
 }
 void CustomFunction()
 {
     // . . .
 }

I don't even need to define Update or OnEnable if the updatePolicy is START_ONLY. The only way I can conceive of splitting these up is to write different classes wrapper that only implement one of the functions, and letting the user select which wrapper class to apply to their object in the inspector. I'm hoping I can find some alternate solution here.

avatar image ThePunisher · Nov 15, 2013 at 10:18 PM 0
Share

I'm looking into something related to reflection at the moment. I'll let you know if it would be possible to use it for your case.

1 Reply

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

Answer by ThePunisher · Nov 15, 2013 at 11:21 PM

So after swimming through members in all the base classes of Monobehavior (through reflection). I was unable to find anything that could potentially store the definable methods like Awake, Start, OnEnable, OnDestroy, etc. This could mean that they use their own built-in SendMessage functionality to call your Awake, Start, Update, and so on, through reflection. In other words, you can't change which of those methods exist during run time (based on my knowledge).

So to answer your question, I don't think you can do what you're wanting in a clever way, although I am not 100% sure. You're best bet will probably be creating the different types of classes that have only the required methods defined.

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 LunaArgenteus · Nov 16, 2013 at 02:04 AM 0
Share

Alright, well thanks for trying. Hopefully someone else can provide some clever solution we haven't thought of, but I guess I'll just go with different wrapper classes for now.

avatar image ThePunisher · Nov 17, 2013 at 08:30 PM 0
Share

You could tell me your overall goal for this kind of framework and perhaps I can provide some suggestions or alternatives altogether.

avatar image LunaArgenteus · Nov 17, 2013 at 11:35 PM 0
Share

The overall goal was just efficiency / not needlessly checking booleans when I don't need to. For all the other situations I'm already using a series of delegates that are hooked up in the start function based on what the user specifies in the inspector. $$anonymous$$y problem only arose because this was more about frequency of update vs. what the updates do. I thought about using coroutines to specify intervals of updates, but that still doesn't really handle the OnEnable case. At this point, the best solution still seems to be my original plan: just make a couple separate wrapper classes.

If I don't get an alternative within the next couple days, I'll mark your answer as correct and be done with it.

avatar image ThePunisher · Nov 19, 2013 at 09:09 PM 0
Share

Sorry I wasn't clear, I meant the purpose of the framework. What does it do? Or what is it used for?

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

17 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

Related Questions

What static settings are allowed for moving 2D sprites? 0 Answers

How do I draw directly (and quickly) into the rendered frame? 1 Answer

Is it better/more performant to make a SerializedProperty reference or get the reference as needed from the serializedObject reference? 0 Answers

Best practice to make NavMeshAgent chase a moving target? 1 Answer

How to avoid storing the same resource for each gameobject? 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