You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity单元测试中Missing Reference Error的解决方法求助

Unity单元测试引用脚本报错,添加Assembly Definition后业务脚本丢失库引用的高效解决办法

问题背景

在Unity中编写NUnit单元测试时,测试脚本无法引用业务脚本MyScript,出现CS0246类型未找到错误。参考方案在Scripts目录添加Assembly Definition后,测试引用问题解决,但原业务脚本出现46个类似The type or namespace name 'TMPro' could not be found的引用丢失错误,逐个添加引用效率极低,寻求高效解决方法。

项目结构

├── Assets
├── Scenes
├── Scripts
│   └── MyScript.cs
│   └── NewAssembly.asmdef
├── Tests
│   ├── MyScriptTest.cs
│   └── Tests.asmdef

相关代码与环境

环境

Unity 2020.3.37f1,Visual Studio 2019(版本16.11.20)

Tests.asmdef(Unity自动生成)

{
    "name": "Tests",
    "rootNamespace": "",
    "references": [
        "UnityEngine.TestRunner",
        "UnityEditor.TestRunner"
    ],
    "includePlatforms": [
        "Editor"
    ],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": true,
    "precompiledReferences": [
        "nunit.framework.dll"
    ],
    "autoReferenced": false,
    "defineConstraints": [
        "UNITY_INCLUDE_TESTS"
    ],
    "versionDefines": [],
    "noEngineReferences": false
}

MyScriptTest.cs

using NUnit.Framework;
public class MyScriptTest
{
    [Test]
    public void SomeTest()
    {
        var obj = new MyScript(); // Error CS0246  The type or namespace name 'MyScript' could not be found
    }
}

MyScript.cs

using UnityEngine;
using TMPro;
public class MyScript : MonoBehaviour {}

已尝试方案

在Scripts目录创建Assembly Definition,解决了测试脚本的引用错误,但业务脚本大量丢失外部库(如TMPro)和自定义类的引用,逐个添加引用不可行。

高效解决办法

1. 修复业务Assembly的引用配置

当创建NewAssembly.asmdef后,Unity默认关闭了自动引用,导致业务脚本无法获取引擎和第三方库的默认引用。修改NewAssembly.asmdef:

{
    "name": "NewAssembly",
    "rootNamespace": "",
    "references": [],
    "includePlatforms": [],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": false, // 关闭覆盖引用,继承Unity默认引用集合
    "precompiledReferences": [],
    "autoReferenced": true, // 开启自动引用,Unity会自动关联引擎、已导入第三方库
    "defineConstraints": [],
    "versionDefines": [],
    "noEngineReferences": false
}

修改后业务脚本会自动恢复TMPro等库的引用,无需手动添加。

2. 让测试Assembly关联业务Assembly

修改Tests.asmdef,在references数组中添加业务Assembly的名称(即NewAssembly):

{
    "name": "Tests",
    "rootNamespace": "",
    "references": [
        "UnityEngine.TestRunner",
        "UnityEditor.TestRunner",
        "NewAssembly" // 新增业务Assembly引用
    ],
    "includePlatforms": [
        "Editor"
    ],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": true,
    "precompiledReferences": [
        "nunit.framework.dll"
    ],
    "autoReferenced": false,
    "defineConstraints": [
        "UNITY_INCLUDE_TESTS"
    ],
    "versionDefines": [],
    "noEngineReferences": false
}

测试脚本即可正常引用MyScript。

3. 批量管理多Assembly引用(多业务Assembly场景)

如果项目拆分了多个业务Assembly,无需逐个添加到测试Assembly:

  • 右键Tests目录 → Create → Assembly Reference
  • 在弹出的窗口中勾选所有需要测试的业务Assembly,Unity会自动维护引用关系,后续新增业务Assembly只需勾选即可。

4. 取消不必要的Assembly拆分(中小型项目首选)

如果项目没有代码隔离需求,直接删除NewAssembly.asmdef,修改Tests.asmdef让其自动引用Unity默认的业务Assembly(Assembly-CSharp):

{
    "name": "Tests",
    "rootNamespace": "",
    "references": [
        "UnityEngine.TestRunner",
        "UnityEditor.TestRunner"
    ],
    "includePlatforms": [
        "Editor"
    ],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": false, // 关闭覆盖引用
    "precompiledReferences": [],
    "autoReferenced": true, // 开启自动引用,会自动关联Assembly-CSharp
    "defineConstraints": [
        "UNITY_INCLUDE_TESTS"
    ],
    "versionDefines": [],
    "noEngineReferences": false
}

这种方式无需手动维护任何Assembly引用,直接解决测试脚本的引用问题,同时业务脚本的原有引用不受影响,适合大多数中小型项目。

内容的提问来源于stack exchange,提问作者tturbo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 23:25:19