C# style guide: E-book and sample project for Unity users

Hey everyone,

My name’s Thomas. I head up the technical content team in Copenhagen. Our team produces e-books, demos, videos, and how-to articles available to anyone creating games and apps with Unity. We do that in partnership with our R&D teams, and internal and external Unity developers. We use your feedback on forums and social media to help us prioritize a lot of the topics as well.

One of the popular requests we see a lot of interest for is naming conventions and how to style and format your code. There are many code conventions out there and while there might not be one right way to format your C# code, agreeing on a consistent style across your team can result in a cleaner, more readable and scalable codebase.

In this short post I thus wanted to highlight our resources for creating a C# style guide for your programming team.

How can a style guide help your team?
Unity_C-sharp style guide_e-book

Let’s start with the question of why you need a style guide. Some might say, you don’t need one, you should just follow the industry standards. However, if you worked on a bigger game project in a larger studio you likely experienced the need to tweak that to your project needs. I think the key takeaway is that ideally, a Unity project should feel like it’s been developed by a single author, no matter how many developers actually work on it. A style guide can help unify your approach for creating a more cohesive codebase and can be a great formalized way of agreeing on what standards you want to follow in your team

Developing a style guide will probably require a sizable investment of your time upfront, if you decide to go a very custom route. Obviously, it will be easier the more you stick to the standards.However, in any case, it can really pay off in the long run because you’ll be managing, and working from, a single set of standards. It can help you onboard new programmers efficiently, for example.

My team’s general advice is to keep things practical by focusing on a guide that covers the majority of day-to-day use cases. Don’t over-engineer it by attempting to account for every single edge case from the start. The guide will evolve organically over time as your team iterates on it from project to project.

Allright. That was a bit of the why and some high level guidance. Let’s take a look at the resources we have to help you get started.

  1. The e-book: Create a C# style guide: Write cleaner code that scales

    This 58-page free e-book was created in partnership with internal and external Unity experts. It provides tips and best practices for naming conventions, formatting, classes, methods, comments and more for those studios who would like to create their own internal style guide

    Our overall approach was to follow the Microsoft C# style standards and make a Unity-specific subset of that, but there is no one “true” method; the Google C# guide is also a great resource for defining guidelines around naming, formatting, and commenting conventions.

    We recommend customizing the guidelines provided in our style guide to suit your team’s preferences. Your preferences should be prioritized over our suggestions and the Microsoft Framework Design guidelines if they’re in conflict.

    Formatting code in Microsoft Visual Studio; the Preview window shows off your style guide choices

  2. The sample C# file

    An example C# file accompanies the e-book, providing a concrete example you can use for inspiration. Feel free to make a copy and use it as a starting point for your own,tweaking it as see fit. We are not covering all the edge cases but we are focusing on helping you remove the guesswork for every member of your team by establishing consensus around important coding conventions, thereby supporting the growth of your codebase into a commercial-scale production.

  3. The video tutorial: Tips for creating your own C# code style guide

    Finally, you can watch this introductory tutorial which highlights some of the key tips from the e-book. If you are just getting started this 12 min video gives you a quick overview of some of the key concepts to consider.

More advanced know-how for Unity developers

The C# style guide is one in a series of resources we created for experienced Unity programmers. The other e-books in the series are:

  1. Level up your code with game programming patterns
  2. Create modular architecture in Unity with ScriptableObjects

You can find all the e-books (and many how-to articles) in the Unity best practices hub or via the best practices page in Unity documentation.

Let us know if any of these resources are helpful. We really appreciate it and use your feedback to both update existing but also plan future resources.

1 Like