Unity Build错误:当前上下文中不存在名称“ EditorUtility”

时间:2019-06-28 22:44:47

标签: c# unity3d

我有一个非常简单的问题。尝试统一构建游戏时出现构建错误。当我在编辑器中运行游戏时,它运行正常。这是控制台中显示的错误。

Assets\Scripts\Pattern.cs(284,17): error CS0103: The name 'EditorUtility' does not exist in the current context

现在,这是错误所引用的代码行:

EditorUtility.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");

最后,如果您认为错误是我没有将正确的内容导入脚本,那么您是错误的,因为我导入了以下内容:

using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEditor;

有人可以帮助我吗?预先谢谢你。

编辑: 执行此代码:

#if UNITY_EDITOR
            EditorUtility.DisplayDialog("Meh", "You got the pattern wrong ):", "Try Again!");
#endif

不起作用。在构建版本中,它只是不显示消息框,并且它的作用就好像这行只是一条注释。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

现在出现错误的原因是,Unity在编译时针对其设计的名称空间删除了“ UnityEditor”名称空间。这就是为什么当您尝试在平台上使用它时,除UnityEditor外,“ EditorUtility”将永远不会存在于任何平台上。因为“ EditorUtility”位于“ UnityEditor”命名空间中。

因此,如果您要执行与使用“ EditorUtility”在Unity编辑器中所做的相同的工作,则应按自己的方式实现。

#if UNITY_EDITOR
    EditorUtility.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");
#else
    YOUROWNCLASS.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");
#endif

答案 1 :(得分:0)

UnityEditor内容通常仅可在编辑器中使用,因为在将游戏构建为独立EXE时,您不能使用它。

如果您实际上是在为编辑器编译游戏,请尝试将预处理器指令添加到中,以包含与编辑器相关的内容。

#if UNITY_EDITOR
    EditorUtility.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");
#endif

您还需要在#if UNITY_EDITOR ... #endif行周围放置相同的using UnityEditor;指令,就像@Retired Ninja在评论中告知我们的那样。