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

Unity构建报错:UnityEditor.SceneAsset不可访问,Addressables场景引用异常

解决Unity Addressables SceneReference打包编译失败问题

问题根源

  • SceneAsset属于UnityEditor命名空间,仅在编辑器环境中可用,无法在Player构建的运行时环境中找到该类型
  • 工具程序集引用UnityEditor.CoreModule后,编辑器中能正常工作,但打包时Unity会排除所有编辑器专属程序集,导致代码中引用的SceneAsset无法解析,触发编译错误,进而引发构建系统异常

解决方案:拆分编辑器与运行时代码

将场景引用的逻辑拆分为运行时兼容类和编辑器约束类,避免在运行时代码中引用编辑器专属类型。

1. 创建运行时SceneReference类

将以下代码放在非编辑器专属的程序集(如你的工具程序集)中,确保该程序集未引用任何UnityEditor相关模块:

using System;
using UnityEngine;
using UnityEngine.AddressableAssets;

[Serializable]
public sealed class SceneReference : AssetReference
{
    public SceneReference(string guid) : base(guid) { }

    // 仅在编辑器中验证资源类型,运行时跳过
    public override bool ValidateAsset(Object obj)
    {
#if UNITY_EDITOR
        return obj is UnityEditor.SceneAsset;
#else
        return true;
#endif
    }
}

2. 创建编辑器专属属性抽屉

将以下代码放在Editor目录或编辑器专属程序集中(Assembly Definition需勾选"Editor Only"):

using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.GUI;
using UnityEngine;

[CustomPropertyDrawer(typeof(SceneReference))]
public class SceneReferenceDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var guidProperty = property.FindPropertyRelative("m_Guid");
        // 限制仅能拖入SceneAsset类型资源
        AddressableAssetGUI.DrawAssetReferenceField(position, label, guidProperty, typeof(SceneAsset), false);
    }
}

3. 清理程序集引用

  • 打开工具程序集的Assembly Definition Asset,移除所有UnityEditor相关的引用
  • 确保编辑器专属代码所在的程序集仅在编辑器环境中生效

额外说明

  • 原错误中的Backend exited with code 139是编译错误引发的连锁问题,解决编译错误后该异常会自动消失
  • 这种拆分方式既保留了编辑器中仅允许拖入场景的约束,又保证了运行时代码的兼容性,符合Unity的程序集分离规范

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 20:45:53