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
3
Question by Fattie · May 08, 2013 at 02:58 PM · monobehaviormessaging

So, how the heck is "OnCollisionEnter" sent?

 function OnCollisionEnter(col:Collision)
    {
    }

Whenever there's a collision this function is called.

How is that done?

1) It's not reflection because that's slow.

2) It's not that they scan (in some way) at compile-or-launch time, because you can add/remove components at runtime and all works perfectly.

Thanks!

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
2
Best Answer

Answer by whydoidoit · May 10, 2013 at 07:25 AM

They do it in AddComponent at runtime - just cache it and add it to the list of things to be processed.

They've got a list of rigidbodies being transferred to the PhysX, they have a list of methods to be called in each of the circumstances of collision. When PhysX says OnCollisionEnter they either invoke that C# list as a multicast delegate or they run through a List and call all of the individual delegates.

When you Destroy a component its methods are removed from the list, when you Add one it's methods are appended.

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
3

Answer by CHPedersen · May 08, 2013 at 03:10 PM

Not having seen their source, it's difficult to know exactly what they do, but if I were to venture a best guess, I'd say they're probably using reflection. Summarized very briefly in this context, Reflection is a mechanism that allows you to search the contents of an assembly and look for "symbols", that is, the names of classes and their members, such as methods.

The scripts we write do not result in an executable in and of themselves, but instead compile into an assembly (a .dll file) which Unity uses to build into an executable when you ask it to build a standalone. The executable constructed by Unity (and the Editor during development) likely use Reflection to browse this dll for all instances of MonoBehavior, and then use GetMethod("methodname") on all the supported built-in methods, such as Awake, Start, Update, OnCollisionEnter, OnMouseOver, etc. etc.

See this post from StackOverflow that contains an example of what Unity's internal code might look like:

http://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters

Notice that the example acquires an object of type MethodInfo which represents information about the compiled method, such as Update or OnCollisionEnter. If this object is non-null, it means it was able to find the requested method in the class, i.e. the user (us!) wrote an implementation of the method. Then it calls MethodInfo.Invoke when appropriate, such as once per frame if it's Update, for example.

Edit:

Also notice that the search for methods using Reflection is string-based. This helps explain why it is so imperative to use the correct name for the built-in methods, that is, it will find and invoke method Update(), but not update(), and OnCollisionEnter(), but not OnColisionEnter().

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 Bunny83 · May 08, 2013 at 03:20 PM 1
Share

Well explained and probably right ;)

Unity does a lot optimisations beforehand. So they store internally what callbacks are implemented by which class and propably put the different classes in update lists based on the information gathered above.

Also keep in $$anonymous$$d that the engine itself is written in native code. $$anonymous$$ono is just a "virtual machine" inside the C++ program. From native code you could do anything to the perfect .NET world.

The best analogy would be "the $$anonymous$$atrix" where the C++ code is the real world and the .NET scripts are inside the $$anonymous$$atrix :D

avatar image CHPedersen · May 08, 2013 at 03:23 PM 1
Share

I am hopelessly lost inside The $$anonymous$$atrix.

avatar image whydoidoit · May 08, 2013 at 03:38 PM 1
Share

Actually reflection can be case insensitive - but it would be ugly as hell wouldn't it :)

I'm guessing that they aren't using Invoke and are ins$$anonymous$$d creating a known delegate (as then it's as nearly fast as calling the method natively).

avatar image whydoidoit · May 08, 2013 at 04:16 PM 1
Share

Reflection is slow to find the method, but if you convert it to a delegate then the execution of that method is very very fast. If you use .Invoke it's a dog.

avatar image whydoidoit · May 08, 2013 at 04:18 PM 1
Share

You can improve the performance of Invoking methods with a range of signatures by precaching the types: http://whydoidoit.com/2012/04/18/faster-invoke-for-reflected-property-access-and-method-invocation-with-aot-compilation/

Show more comments

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

15 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

Related Questions

[En & Fr] Show a message on the screen - Faire apparaître un message sur l’écran 1 Answer

Why can MonoBehavior.Start() return an IEnumerator? 3 Answers

Abusing NotificationCenter... 0 Answers

Access Monobehavior Instance from Static Function of Editor Script 1 Answer

Create custom constructors for certain Value types/Objects (Vector3, Transform, etc...) 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