c# - 统一构建错误 : The name 'EditorUtility' does not exist in the current context

标签 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

没用。在构建版本中,它只是不显示消息框,它的行为就好像这一行只是一条评论。有人可以帮忙吗?

最佳答案

您现在出现错误的原因是,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

关于c# - 统一构建错误 : The name 'EditorUtility' does not exist in the current context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56813826/

相关文章:

c# - 哪种方法最好? AutoMapper 反对隐式(C# 引用)

c# - 在 C# 中读取非常大的文本文件的最快方法

c# - 我怎样才能用鼠标围绕播放器旋转相机?

android - 在手机上构建游戏。原生还是引擎?

c# - DateTime.Parse 和 Convert.ToDateTime 之间有什么区别?

c# - 在 Windows Mobile 上访问可用 Wifi 应用程序列表

c# - 如何解决ffmpeg水印 "No such filter: '电影'"AND "avformat_open_input失败?

c# - 我怎样才能使用 linerenderer 绘制一个圆圈,并使它成为圆圈,因为对象的子对象应该围绕它绘制?

c# - 给对象唯一的 ID onStart

c# - 始终在玩家面前显示对象