You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity安卓打包报错:当前上下文中不存在‘FileUtil’的解决咨询

Fixing "The name 'FileUtil' does not exist in the current context" in Unity Android Builds

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.Combine is 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.persistentDataPath usually doesn't require extra permissions, but double-check scoped storage settings if targeting Android 10+.
  • Editor vs Runtime: Always remember: any class under UnityEditor is for editor tools only. Stick to System.IO or Unity's runtime file utilities (like File/Directory) for code that runs in your published app.

内容的提问来源于stack exchange,提问作者Starboi97

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:17:10