Unity安卓打包报错:当前上下文中不存在‘FileUtil’的解决咨询
Hey there! Let's break down why you're hitting this error and how to fix it with a reliable alternative.
The Root Cause
FileUtil is part of the UnityEditor namespace — these classes are only meant to run inside the Unity Editor, not in your compiled Android app. When you build for Android, Unity strips out all Editor-specific code, which is why the method works in the editor but throws an error during packaging. Adding using System.IO; didn't help because you were still trying to call an Editor-only utility.
The Alternative: Use System.IO.Directory
Instead of FileUtil, use the standard .NET Directory class from System.IO — it's fully supported in runtime builds (including Android) and does exactly what you need. Here's how to rewrite your Delete method:
using System.IO; // Keep this using directive, it's required for Directory public void Delete() { P = EventSystem.current.currentSelectedGameObject.transform; // Use Path.Combine to safely build cross-platform paths (avoids slash issues) string targetFolderPath = Path.Combine(Application.persistentDataPath, "ARPortal", P.parent.gameObject.name); // Check if the folder exists before trying to delete it (prevents errors) if (Directory.Exists(targetFolderPath)) { // The second parameter "true" tells it to delete all files/subfolders recursively Directory.Delete(targetFolderPath, true); } P.gameObject.SetActive(false); P.parent.GetChild(4).gameObject.SetActive(true); // Simplified redundant transform access counter = 0; }
Extra Tips
- Path Safety:
Path.Combineis better than string concatenation because it automatically handles platform-specific path separators (like/vs\). - Permissions: For Android, make sure you've enabled necessary file access permissions in Unity's Player Settings. Using
Application.persistentDataPathusually doesn't require extra permissions, but double-check scoped storage settings if targeting Android 10+. - Editor vs Runtime: Always remember: any class under
UnityEditoris for editor tools only. Stick toSystem.IOor Unity's runtime file utilities (likeFile/Directory) for code that runs in your published app.
内容的提问来源于stack exchange,提问作者Starboi97

