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

INT*_*DAN 4 c# unity-game-engine

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

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

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

EditorUtility.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");
Run Code Online (Sandbox Code Playgroud)

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

using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEditor;
Run Code Online (Sandbox Code Playgroud)

有谁能够帮助我?先感谢您。

编辑:做这个代码:

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

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

小智 5

您现在出现错误的原因是,Unity 在为其设计的编译时删除了“UnityEditor”命名空间。这就是为什么当您尝试在平台上使用它时,“EditorUtility”将永远不会存在于除 UnityEditor 之外的任何平台上。因为“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
Run Code Online (Sandbox Code Playgroud)