Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Party Boy · Dec 12, 2013 at 12:32 AM · objectinheritanceweaponstandardpattern

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!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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:

http://unity3d.com/learn

Comment
Add comment · Show 3 · 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 Party Boy · Dec 12, 2013 at 10:04 PM 0
Share

@TehJustice I know it is quite broad, but I'm just not sure if you can make Unity scripts to a canonical standard really

avatar image TehJustice · Dec 12, 2013 at 10:21 PM 0
Share

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);
 }
 
 }
avatar image Party Boy · Dec 12, 2013 at 10:37 PM 0
Share

@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?

avatar image
0

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); }
 }
Comment
Add comment · Show 3 · 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 Party Boy · Dec 12, 2013 at 10:40 PM 0
Share

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

avatar image Loius · Dec 13, 2013 at 10:13 PM 0
Share

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.

avatar image Party Boy · Dec 14, 2013 at 12:56 AM 0
Share

That's cool man I appreciate the help and links :-)

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

18 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

Related Questions

How to flip object according to its rotation? 1 Answer

Storing and using weapons and objects 0 Answers

Overriding an inherited Enum? 1 Answer

FPS game: weapons 2 Answers

Class inheriting Runtime Classes 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