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 peterpunk · Nov 15, 2013 at 11:32 AM · methodsoopbasic programming

Good practice to do a base extend of MonoBehaviour?

I am wondering whether it is a good practice to do a (basically void) extension of MonoBehaviour and make all your classes extension of your base - so you can anytime inject custom "trapping" code. (Searched the community but found no answers.)

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 PAEvenson · Nov 15, 2013 at 11:51 AM 0
Share

I personally think its a pretty good idea. It is also mentioned here http://devmag.org.za/2012/07/12/50-tips-for-working-with-unity-best-practices/ #21

1 Reply

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

Answer by Bunny83 · Nov 15, 2013 at 12:28 PM

The link PAEvenson posted above suggest an own base class to implement general purpose functions which every MonoBehaviour needs. If that's not what you want, having an empty base class doesn't give you any benefit.

You can't inject trapping code afterwards. This would only work if you had implemented all callbacks Unity provides in the base class as empty virtual methods and you always override them in your actual classes. This however would give you the worst possible preformance. Unity does optimisations depending on what methods you've implemented. If you always implement all methods Unity will call all of them.

If you think you have enough new things that every MonoBehaviour needs, feel free to make a base class, but keep in mind that pure functions can be implemented as extension methods. Only for per instance data fields you need a base class.

In most cases it doesn't make that much sense to create a base class. It's more appropriate for a group of classes that share a lot things. For example for a Node based system you might create a Node base class and derive other Node types from Node. However in Unity it's usually better to use the component system. Take a look at the GameObject. Every gameobject has a Transform component. This component builds the actual tree hierarchy. The actual GameObjects are just data-containers with no relation to each other. Only the Transform component has the parent link but this also doesn't link to other GameObjects but other Transforms.

Components should be designed for a certain task. To get the final object you attach all components an object needs to get the desired behaviour. It's a bit more difficult to write general purpose components, but in the long run it's way more flexible.

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 peterpunk · Nov 15, 2013 at 08:32 PM 0
Share

Thanks for the exhaustive and clear answer, really helped.

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

19 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Inherit from a behaviour (JS) 2 Answers

I have objects that have the same set of variables, but different desired behaviors. How can I do this without repeating variable declarations across all behaviors? 1 Answer

Organizing Enemy Movement Types 0 Answers

Best practice for adding specific attributes to an object but not all of them 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