Was not sure where to post this, but was directed here from unity answers.
Hello, I’m a Windows Phone Developer and I have just finished publishing my first Unity game and really loved the tools. I am considering purchasing the pro version and developing higher-scale games.
My question: Coming from a WP background, I am used to following strict Design Patterns in my projects to write simple good code. Namely MVVM (MVC), Façade, factory patterns, singleton… One of the things I struggled a lot with in Unity was, due to how different the system is, coming up with some Design Patterns to adhere to, as to simplify writing my game, minimize bugs, and making unnecessary changes later. I wanted to know what are some of the Naming (script/class names, namespaces…), structural (folders/ script reuse), and Design patterns people use for large-scale Unity projects. Also, should one adhere to using a Façade with many adapters or reusing a single script across many GameObjects? Thanks! -Francisco
I think the best approach is the simplest - write good components with clear, well defined responsibilities. Use a small number of manager/singleton/static classes where absolutely necessary. Keep things as data driven as possible, and use prefabs and scriptable objects to store different cofigurations. For well understood common problems like UI, level scripting, audio authoring, etc use battle tested solutions from UAS like playmaker, NGUI, SECTR, etc.
PS - you should also resist the temptation to use too many patterns proactively. Game code changes a lot and is intrinsically complex, so just KISS all the time and use patterns to improve something that works, not speculatively before you really know what you’re making.
Sounds good.
I know this is kind of off topic, but I figured I would ask. Would it be more efficient to minimize the number of update/fixedupdate loops a game has and keep components isolated or try and utilize a smaller number of loops.
Right now i’m doing mostly mobile programming and efficiency has been kind of tough to deal with in terms of heating up the phone and/or battery life. I’ve got it down averagely well now but I had to do some very unorthodox things like limit the framerate by changing the vsync count to every second vblank…
In general, favor event based designs over polling/update whenever possible. Beyond that, I’d recommend you have as many update functions as you need and keep the components clean. If you find that down the line you’ve made them too fine grained, then you will be in a good position to know how best to consolidate things.
Also, dropping framerate to 30 or even 20 is not uncommon in mobile games provided you do it at times when it won’t negatively impact responsiveness.
Ok, yes, I have been trying to used events/function calls as much as possible.
But, In this specific example, what would you recommend:
I have a parallax background, with a parent Background Manager gameobject. This manager has 3 children, Background, Midground, and Foreground. Each of the children has the same script a ParallaxController with an update loop. Wouldn’t it be better to just have 1 update loop on the parent that controls all 3 children instead of 1 per child?
This example is a recurring theme in my dev. experience so far. There is always some parent object that could take over the loop for multiple children objects and it makes me feel like if I did that exclusively my games would be better performing…
In this case I’d just make one Background component which takes references to the individual layer objects. A background component is reasonably specific, simple, and independent, so no need to have one component per layer.