Unity Project Folder Structure

Structuring our files in a Unity project can be challenging. In this post, I'm going to share my approach.

There is no single method when it comes to structuring our files, but there is a single reason why we do so. I believe understanding this reason can help us a lot to choose the best method for a given scenario.

Think of a situation where you have a few hundred files in a single folder. How difficult would it be to find a particular file among them? Pretty hard, right? Well, that's why we need to structure our files! We do so to make it easier to find them later.

One approach is to subdivide the files and put them in subfolders based on their type, then their features. So we will have something like this:

  • Project
    • Scripts
      • Player
        • Movement.cs
        • Attack.cs
      • Enemy
        • Pathfinder.cs
        • Attack.cs
    • Prefabs
      • Player
        • Character.prefab
        • Weapon_A.prefab
        • Weapon_B.prefab
      • Enemy
        • Dwarf.prefab
        • Elf.prefab
    • Materials
      • Player
        • Outfit.mat
        • Weapon_A.mat
        • Weapon_B.mat
      • Enemy
        • Dwarf.mat
        • Elf.mat

This approach works fine if your project is small; otherwise, it's not scalable, and again you'll find yourself constantly scrolling up and down the Project tab looking for particular files.

Another approach is to group files based on their feature first, then their type. So we will have something like this:

  • Project
    • Player
      • Scripts
        • Movement.cs
        • Attack.cs
      • Prefabs
        • Character.prefab
        • Weapon_A.prefab
        • Weapon_B.prefab
      • Materials
        • Outfit.mat
        • Weapon_A.mat
        • Weapon_B.mat
    • Enemy
      • Scripts
        • Pathfinder.cs
        • Attack.cs
      • Prefabs
        • Dwarf.prefab
        • Elf.prefab
      • Materials
        • Dwarf.mat
        • Elf.mat

This approach is more scalable than the first approach because here, we are expanding horizontally. For example, if we want to add a new feature like NPC, we can add a new subfolder like this:

  • Project
    • [other features]
    • NPC
      • Scripts
      • Prefabs
      • Materials

Although this approach is better, we will end up with a very flat hierarchy when there are a lot of features. We can mitigate that by having some additional top-level folders to subdivide files even further. For instance, we can have something like this:

  • Project
    • Level Selection
      • Environment
      • UIs
    • Play Scene
      • Player
        • Scripts
        • Prefabs
        • Materials
      • Enemy
      • NPC
      • Environment
    • Shop
      • UIs
      • Purchasable Items

This approach is excellent, but how should we define our hierarchy? Not so clear, right? Sometimes, it's tough to clearly define what a feature is or what files can be group together. For instance, is the game manager a feature? How would you name that feature? Should we have a dedicated folder for single-player and another for multi-player mode? Where should we put the Player subfolder in that case, single-player or multi-player folder? You see, it's a tough job.

To make decisions about grouping and subdividing files easier, we should refer back to the reason why we structure our files in the first place. We know by now that the ultimate goal is to make it easier to find them later. We can go one step further and ask what group of files we usually need to access at the same time to complete a particular task? Well, this heavily depends on the task and the project, but asking/understanding this question helps us in grouping the files more effectively. My suggestion is to group the files based on how often we access them together.

An important thing to keep in mind is regardless of what method we choose to structure files and folders; we should be clear about it, and everyone in the team should be aware of it.

Comments

Popular posts from this blog

Implicit coupling will stab you in the back!

Why this blog?