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 book · Aug 20, 2013 at 06:39 AM · designoop

Are there any Unity demo projects that have an OOP design?

Are there any Unity demo projects that have an OOP design. I would like to start using a more object oriented design in my projects.

I'd really like to see how other people have used it.

Cheers!

Comment
Add comment · Show 4
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 whydoidoit · Aug 20, 2013 at 07:00 AM 1
Share

Are you looking to do that because you have a specific "need" to learn OOP? Pretty much all Unity projects use OOP (inheriting from $$anonymous$$onoBehaviour, Renderer etc) but I guess you want to see more game play logic in an OOP layer of classes? For my money individual component behaviours are way more powerful and flexible and should be used with class hierarchies where relevant.

avatar image robhuhn · Aug 20, 2013 at 07:24 AM 2
Share

I'm also not sure what you mean by OOP projects because it's all OOP. Are you talking about the design pattern?

avatar image book · Aug 20, 2013 at 08:51 AM 0
Share

I guess I've confused myself. Yeah I understand that unity is using oop, but does anyone create there own custom classes that inherit from other custom classes. Like creating a humanoid class and then creating an enemy and player subclasses that inherit from humanoid. then creating an orc subclass of the enemy like on the unity learn section on inheritance.

avatar image whydoidoit · Aug 20, 2013 at 09:49 AM 0
Share

Yes people do that - but as @robhuhn says, you want to use a component approach because it's more flexible. Deep inheritance architectures are very hard to refactor and should to be avoided if possible.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by robhuhn · Aug 20, 2013 at 09:35 AM

You would use OOP in Unity like in any other OOP environment. But following the component pattern used in unity you might have a slightly different approach when building the architecture.

You would set up an empty game object and all classes attached are components/behaviours. E.g. we have a component LaserGun extends Weapon extends Monobehaviour (MB) and ProjectileGun -> Weapon -> MB

Then we have another tree: Shield -> Armor -> MB and Hull -> Armor -> MB

These components can be attached to a game object Spaceship as needed. So in this case you would create different spaceships just by changing the components, not by extending a base spaceship class - Actually you don't have any spaceship class here.

That's just a very simple example. Of course you also use interfaces, structs and all that if needed.

Hope that will help you.

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 Jamora · Aug 20, 2013 at 09:54 AM 0
Share

This answer assumes spaceships don't have any behavior of their own. If they do, and all spaceships share some behavior, you can of course create a base spaceship class and extend that to each spaceship behavior.

avatar image
1

Answer by Jamora · Aug 20, 2013 at 09:39 AM

I only checked the stealth tutorial, I don't know about the rest. The stealth tutorial had a good Object Oriented Programming approach. They have their player in three objects ( = classes): health, inventory and movement; enemies have four objects, etc. While strictly speaking only instantiated classes are objects, I consider classes to be objects in their own right, only abstract.

All you need to do to follow the basic OOP guidelines is to think in terms of objects; determine the responsibilities and behavior of an object, then the encapsulate relevant data and functions to that class. It's impossible (or at least quite hard) to not do OOP (in the broadest sense) when working with Unity. If you have one script with a huge Update function that contains all logic, you are not doing OOP. If you have multiple classes with clearly defined behavior and relevant data fields you are doing OOP.

What helped me broaden my horizons, was the realization that objects are actually behavior. Before, I was thinking about objects as a unit: I asked myself, what does this object do on its own. Now I ask myself: how is this object used by other objects? To me, this difference is that of non-OOD vs. OOD.

tr;dr: Object Oriented Programming (in the broadest sense) can't really be avoided when using Unity, you must mean OOD. At least the stealth tutorial has OOD.

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
0

Answer by mathmos_ · Aug 20, 2013 at 09:59 AM

I find that using events and the State design patterns can help a lot when making something big and complex in Unity. Maybe consider using those.

Besides that, just remember that Unity is component based, so always try to make each of your components only have a single responsibility. It's usually better to have a lot of small components, that you can reuse, than a few big ones. And try to keep coupling low as well.

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

20 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

Related Questions

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

Best practice for adding specific attributes to an object but not all of them 2 Answers

OO Design | Specific example 2 Answers

how to planning n designing game structure/architecture? 0 Answers

Why do we create instances of classes in OOP? 1 Answer


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