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
1
Question by thormond · Nov 13, 2013 at 09:35 AM · nguionclickunity 4.3

Custom events in MonoBehaviour

As many of you know there are custom events like OnClick, OnDrag, OnPress, etc. in NGUI toolkit (Example).

My question is: how do you add your own custom events to Unity's MonoBehaviour just like ArenMook did. With the introduction of the Unity 4.3 I'm missing mouse events and I'd like to write my own small extention using raycasts and new 2d colliders but I don't want to use C#'s way of making events since NGUI's solution is way more convenient.

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

1 Reply

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

Answer by Jesdisciple · Nov 24, 2013 at 06:17 AM

NGUI seems to use Unity's built-in SendMessage feature to emulate how Unity calls methods with agreed-upon names (even if they're private): http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html

Note that SendMessage uses a feature in C# (and other languages) known as reflection. This feature allows a program to examine its own structure at runtime - types used, their members, their names, etc. It offers a runtime model of your source code.

Reflection, like most language features that involve "runtime" in their explanation, is often used where it is not needed and usually less efficient than the alternatives. I suggest learning to use C# delegates (such as events) rather than adding more reflection to what Unity already has built into it.

Comment
Add comment · Show 2 · 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 thormond · Nov 24, 2013 at 11:00 AM 0
Share

I've been using delegates before but from user's perspective they are less convenient (having to inherit a class with event logic that derives from $$anonymous$$onoBehaviour ins$$anonymous$$d of $$anonymous$$onoBehaviour itself, and having to subscribe to all desired events in Awake() or Start()), what kind of efficiency drop are we talking about when it comes to reflection? $$anonymous$$uch lower performance?

avatar image Jesdisciple · Nov 24, 2013 at 10:05 PM 1
Share

The inefficiency generally won't be noticeable unless you use Send$$anonymous$$essage in, say, every OnGUI invocation. But I understand reflection as one of the least efficient language features (I don't know figures), so if it lands in a performance bottleneck it'll hurt. I code defensively, so unless I have a good excuse I don't use it.

But no, you probably won't notice the performance hit. Just be careful with it.

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

OnMouseUp interfering with NGUI OnClick Button 1 Answer

Unity 4.6 UI - Trigger OnClick event via script 1 Answer

Best way to load a level depending on which game object is clicked 1 Answer

Not able To send integer as a parameter on onclick? 1 Answer

NGUI Button Click Callback from external script. 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