- Home /
Need help implementing a design pattern
Hey. So I'm new to Unity and I'm going to try develop a game for my project in college. In another course we were asked to apply a Design Pattern to our project. There are some specifications that sort of confuse me as to how they can be done in Unity. Here is basically what the specification says:
1) Apply a design pattern 2) Develop 4 or more classes up to canonical standard 3) Link some classes using inheritance
For 1, I heard that the Object Pool pattern is quite handy to implement in Unity, and I actually have it sort of working at the moment. The bits that make it harder for me to do are 2 and 3. From what I read, canonical standard classes basically implement Cloneable and Serializable and override the equals method.
I'm basically looking for advice on how I could approach this? I was thinking of having a weapon class that is used for different weapons, with each weapon having a different class that inherit the main weapon, and using the object pool to display a different outputted object each time you use a weapon. However I'm not too sure how I can make this work in Unity, as it uses Scripts which I'm new to.
I would appreciate any help in the right direction. Thanks!
Answer by TehJustice · Dec 12, 2013 at 09:51 PM
How to use scripts in Unity is perhaps the broadest question you could ask. You should take a look at some tutorials and documentation:
@TehJustice I know it is quite broad, but I'm just not sure if you can make Unity scripts to a canonical standard really
I implemented something like this in one of my scripts:
public class SomeObject : IComparable<SomeObject> {
public int length;
public SomeObject() {
...
}
public int CompareTo(SomeObject other) {
return length.CompareTo(other.length);
}
}
@TehJustice Ah I see so it doesn't always have to be SomeObject : $$anonymous$$onoBehaviour.
So if I had to make a class a canonical class which implements Serializable and Comparable, would it be something like:
[System.Serializable]
public class Score$$anonymous$$anager : IComparable<EnemyScore$$anonymous$$anager>
{}
For example?
Answer by Loius · Dec 12, 2013 at 10:28 PM
You need to not think a "script" is somehow different than what you're used to. A "script" in Unity just is a class that extends MonoBehaviour class so it can be attached to a game object. One thing to note is that MonoBehaviour is not System.Serializable, so anything that extends it cannot meet the canonical standards requirement (i don't know what that actually means, i'm just working off of your definition).
But it's easy to get to a place where you need custom classes in a Unity project, and they work just like the classes you already know. For instance, I need an integer-based Vector3, so I made an IEquatable, Serializable class:
/// <summary>Integer Vector3. Any input floats are rounded to int on the assumption that you meant 0.99999999 as 1.0, not 0.0.</summary>
[System.Serializable]
public class IntV3 : IEquatable<IntV3> {
public static bool operator ==( IntV3 a, IntV3 b ) {return a.y == b.y && a.x == b.x && a.z == b.z;}
public static bool operator!=(IntV3 a, IntV3 b) {
return ( a.y!=b.y || a.x!=b.x || a.z!=b.z );
}
public bool Equals(IntV3 o) {return o.x==x&&o.y==y&&o.z==z;}
public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode(); }
public override string ToString() { return "("+x+","+y+","+z+")"; }
public override bool Equals(object obj) {
if ( !(obj is IntV3)) return false;
IntV3 o = (IntV3)obj; return o.x==x&&o.y==y&&o.z==z;
}
public int x,y,z;
public IntV3() {x=y=z=0;}
public IntV3(int a, int b, int c) { x=a;y=b;z=c; }
public IntV3(float a, float b, float c ) {x=Mathf.RoundToInt(a);y=Mathf.RoundToInt(b);z=Mathf.RoundToInt(c); }
}
Thanks a lot for the in-depth post, it clears up quite a bit for me. Going by my canonical requirement, does your example meet that standard? I see you use Serializable, and I'm guessing IEquatable is the Unity equivalent of Comparable? Or would it be IComparable???
What design pattern would you suggest I look into, to get familiar with it. I already have a singleton pattern implemented, along with an Object pool, I think they are done properly, but I will have to make them canonical
Like I mentioned, I really don't know much about what 'canonical' means, I can only work off what you define it as. IEquatable and IComparable are different things, so if your definition says to use IComparable, you'll want to be sure to use that ins$$anonymous$$d. It'll probably end up looking pretty similar.
IComp: http://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.110).aspx
IQaut: http://msdn.microsoft.com/en-us/library/ms131187(v=vs.110).aspx
As far as design patterns, I only really know a few off the top of my head. Object pools, singleton managers, factories... yep.
Your answer
