Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity构建错误:名称';EditorUtility';在当前上下文中不存在_C#_Unity3d - Fatal编程技术网

C# Unity构建错误:名称';EditorUtility';在当前上下文中不存在

C# Unity构建错误:名称';EditorUtility';在当前上下文中不存在,c#,unity3d,C#,Unity3d,我有一个非常简单的问题。当我试图在unity中构建游戏时,我有一个构建错误。当我在编辑器中运行游戏时,它运行得很好。这是控制台中显示的错误 Assets\Scripts\Pattern.cs(284,17): error CS0103: The name 'EditorUtility' does not exist in the current context 现在,这是错误引用的代码行: EditorUtility.DisplayDialog("Great!", "You got the p

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

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

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

UnityEditor
内容通常只能在编辑器中使用,例如,在将游戏构建为独立的EXE时,您不能使用它

如果你真的在为编辑器编译游戏,尝试添加预处理器指令,只包含与编辑器相关的内容

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

如果UNITY编辑器#endif使用UnityEditor围绕您的
执行指令行,正如@Retired Ninja在评论中告诉我们的那样。

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

所以,如果您想使用“EditorUtility”在Unity editor中完成相同的工作,您应该像他们一样实现它

#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

在运行时代码中不能使用编辑器类。将脚本移动到编辑器文件夹。您确实需要将ifdef放在using周围。但是我应该提到:从文本的外观来看,这应该是一个面向玩家的对话框
EditorUtility.DisplayDialog()
仅在处于编辑器模式时才起作用,因此它对实际玩游戏的任何人都没有用处。您应该考虑使用Unity UIThanks@RetiredNinja制作自己的对话框@这正是预处理器指令的想法,它使这些行在游戏过程中根本不会出现(除非你在编辑器中玩)。当您不在编辑器中时,您不能使用
EditorUtility
,您需要找到其他方式来显示对话框,unfortunately@INTODAN虽然这个答案比另一个答案早两个小时添加,但你接受这个答案而不是这个答案有什么特殊原因吗?答案不是早两个小时就回答了吗?