Unity 出现error CS0103: The name ‘AssetDatabase‘ does not exist in the current context

问题描述

在Unity场景中,在进行build操作时出现这种报错,导致资源bundle无法正常生成,出现以下问题:

error CS0103: The name 'AssetDatabase' does not exist in the current context

error CS0234: The type or namespace name 'AssetDatabase' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)

ps:上面两种错误都是同一种问题造成的,报错不一样的原因是由于UnityEditor在代码中的位置不同造成的:
前者是在开头声明了using UnityEditor,方法中使用AssetDatabase.LoadAssetAtPath;
后者是未声明using UnityEditor,而是在方法中直接使用了UnityEditor.AssetDatabase.LoadAssetAtPath


原因分析

在非Editor文件夹下的脚本中,存在着有关UnityEditor方法的使用

方法中第一行使用了UnityEditor中的AssetDatabase.LoadAssetAtPath方法,并且该方法所在的文件并非是在Editor文件夹下,导致build操作时出现报错

    private void EditorLoadAsset(string assetName, Action<UnityEngine.Object> action)
    {
        UnityEngine.Object obj = UnityEditor.AssetDatabase.LoadAssetAtPath(assetName, typeof(UObject));
        if (obj == null)
            Debug.LogError("asset name is not exist: " + assetName);
        action?.Invoke(obj);
    }

在这里插入图片描述


解决方案

添加
#if UNITY_EDITOR

#endif

在方法外部,加上#if UNITY_EDITOR #endif,保证该方法只有在Unity编辑器中运行

#if UNITY_EDITOR
    private void EditorLoadAsset(string assetName, Action<UnityEngine.Object> action)
    {
        UnityEngine.Object obj = UnityEditor.AssetDatabase.LoadAssetAtPath(assetName, typeof(UObject));
        if (obj == null)
            Debug.LogError("asset name is not exist: " + assetName);
        action?.Invoke(obj);
    }
#endif

注意

注:需要把调用该方法的地方也要用#if #endif包括起来
因为该方法时需要被调用的,然后测试的时候出现了以下问题
error CS0103: The name 'EditorLoadAsset' does not exist in the current context
出现问题的原因是调用此方法的地方未用#if #endif包含进去,在正式运行状态下,他会认为该方法不存在,找不到该方法导出出现报错。所以要将调用该方法的地方也要用#if #endif包括进来,让正式运行状态下也不用执行调用该方法的语句

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值