How do i implement design patterns in Unity?
Hi,
I am new to Unity and i have been trying out a game. I have made Arkanoid game in 2D using tutorials. But now i am trying to implement design patterns in the game. I am not sure how to use it in unity. I have implemented it before in greenfoot. Please guide me in my problem.
The basic idea of the game is to bounce off the ball from a pad and hit the bricks above to destroy them. When all bricks are destroyed the level is changed. Some bricks when destroyed drop some powers which when collected by the pad changes its behavior.
In what cases can i implement the design patterns. I know i can implement observer pattern to show the score. Command pattern to show the menu screen.
You can't implement "design patterns", you have to implement some design pattern. Also, forcing the implementation of a design pattern is the wrong way to use them, a design pattern is something you should use if your requirements and tools allow you to implement it. Some patterns don't even make sense in Unity, so be more specific, what pattern do you need?
For fun, you can Search for all the questions here about the Singleton design pattern. The thing is, there's two obvious ways to do it, which anyone would do if they never heard about design patterns or singletons. Also for fun, Search for "design patterns stupid."
i am trying to implement command pattern. I know it will be used on my menu screen. but i am not sure how to implement it in Unity. I am able to implement design patterns in java.
Please post non-answers as comments.
The phrase "command pattern" doesn't really communicate anything. Particularly for a menu screen, which makes your request even more confusing. I think you're trying to use jargon you picked up in some other development environment, and it does not translate to any other environment.
Sunny: Java-only corporate shops are big into "Design Patterns," and I think many Software engineering programs $$anonymous$$ch them as a Big Deal. "Tell me about something you implemented using Command Pattern" is a job interview question. There's a whole world where people think Command Pattern communicates something.
rugved: But, yeah, most working programmers think Design Patterns are useless jargon, and are annoyed when people ask about them. AFAI$$anonymous$$, Command Pattern is just a fancy word for csllbacks, which I believe the latest GUI system has.
Answer by mehtanitish · Sep 06, 2019 at 01:59 AM
Hi,
Unity is a Entity component engine, so it makes sense to assemble all your functionalities as Entity- component. On top of this Entity component pattern you can implement various patterns as per your requirement. Below are some of the design patterns for various use cases:
-For HUD design, victory condition or achievements system you can use Observer Pattern, the basic idea is that you should use the observer pattern when you need many objects to receive an update when another object changes, like for health change, coins collected, level completed, etc. There are various ways to implement Observer pattern like creating pure observer/subject class(not inheriting from monobehaviour) or once using monobehaviour. The one way I find most simple is to use scriptable objects as game events and then registering those to listener/observer. For more information on scriptable objects and using those as game events(for observer pattern) you can search for Unite Austin 2017 - Game Architecture with Scriptable Objects, in youtube. Really good stuff.
For resusing Scripts you can Use Type Object pattern, lets say you have a game where there are 4 type of dragons, each behaves different from another, so instead of creating 4 totally different classes, what you can do is create once single Dragon parent class implementing features common for all and for ones the features are not exactly same you can mark some methods virtual and use that parent Dragon class to inherit your Base classes which are each different dragons. I did the same in one of my game. Basically you use polymorphism here.
For reusing scene, that is having a single scene acting as different levels, you can use some Game Config file, here scriptable objects will be extremely helpful, you can store the information about each level in the game config file and use that file according to the level you want to play. Taking the dragons example, say at level 2 you want to have a different background, different sprite for dragon and so on, but basically the controller and image component for background are same, so using that config that you can use different image for dragon and background on the fly.
Command pattern when you need some functionality with replay or undo, you can use command pattern.
Sometimes old Q's get "pinged" and brought up -- check the date. It's fine to add a new answer to this 4-year-old old question, if that's what you were intending. If you want to discuss design patterns, the Forum area is better for back-and-forths and opinion.
Your answer
Follow this Question
Related Questions
Implementing game logic in a separate library: callbacks 1 Answer
Extending Behaviour instead of Monobehaviour 0 Answers
NullReferenceException while following Unity3d College tutorial @JasonWeimann 0 Answers
Weapon enchantments - designing the code architecture? 2 Answers
designing GameController UI relation 0 Answers