【问题标题】:UNITY - The name `AssetDatabase' does not exist in the current contextUNITY - 当前上下文中不存在名称“AssetDatabase”
【发布时间】:2019-04-02 16:44:54
【问题描述】:

我在尝试使用 unity 构建游戏时收到以下错误:

名称AssetDatabase在当前上下文中不存在

任何人都可以帮忙..我会很感激我能得到的所有帮助..谢谢

构建完成,结果为“失败” UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()

UnityEditor.BuildPlayerWindow+BuildMethodException:5 个错误
在 UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer(BuildPlayerOptions 选项)[0x0021f] 在 C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:187
在 UnityEditor.BuildPlayerWindow.CallBuildMethods (Boolean askForBuildLocation, BuildOptions defaultBuildOptions) [0x0007f] 在 C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:94 UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    似乎在您的一个脚本中使用了属于UnityEditor 命名空间的AssetDatabase。它仅存在于 Unity 编辑器中。

    在构建中,命名空间 UnityEditor 不存在 -> 您不能在构建的应用程序中使用它的任何内容!


    因此,在意识到这一点之后,基本上有解决方案如何在使用时修复这些错误,例如自定义编辑器脚本或一些仅存在于 Unity 编辑器内部但不存在于构建中的代码块:

    1. 要么确保所有编辑器脚本的脚本都放在名为Editor 的文件夹中。 Unity 会自动排除构建中的那些。

    2. 或将#if pre-processorUNITY_EDITOR 一起使用:

      #if UNITY_EDITOR
          using UnityEditor;
      #endif
      
      ...
      
      #if UNITY_EDITOR
          //some code here that uses something from the UnityEditor namespace
      #endif
      

    【讨论】:

      【解决方案2】:

      使用下面的代码,它会正常工作。确保您的文件位于“Resources”文件夹中。添加不带文件扩展名的名称。此解决方案适用于所有情况。

      Object[] GetSprites(string fileName)
      {
          Object[] sprites = Resources.LoadAll(fileName);
          return sprites;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-02
        • 2014-04-22
        • 2017-06-17
        • 2015-09-11
        • 1970-01-01
        相关资源
        最近更新 更多