Table of Contents

Unity中的程序集定义 Assembly definitions

https://docs.unity3d.com/2021.3/Documentation/Manual/ScriptCompilationAssemblyDefinitionFiles.html

通俗理解:将代码分成多个DLL


如何创建程序集定义

To organize your project code into assemblies, create a folder for each desired assembly and move the scripts that should belong to each assembly into the relevant folder. Then create Assembly Definition assets to specify the assembly properties.
Unity takes all of the scripts in a folder that contains an Assembly Definition asset and compiles them into an assembly, using the name and other settings defined by the asset. Unity also includes scripts in any child folders in the same assembly, unless the child folder has its own Assembly Definition or Assembly Reference asset.

创建一个文件夹,在其中创集一个Assembly Definition assets,在这个assets中可以设定程序集的各种属性,在这个文件夹(包括递归的子文件夹)中的代码都属于这个程序集

内部

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
// 程序集内部
namespace RealMath
{
    // 外部可以访问,Assembly-CSharp.dll (predefined assemblies)默认可以直接访问
    // 自己定义的程序集,默认可以直接访问Unity底层提供的程序集:precompiled (plug-in) assemblies
    public class RealMathAPI
    {
        // 提供给外部程序集的接口
        public static int Add(int x,int y)
        {
            return RealMathHelper.Add(x, y);
        }
    }
 
    // 程序集内部才可以访问
    internal class RealMathHelper
    {
        public static int Add(int x, int y)
        {
            return x - y;
        }
    }
}

外部

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class NeedMath : MonoBehaviour
{
    private void Start()
    {
        // 在Assembly-CSharp.dll中访问自定义的程序集
        int realAnswer = RealMath.RealMathAPI.Add(1, 1);
    }
}
To include scripts from a non-child folder in an existing assembly, create an Assembly Reference asset in the non-child folder and set it to reference the Assembly Definition asset that defines the target assembly. For example, you can combine the scripts from all the Editor folders in your project in their own assembly, no matter where those folders are located.

创建引用资源(Assembly Reference asset)可以将其他文件夹下的代码也包括到此程序集

引用关系

By default, the predefined assemblies reference all other assemblies, including those created with Assembly Definitions (1) and precompiled assemblies added to the project as plugins (2). In addition, assemblies you create with an Assembly Definition asset automatically reference all precompiled assemblies (3)

默认情况下的引用关系: Assembly-CSharp.dll < 自定义程序集 < Unity底层precompiled assemblies

You can control the references between assemblies used in your project using the options of an Assembly Definition.

在Assembly Definition asset中可以设定引用规则,包括以下几种方式(参考上面Assembly Definition asset的截图)

Auto Referenced – Whether the predefined assemblies reference the assembly Assembly Definition References – References to other project assemblies created with Assembly Definitions Override References + Assembly References – References to precompiled (plugin) assemblies No Engine References – References to UnityEngine assemblies