Introduction to folders in Unity projects

Preface

I read an article on Zhihu. There are two ways to get started when teaching the same thing. One way is to start from simple learning, and the other way is to start from basic learning. As the saying goes, no matter whether it is simple or basic, both must be learned in the same way.

This article introduces the content related to the directory structure of the Unity project that the blogger himself has learned. In the end, the official documentation of Unity shall prevail. Of course, there are many kinds of sharing on the Internet, and bloggers just throw out ideas. If you have any questions, please feel free to correct them.

Directory introduction to Unity project

The Unity version used here is 2022.3.3f1c1. Other versions may be slightly different, but they are generally the same.

First we create an empty project, then let’s name the project Dummy_2022_3_3f1c1, and create it in the directory

Here Unity automatically creates the required files, and you can observe that the project is a C# project.

.sln and .csproj are C# project configuration files, related document  assembly definition - Unity manual

Other directories are agreed upon by Unity and created in advance. Related documents,  special folders and script compilation order - Unity Manual

When reading and understanding the purpose of these directories and files, please keep in mind that most files in Unity projects are in text format by default. It is important to be able to open and see the contents. It is important to remain curious about the contents of these files.

Assets directory

The Assets directory contains the resources used by the project. There are usually two categories:

  1. Create external files that can be recognized by Unity, such as code (.cs file), textures (.png/.jpg, etc.), models (.fbx, etc.), etc.
  2. Files created in Unity, such as materials (.mat), configurations (ScriptableObject, suffix is ​​.asset), prefabs (.prefab), scenes (.unity)

All files and folders under Assets will be generated by Unity with corresponding files with the meta suffix. For example, if there is GameObject.prefab, then Unity will create a GameObject.prefab.meta file, and this meta file will store this resource. Globally unique id, this id is used to record reference relationships between resources. Therefore, when manually moving files outside the Unity editor, make sure that the file and meta file are moved together, otherwise the reference relationship between resources will be lost.

Under the Assets directory, there are also some directories specially agreed by Unity.

Editor directory

Unity - Manual: Special folders and script compilation order (unity3d.com)

In short, the code that is only used in the Unity editor should be placed in the Editor directory. These codes will be removed when building the game package, because the editor functions provided by Unity are only used in the editor environment. If they are included with the code It is built into the game package and an error is reported directly during the build.

Since the Editor directory and Plugins directory involve the concept of assembly, they are combined here and discussed together.

Unity will automatically merge code that is not a custom assembly into the top 4 special assemblies, namely

Assembly-CSharp-Editor-firstpass: Code located in the Assets/Plugins/Editor directory

Assembly-CSharp-firstpass: Code that is in Assets/Plugins but not under Assets/Plugins/Editor

Assembly-CSharp-Editor: Code in **/Editor but not under Assets/Plugins/

Assembly-CSharp: The remaining code of the above assembly

Another: The picture above is the result of using Rider IDE to select solution mode. VSCode also has similar functions to see the solution division.

The C# code written in the project is of course not the source code that runs directly, but the dll compiled after executing the source code.

Each assembly in Unity corresponds to a csproj, and each csproj corresponds to a dll (dynamic link library) file.

The dll used in the editor environment will be generated under Library/ScriptAssemblies. As can be seen in the screenshot below, there are 4 csprojs on the left and corresponding dlls in the Library on the right.

Unity's four special assemblies Assembly-CSharp-* are maintained by Unity and will reference other related assemblies by default. At the same time, please note that user-defined asmdef assemblies cannot reference the code in these four assemblies. Usually, you can write code without dividing the assembly according to Unity rules. However, as the scale of the project becomes larger, not dividing the assembly may bring many pitfalls. To give a few examples, the code hot update solution, an introduction to the popular solution | HybridCLR ( code-philosophy.com)  This solution requires specifying which assemblies to hot update, and finer-grained partitioning is more controllable.

For custom assemblies, Unity provides the Assembly definition function, which can create an .asmdef file. All directory files and folders where the .asmdef file is located will belong to this asmdef.

For example, first create a MyAsm.cs and see that the assembly is divided into Assembly-CSharp

Then right-click the menu in the MyAsm directory, Create=>Assmebly Definition to create an asmdef file.

In this way, let's compare the changes between assemblies. Now MyAsm has been moved from Assembly-CSharp to NewAssembly.

At the same time, csproj also has one more NewAssembly.csproj, and dll has one more NewAssembly.dll.

This completes the division of the assembly, but it is not finished yet. Assembly division also comes at a cost.

  1. To call code in another assembly, an assembly must configure a reference, and it can only be referenced in one direction, not in two directions.
  2. In addition to Unity's four special assemblies that automatically reference other assemblies, custom assemblies need to be manually set to reference other assemblies and reference other dlls.
  3. Custom assemblies require manual setting of the effective platform

Here you have to look at the assembly configuration panel. Here are a few important ones to introduce.

Plugins directory

Plug-in directory, other directories can be placed under the plug-in directory according to the platform, such as Android directory, iOS directory, etc.

Resources Directory

Resources folder - Unity Manual

