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

Unity安卓项目集成Stockfish引擎启动时遇Win32Exception问题求助

解决Unity安卓项目中启动Stockfish引擎的Win32Exception问题

我之前在Unity安卓项目里集成Stockfish时也踩过类似的坑,结合你的报错信息和代码,给你几个排查和解决的方向:

1. 优先检查文件权限与完整性

安卓系统中,即使文件存放在persistentDataPath目录下,默认也没有可执行权限,这是这类问题最常见的诱因。另外注意你报错信息里的路径有个异常空格:Stockfish-9- arm64v8(9-后面多了空格),先确认文件名是否正确,有没有复制或命名时的拼写失误。

给二进制文件添加可执行权限

在启动进程前,先通过Android原生API给文件设置755权限:

string stockfishPath = Path.Combine(Application.persistentDataPath, "Stockfish-9-arm64v8");
// 先确认文件是否存在
if (File.Exists(stockfishPath))
{
    // 调用Android的File类设置可执行权限
    using (AndroidJavaObject file = new AndroidJavaObject("java.io.File", stockfishPath))
    {
        bool isPermissionSet = file.Call<bool>("setExecutable", true, false);
        Helper.PrintString("设置可执行权限结果: " + isPermissionSet);
    }
}
else
{
    Helper.PrintString("未找到Stockfish二进制文件,路径: " + stockfishPath);
}

2. 修正ProcessStartInfo的启动配置

安卓上直接启动二进制文件可能需要通过系统shell包装执行,而不是直接指定文件名。你可以调整启动参数,用sh来调用Stockfish:

ProcessStartInfo si = new ProcessStartInfo()
{
    FileName = "/system/bin/sh",
    Arguments = $"-c \"{stockfishPath}\"",
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    WorkingDirectory = Application.persistentDataPath // 显式设置工作目录
};

3. 验证二进制架构与设备匹配

你使用的是Stockfish-9-arm64v8,要确保测试设备是arm64-v8a架构的。如果用的是x86/x86_64模拟器,必须替换为对应架构的Stockfish二进制文件,否则会因架构不兼容启动失败。

4. 额外调试技巧

  • 打印persistentDataPath的完整路径,通过adb手动检查文件状态:
    adb shell ls -l /storage/emulated/0/Android/data/com.chessmania.chess/files/
    
    正常权限应该显示为-rwxr-xr-x,其中x代表可执行权限。
  • 捕获更详细的Win32Exception信息:
    catch (Win32Exception e)
    {
        Helper.PrintString("Win32错误代码: " + e.ErrorCode);
        Helper.PrintString("原生错误代码: " + e.NativeErrorCode);
    }
    

按照这些步骤排查,应该能解决启动失败的问题。我当初就是因为漏了设置可执行权限卡了好几天,加上shell包装启动后就正常运行了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:31:13