Unity打包报错:当前上下文不存在‘EditorUtility’名称
Hey there, let's break down your problem and fix it step by step!
The Root Cause
The error you saw (CS0103: The name 'EditorUtility' does not exist in the current context) and the missing popup in builds both stem from the same fact: EditorUtility.DisplayDialog is an editor-only API.
This method lives in the UnityEditor namespace, which is completely stripped out when you build your game. When you wrapped the code in #if UNITY_EDITOR, you fixed the compile error—but that also tells Unity to exclude this code from builds entirely. That's why your build acts like the line doesn't exist!
Solutions: Replace with Runtime-Compatible Popups
Since editor APIs don't work in builds, you need to use a runtime-friendly way to show popups. Here are two reliable options:
1. Custom UI Popup (Recommended, Cross-Platform)
This is the most flexible approach that works on all platforms. You'll create a reusable UI popup prefab:
- Step 1: Build the Popup UI
Create a new Canvas, add a Panel (for the popup background), Text elements for the title/content, and a Button for confirmation. Save this as a prefab (e.g.,GameDialogPrefab). - Step 2: Add a Dialog Manager Script
This script will handle instantiating and showing your popup:using UnityEngine; using UnityEngine.UI; public class DialogManager : MonoBehaviour { public static DialogManager Instance; [SerializeField] private GameObject dialogPrefab; [SerializeField] private Transform canvasTransform; private void Awake() { Instance = this; } public void ShowDialog(string title, string content, string buttonText, System.Action onButtonClicked) { GameObject dialogInstance = Instantiate(dialogPrefab, canvasTransform); Dialog dialog = dialogInstance.GetComponent<Dialog>(); dialog.SetupDialog(title, content, buttonText, onButtonClicked); } } // Attach this to your popup prefab public class Dialog : MonoBehaviour { [SerializeField] private Text titleText; [SerializeField] private Text contentText; [SerializeField] private Button confirmButton; public void SetupDialog(string title, string content, string buttonText, System.Action onButtonClicked) { titleText.text = title; contentText.text = content; confirmButton.GetComponentInChildren<Text>().text = buttonText; confirmButton.onClick.AddListener(() => { onButtonClicked?.Invoke(); Destroy(gameObject); }); } } - Step 3: Replace the EditorUtility Call
Swap out your original code with this runtime-compatible call:// Instead of EditorUtility.DisplayDialog DialogManager.Instance.ShowDialog("Great!", "You got the pattern right!", "Next Level!", () => { // Add your post-popup logic here (e.g., load next level) LoadNextLevel(); });
2. Platform-Specific Native Popups (Limited Use)
If your game targets a single platform (like Windows standalone), you can use native system popups. Note this isn't cross-platform:
- For Windows builds, you can use
System.Windows.Forms.MessageBox(set your PlayerSettings API Compatibility Level to .NET Framework first):
This will show a native Windows message box, but won't work on Mac, mobile, or other platforms.// Add platform-specific directives #if !UNITY_EDITOR && UNITY_STANDALONE_WIN using System.Windows.Forms; #endif // In your logic #if UNITY_EDITOR // Keep using EditorUtility for editor testing EditorUtility.DisplayDialog("Great!", "You got the pattern right!", "Next Level!"); #elif UNITY_STANDALONE_WIN MessageBox.Show("You got the pattern right!", "Great!", MessageBoxButtons.OK); LoadNextLevel(); // Run your post-popup logic here #endif
Final Note
Always remember: anything in the UnityEditor namespace is for editor use only. When building your game, Unity removes all editor-specific code, so you need to replace those calls with runtime alternatives for your builds to work as expected.
内容的提问来源于stack exchange,提问作者INTODAN