The resources in this directory can be loaded by Unity using the Resources - Unity script API method, and when building the game package, the resources in the Resources directory will be built into the base package. Note that this is built into the package, so the resources under Resources can be loaded directly.

Note that Unit now does not recommend using this directory to store too many resources.

The path should be written starting from the Resources directory. There is no need to write the file name suffix. For example

var prefab = Resources.Load<GameObject>("TestLoad/GameObject");

StreamingAssets directory

The files in the StreamingAssets directory will be put into the base package when building the game package. Note that it is put into the base package rather than built into the base package, so resources cannot be placed directly into the StreamingAssets directory. The following is a counterexample. Put a prefab into the base package. Drag into the StreamingAssets directory, Unity doesn't even recognize it:

 Unity can use  Application-streamingAssetsPath - Unity script API to splice and obtain paths.

What is special is the game package of the Android platform. Under the Android platform, the StreamingAssets path is in the apk file and cannot be read directly using the file IO interface (in short, apk is equivalent to a zip package. If you want to read the file of the zip package Direct reading is not possible, of course there are other ways), you need to use WebRequest to read

public IEnumerator LoadStreamingAssets()
{
    var streamingAssetsFilePath = Application.streamingAssetsPath + "/TestLoad/Custom.bundle";
    if (Application.platform == RuntimePlatform.Android)
    {
        var request = UnityWebRequest.Get(streamingAssetsFilePath);
        yield return request.SendWebRequest();
        if (request.result == UnityWebRequest.Result.Success)
        {
            var bytes = request.downloadedBytes;
        }    
    }
    else
    {
        var bytes = File.ReadAllBytes(streamingAssetsFilePath);
    }
}

In order to read the resources under StreamingAssets, the usual approach is to convert the resources into Unity's Bundle (Bundle can be thought of as encapsulating resources. Unity's two resource-to-bundle conversion solutions are AssetBundle and AddressableBundle, which will not be expanded here), and then Put the Bundle into StreamingAssets, which means reading the Bundle and then loading the resources in it. What is more convenient is that the interface provided by Unity has already handled the situation of reading the resources in the Android package, so the path is directly passed to Unity to read the Bundle.

var streamingAssetsFilePath = Application.streamingAssetsPath + "/TestLoad/Custom.bundle";
var bundle = AssetBundle.LoadFromFile(streamingAssetsFilePath);
var prefab = bundle.LoadAsset<GameObject>("Assets/TestLoad/GameObjec"); 

Library directory

The Library directory places the resources imported by Unity, the intermediate files of the package and build, and the package

First, let’s explain what import is. For example, create a folder in Unity’s Assets directory, and then put a smile.png smiley face picture. Then when Unity runs the game later, it will use this picture directly. png picture? Of course not, Unity needs to convert this png image resource into a file format that Unity can use and put it in the Library directory. This conversion process is the import of the resource. Only imported resources can be used by Unity. And because the final output platform (Android, iOS, Win, Macos) requires different resource import settings, the image import processing may also be different.

Logs directory

Unity wants to write some logs to this directory, but note that UnityEditor.log and many special logs are not here.

For more details, please refer to  Unity - Manual: Log files (unity3d.com)

Packages directory

The directory where package management is located. Package can be understood as some plug-ins or toolkits. This is a set of directories used by Unity to facilitate configuration and management. Usually there are two files manifest.json and pacakges-lock.json. This package manager has a structure similar to npm, in which manifest.json configures the version number information of each package, and packages-lock.json configures the actual package version used.

You can see that manifest.json writes the package name and version number, and package-lock.json writes the warehouse, installation method, dependencies and other information of the package used by the local project. This package configured with the package warehouse will be placed in the Library/PackageCache directory of the project after installation.

However, please note that the package content placed in Library/PackageCache cannot be modified. If you need to modify it, you need to use a plug-in or manually copy the package to be used in Library/PackageCache to the Packages directory. At this time, return to the Unity editor and you will find that Unity automatically Change the source: "registry" of the package recorded in packages-lock.json to the source: "embeded' configuration, so that the priority is to read the Packages directory and maintain and modify the package by yourself.

ProjectSettings directory

Project configuration path, the configuration in Unity ProjectSettings is written to the configuration file under this path

Temp directory

Temporary path. This directory will be deleted when the Unity editor exits. Unity will put some intermediate results of processing into this path. Note that there is a file named UnityLockfile in the directory. This is used to mark that the Unity project has been A Unity editor is opened. If the UnityLockfile is deleted while the project is being opened, the Unity editor can open the same project multiple times at the same time. Of course, doing so will cause various strange error reports.

UserSettings directory

It usually records user configuration and interface layout. The blogger has not read the specific content of this directory, hahaha

Summarize

The above is some introduction to the Unity project directory. The content is not completely covered. The use of many more specific files has not been mentioned by Unity officials. You have to check the information on your own to find out the specific use. If bloggers have new discoveries in the future, they will continue to add.

Also share a link I found online that you can refer to.

  1. The Uninomicon. Share some obscure dark corners in Unity documentation  The Uninomicon [Uninomicon]

おすすめ

転載: blog.csdn.net/zdyykiller/article/details/133972292