Objective: The objective of this post is to give you an idea about Strategy Pattern, How to implement and why should you use in unity. Overview: Strategy pattern can be categorized as behavioral pattern, which is very simple and powerful. It can be considered in a kind of scenario where your object would perform a common action and that too selecting from various options available. What is Strategy Pattern? Strategy pattern defines a family of algorithms, encapsulates each one of them and makes them interchangeable. 1) Family of Algorithms- The definition says that the pattern defines the family of algorithms- it means we have functionality (in these algorithms) which will do the same common thing for our object, but in different ways. 2) Encapsulate each one of them- The pattern would force you to place your algorithms in different classes (encapsulate them). Doing so would help us in selecting the appropriate algorithm for our object. 3) Make them interchangeable- The beauty with strategy pattern is we can select at run time which algorithm we should apply to our object and can replace them with one another Seriously we will confuse with the definition at first time. So let’s make it simple. We can say it other simple words, Strategy pattern are algorithms inside a class which can be interchanged depending on the class used. This pattern is useful when you want to decide on runtime which algorithm to be used. Problem Statement: So consider a game scenario. A Military Tank can able to shoot the enemies with weapons consider as Bullet, Missile, grenade and bomb etc. How will plan to develop this scenario. Lets discuss one by one. In game development we will get new type of feature and requirements at any point of time. So, our responsibility is to make our code to be expandable at time. How a beginner behaves – Myself on earlier days: Without knowing the proper use of OOPS for given problem statement, a beginner will start with creating a class named as PlayerScript with list of transform for Weapons and fire bullets with the weapon selected. So this is how our normal script looks and it will work as expected. After few days a new requirement is coming up with adding more weapon types. What we are going to do now? Add a new weapon into prefab list and modify the weapon type. This will get more complex when weapon coming up with more properties like distance, moving speed and etc. This solution is not going to work anymore. Can we use inheritance? We can create a class for weapon and create sub classes for weapons. Will it work out? Yes, it will work out. Create a weapon class and extend with different weapon type classes. But, consider if you want to add a new method to set color for Military Tank. For this answer, you must create another Player class and its sub classes. So, Even this solution is not going to work anymore. Answer – Use Strategy pattern In this solution, create a weapon class and two more classes which extend this class. (I.e) Weapon and Color class. Weapon class is implemented by weapon type and Color class is implemented by color type. Here we got the solution. Let’s try with code. Strategy pattern - using in unity: Before you start consider that Cube as Military tank, Sphere as Bullet and Cylinder as Missile. Set Scene
Here is weapon manager class, Code Editor
And Color Manager for cube, Finally here comes PlayerScript, Finally you can happily see the strategy pattern was implemented successfully in unity. GamePlay: Find the attached unity package below. If you have any queries related this, please do comment. :) Next will meet all with Observer Pattern in Unity. Thank you.
0 Comments
Every unity developer came across the situation, where he needs to split animations, what 3D artist were provide. few of them might have very large animation in to their model. So people like me get easily irritated to split it every time, when a model is imported. For example, i was worked in a multiplayer fighting project and it contains lot of characters and different set of animations. I was tired editing and adding key frames. To avoid this pain point, i came up with the solution for splitting animation automatically. In order to achieve it, we just need to select the animation list .txt file and just click on split animation button. The rest of the work will done by our editor tool. one thing you need to do is just download the unity package. Advantages : - Most important thing is time save. - No need to look every time either we split the key frames perfectly. - Easy to use.
This tiny editor script will help to modify the color of selected game object via editor. I have been changing "standard shader" main color. Modify the shader name as per your requirement.
Hello All, Here is the simple package to explain How to create a moving text in unity3d. Just download the package and explore it. Before we go more detail, just open the MovingText Assets -> scene -> MovingText, Run it.... You can able to see texts are moving around left, right and middle of the scene. Okay, now we’ll see how to make it use in your game. Here the Steps to follow use other gameobjects:- 1) MovingText Script will helpful to create moving texts, and here it’s attached to main camera, you can attach it to any game objects in your scene. 2) Drag the Moving text prefab to go_movingText. 3) Drag the GainTextMat and LoseTextMat to GainMat and LoseMat. 4) Call the CreateMovingText Function whenever you needed. 5) Just pass the position to create, score, Gain or Lose and move to which side of screen. Eg) CreateMovingText(v3_somePosition, “+25”, “Gain”, “Left”); // For Gain CreateMovingText(v3_somePosition, “-25”, “Lose”, “Right”); // For Lose 6) If you wanna to call from other script, use MovingText. CreateMovingText(v3_somePosition, “+25”, “Gain”, “Left”); // For Gain Enjoy it... Here is the package to download ,
|
AuthorSriram M G. ArchivesCategories |