Search Unity

Design Patterns applied on Unity

Discussion in 'General Discussion' started by AlexFormoso, Mar 25, 2013.

  1. AlexFormoso

    AlexFormoso

    Joined:
    Aug 12, 2012
    Posts:
    25
    Hello! I'm currently doing an investigation for my engineering thesis, about Software Design Patterns for videogames, and since I think this is going to be my chosen thesis subject I want to ask this community about something particular:

    •If you where about to implement every single programming pattern you can think of (from the well known singleton, so something more weird in gamedev like MVC/MVP), would you use Unity??

    I'm developing under Unity, but I don't know if the component oriented structure in unity will make things easy for me when using architectural patters such as MVC.
    In my thesis I will add simple mini-games or demos using each pattern with a PRO/CONS table.

    Already posted on reddit gamedev, if someone is interested in knowing the answers I got there, here they are
    http://www.reddit.com/r/gamedev/comments/1awxxs/design_patterns_for_videogames/

    Thanks in advance for the feedback!
     
  2. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Unity is a tool and you can use any design patterns where they are appropriate.

    i.e The State Design Pattern can be used for an AI FSM and there is a plugin for NGUI which uses MVVM. Two simple examples but patterns are meant to address common design problems, no matter the end product. I use the singleton pattern for my GameManager object in all my projects.

    At the end of the day if the pattern can help, use it, otherwise don't.
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    When you see that it makes sense to use a pattern, there is always a way. In a programming environment you have always restrictions. In Unity you are bound to MonoBehaviour if you want to see something in the inspector. When you are using WinForms or anything else, there are other restrictions.
     
  4. AlexFormoso

    AlexFormoso

    Joined:
    Aug 12, 2012
    Posts:
    25
    Does that mean that my thesis idea its useless because unity can apply "any" pattern out there?
     
  5. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    Any pattern that doesn't rely on constructors! ;)

    An interesting thesis would be to compare how high performance, high productivity server-side programming and game programming have diverged in the last 10 years along with the parallels in progress of server hardware and home computers / consoles / phones, and what that means for the future of game engines and game programming. Then, when you're done with your degree, put together a team write a game engine that leverages what has been learned on the server side and make a few 10 of millions of bucks.
     
    Last edited: Mar 25, 2013
  6. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    Ehhh...not so much.

    First, while MonoBehaviours in Unity may not use a constructor (directly) - I recommend the Awake and Start events, as those make a close-enough approximation - you can make use of composition.

    A gotcha that I discovered early on with Unity's implementation of C# is, for variables in a subclass to be editable through the editor, the class they are in needs to be decorated with the Serializable attribute, from System.

    Example:

    Code (csharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Foo : MonoBehavior
    6. {
    7.   #region Variables / Properties
    8.  
    9.   public StateMachine Fsm;
    10.  
    11.   #endregion Variables / Properties
    12.  
    13.   #region Engine Hooks
    14.  
    15.   public void Start()
    16.   {
    17.     List<State> states = new List<State>
    18.     {
    19.       new State(ConditionA, ActionA),
    20.       new State(ConditionB, ActionB)
    21.     };
    22.  
    23.     Fsm = new StateMachine(states);
    24.   }
    25.  
    26.   #endregion Engine Hooks
    27.  
    28.   #region Methods
    29.  
    30.   // ConditionA and FooA are defined here...
    31.  
    32.   #endregion Methods
    33. }
    34.  
    35. [Serializable]
    36. public class StateMachine
    37. {
    38.   #region Variables / Properties
    39.  
    40.   private List<State> _states;
    41.  
    42.   #endregion Variables / Properties
    43.  
    44.   #region Constructor
    45.  
    46.   public StateMachine(List<State> states)
    47.   {
    48.     _states = states;
    49.   }
    50.  
    51.   #endregion Constructor
    52.  
    53.   #region Methods
    54.  
    55.   // Behaviors that the class engages in.  Implement your own!
    56.  
    57.   #endregion Methods
    58. }
    Another point of order is that sometimes MonoBehaviours are not the key to implementing some actions; a standard class instance can do the job just as well.

    Smooth P., I understand from another topic you've at least appeared on the Wave Engine forums. Do you actually use Unity at all? For a programmer like yourself, I would've thought you'd known some of this...
     
  7. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    No, it sounds like they are asking for how patterns can be applied to game development, not if they can be used.
    I'd suggest doing some research and see what patterns have been applied in the past in which situations.