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 sandsand · Dec 30, 2013 at 11:57 AM · designarchitecturedesign-patterns

Unity architecture

I have started learning Unity recently. I have some questions (maybe a bit premature) about its design.

First, why there exists the SendMessage method? Why use reflection when you can just use a simple observer pattern and limit the number of receivers to ones that implement an interface? Second, how trigger callbacks (`OnEnterTrigger` etc) are getting called if I dont use override? Again, why is it not implemented by listeners?

And finally, what could you advise me to read on Unity architecture and games design with unity?

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 MagicoCreator · Dec 30, 2013 at 12:44 PM 0
Share

Send$$anonymous$$essage is quite usefull if you use both unityscript and C# because you can make cross-script calls with ease, which otherwise would be a major pain in the ass.

3 Replies

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

Answer by Bunny83 · Dec 30, 2013 at 01:40 PM

Unity tried to make things easy for beginners and decouple components. The observer pattern is nice and dandy but it requires more setup work on the user side. Sometimes it's a pain to get the correct reference of a nested component. SendMessage is a generic way to execute methods on other components.

Unity callbacks (Update, Start, OnTriggerEnter, ...) are ordinary methods but the invocation is highly optimised. As you might know the core of Unity is written in native code, mostly C++. ´Mono is "just" used as scripting interface. Unity analyses your classes during compilation / loading and probably holds some kind of flags / invocation lists for each possible callback. So Unity only executes callbacks that are actually implemented. When using virtual methods all possible callbacks would be required to be executed. That would be a huge overhead. The way they implemented it, the engine only executes callbacks that are implemented.

Unity invokes all callbacks from native code. So there's always the transition overhead between native <--> managed code. SendMessage uses some kind of reflection internally. How it's implemented in detail we don't know, but it's quite optimised. Of course it's some magnitudes slower than using a direct method call, but it most cases it doesn't matter. It also doesn't force you to use it. A lot people use their own messaging system. Check out the wiki there are a lot messaging systems available for free ;)

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
-1

Answer by yatagarasu · Dec 30, 2013 at 12:06 PM

SendMessage is called on the next Update. So unlike simple method call it is delayed by one frame. It can be used if you want to accumulate messages from multiple objects or handle message after all objects are updated.

You should read http://docs.unity3d.com/Documentation/Manual/ExecutionOrder.html

Comment
Add comment · Show 1 · 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 · Dec 30, 2013 at 01:25 PM 0
Share

Sorry but that's just not true. Send$$anonymous$$essage executes the method immediately.

avatar image
0

Answer by autowash · Dec 30, 2013 at 12:06 PM

Totally not an expert here, but I agree with challenging SendMessage (and so do many others, I believe). I've used Unity for a year or two, and never needed to use SendMessage.

Don't really understand your question about the triggers...

Tons of youtube videos out there, I got started with the infiniteammo tutorials (just search), but you seem to have a more solid programming background than I did when I started so there might be some better starting point for you. A good intro though.

And the docs and example projects are actually really good, will take you a long way both architecture- and otherwise.

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

22 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

Related Questions

How to avoid one engine class controlling the game flow getting too large 3 Answers

Callbacks in component based design 1 Answer

Need advice on Design 0 Answers

Where to put debug methods and variables 0 Answers

How do I hook custom inspectors up to models? 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