DEV Community

Artem
Artem

Posted on

Using MVC With Unity

Quick warning- I tend to use framework and design pattern interchangeably.

Back in 2016 I remember seeing a lot of job openings seeking Unity developers with working knowledge of MVC framework. At the time it didn't make too much sense to me considering Unity primarily supports the Component framework. Thus I set out on a mission to create a project using the MVC framework exclusively.

TL;DR - it didn't end too well.
Here's what I learned along the way!

What actually is MVC?

MVC stands for Model-View-Controller.
Each of these are specific functionalities for working with data.
The model stores your data and manipulates it.
The view displays your data, generally you want to keep your view dumb (that is to day, it shouldn't store or manipulate any data).
The controller will fire off commands to your model to do stuff with your data.

Don't try to use one framework for your entire project

Much like how you'll often hear people telling you to not over use Singletons you shouldn't try to over use MVC. All frameworks have their specific functions and support specific features.

A good time to use MVC

I found that in instances of storing, manipulating, and displaying data (like keeping track of score, multiple choice answers, instructions, etc.) MVC is super useful.
I was able to decouple the logic behind multiple choice questions, which is super helpful when it's a last minute add from the UX team and nobody knows how it will look, or how the user will input the data, or what any of the questions or answers are.

A not so good time to use MVC

When you're trying to manipulate game elements like tiles in a match 3, or like 3D objects in a VR application, or storing state in your experience.
However, MVC is really good for manipulating chess or checker pieces on a game board.

Conclusion

MVC is a tool first and foremost, like all other frameworks and design patterns you should pick and choose which works best with what you are trying to do. If you find that using a specific style of coding is making your application harder to maintain, debug, and update then maybe consider not using it as much (or at all).

Are there any frameworks or design patterns you enjoy using?

Top comments (4)

Collapse
 
glennheckman profile image
Glenn Heckman Jr.

Nice article. I’m curious to know what about MVC made it difficult when working with game elements within the 3D / VR space?

What was your solution in place of MVC for those use cases? Was it a hybrid approach of sorts?

Thanks!

Collapse
 
vtiioma profile image
Artem

Hi Glenn :)

Short answer is - MVC is too rigid for a lot of simple functionality, so I only use it in cases where it doesn't introduce more work for me like quizzes.

Longer answer being - sometimes I found that MVC just made the process too rigid and it was hard to effectively pass data back and forth. Examples that come to mind is state management, and physical conditional checking (like to check if an object was placed in a location).

I found that using MVC for specific sections worked well, so my mini-game elements would rely on MVC (like quiz sections really work well with MVC), but everything else would just have a 'whatever works' approach.
Top 3 alternatives I use ended up being

  • observer pattern
  • singleton pattern
  • component pattern (which is the default Unity way)

My goal was to not break Single Responsibility Principle just to force myself to use MVC.

That's a good set of questions and I'm very thankful you asked them :)
Hope this helps.

Collapse
 
goldennoodles profile image
Rus Kuzmin

Great read, thanks!

Collapse
 
vtiioma profile image
Artem

Thanks!