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
2
Question by DannyB · Sep 29, 2012 at 12:07 PM · performancereflection

C# Invoke vs. Unity SendMessage

Hello everybody,

I have a need in my code to call a method by string every frame. Right now I am using C# Invoke, like this:

 Type thisType = handler.GetType();
 MethodInfo theMethod = thisType.GetMethod(funcname);
 if( theMethod != null ) 
     theMethod.Invoke(handler, new object[]{});


Now, I know it has some performance penalty, but I am wondering if it is a significant one to try and solve.

I know I can improve performance by caching it to a delegate, but my main question here is this:

Considering this needs to be done every frame - what would be faster; C# Invoke or Unity SendMessage?

Thanks in advance.

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

2 Replies

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

Answer by whydoidoit · Sep 29, 2012 at 12:27 PM

Unity SendMessage is also using reflection - so it will be as slow as your Invoke code, if not slower if it does some other processing. However, hopefully Unity will cache the delegates for methods it's called in some kind of O(1) storage. I wouldn't trust that though and never SendMessage unless it is a rare event (when it's damn handy along with Broadcast and SendUpwards).

You are much better off caching that in an Action (or Action<parameter1, ... >) if you call every frame - now that is very fast.

When you don;t know ahead of time all of the delegates then you can use this code I wrote to cache the signatures if you are running on an AOT environment like iOS.

Comment
Add comment · Show 16 · 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 shaderop · Sep 29, 2012 at 02:30 PM 0
Share

Very informative. Thanks for sharing :)

avatar image DannyB · Sep 29, 2012 at 02:35 PM 0
Share

Thanks. I will try to replace Invoke with a more direct call.

avatar image DannyB · Sep 29, 2012 at 06:16 PM 0
Share

Well - I ended up caching the action ins$$anonymous$$d of invoking on each frame. So the reflection overhead happens only on first call to an unknown function, but later it is cached as an Action. Thanks, accepted.

EDIT: But by the way, I do not see any significant performance. Some improvement, yes, but nothing major - at least not within the editor. $$anonymous$$aybe on other systems it will be more meaningful.

avatar image Fattie · Sep 29, 2012 at 07:35 PM 1
Share

".. and never Send$$anonymous$$essage.." You know a somewhat related problem that gripes me. A collider (on a Rock, say) bashes you, you have to get some info from the script attached to the Rock, so you have to ................. GetComponent. Arrrg! All these GetComponents every time there's a little scoring to do. That annoys the hell out of me and I've never seen a way around it.

avatar image DannyB · Sep 29, 2012 at 07:39 PM 0
Share

@Fattie - I agree. When I started with Unity this confused me a lot. I always tried to call OtherObject.Some$$anonymous$$ethodInAttachedScript() banging my head against the table trying to figure out why its not working. I also try to avoid these ugly GetComponent where I can.

Show more comments
avatar image
0

Answer by WhendricSo · Nov 11, 2018 at 08:51 PM

SendMessage is only about four times slower than using a delegate. In most cases, the performance penalty is negligible.

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

13 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

Related Questions

Is there any way to cut down the performance costs on reflective and refractive planes? 1 Answer

Regarding Update() Performance 0 Answers

IL2CPP compiled expressions performance 2 Answers

Which is better??? 0 Answers

Pro-water Shader causing large jump in draw calls 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