Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by zazoum · May 26, 2017 at 10:50 AM · updateperformance optimizationeventscheckdelegates

Update() VS delegate events

What is more performance efficient? Checking for something on Update() (or FixedUpdate() or LateUpdate() depending on the situation) or writing my own Events with Delegates for check?

Comment
Add comment · Show 1
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 · May 26, 2017 at 11:06 AM 2
Share

Way too unspecific. What is "something"? The only answer we could give is: "It depends". Custom events have to be raised somewhere and somehow. If the actual polling for something just happens elsewhere it just adds more complexity.

$$anonymous$$ost larger games use their own message / event system, but that's specifically designed / adapted for their use case. Also the main use for an event / message system is not to increase performance but usually to decouple classes and to abstract the idea of an event.

2 Replies

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

Answer by WinterboltGames · Jun 08, 2017 at 01:34 PM

delegates don't affect performance, they are means of controlling how/when functions are called, like this:

 private delegate void Delegate();
 Delegate RefreshInventory:
 bool finish;
     
     private void Start ()
     {
         RefreshInventory += UpdateIcons;
         RefreshInventory += ConsumeItems;
         RefreshInventory += UpdateStacks;
         RefreshInventory += CraftItems;
     }
     
     private void Update ()
     {
         if (RefreshInventory != null)
         {
         RefreshInventory 
         }
 
     if (finish)
     {
         RefreshInventory -= UpdateStacks;
     }
     }
 
 private UpdateStacks ()
 {
     finish = true;
 }

but it doesn't seem to improve performance anyway (this might not be true tho).

and you say which is best from LateUpdate (), Update() and FixedUpdate ()

well, basically

The update function is used to detect input and other stuff that needs to be done every frame. more info >>>> https://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html

the fixed update function is used to move physical objects like rigid bodies and to do more physics based stuff like moving a character controller. more info >>>> https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html

the late update function is called after all other update functions. you can use it, for example, to make a camera follow an object. more info >>>> https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html

also here's a link to a tutorial about delegates and how to use them >>> https://unity3d.com/learn/tutorials/topics/scripting/delegates

hope this helps you!

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
2

Answer by Stresscavity · Jul 07, 2017 at 02:18 AM

Delegates CAN influence performance, but it depends on what you're using them for, and are often used for their other benefits: ease of implementation, code cleanliness, etc. This is a big part of scalability as well, as adding a batch call to certain events (onClick, onDelete, etc.) make the system far more robust than polling 100 global variables that random objects can alter.

If it's something you need to check every frame, than you won't get anything out of a delegate, because it'll need to be called constantly anyways. BUT, if the alternative is calling an entire function (not just a check) every frame, when it only needs to be called on an object's change, then it is much more efficient to use a delegate.

I.E. if you use Update(), most people would probably do this:

 void Update() {
   globalObject.updatePos(newX, newY);
 }

Which is fine, but what happens when behavior gets more complicated? What if updating the position is expensive? What if in the future you add more complexity to updatePos(), making it more expensive to run? What if it's position only needs to be updated when newX or newY changes? What if there are other functions to be called on Update()? All these problems usually get fixed with the typical horrendous stack of polling variables:

 Update() {
   if (redraw) {
     if (newX != lastX || newY != lastY) {
       globalObject.updatePos(newX, newY);
       globalObject.redraw();
       globalObject.markDifference(newX, new Y);
       lastX = newX;
       lastY = newY;
     } else {
       globalObject.markSkipped();
     }
   }
 }

This is an exaggerated example, but I'm sure everyone has seen code or are guilty of writing things like this themselves. More often then not, the nested statements grow ridiculously in size, and make debugging and scaling a pain. That's where delegates, Actions, etc. become a godsent, because anytime you want to add complexity to something, you can simply determine what Action the new behavior is part of, and then add it.

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

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

Related Questions

Using Delegates for separating UI and Logic, are these in the right place? 1 Answer

Subscribers of Delegates? 2 Answers

Is "delegates" in "Update", efficient or worst 2 Answers

Unity Events - Subscribe and Unsubscribe Odd Behavor 0 Answers

MonoBehaviour Update() performance vs handling updates in a single UpdateManager script 0 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