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

已包含所有依赖仍遇FileNotFoundException,如何解决?

Unity模组开发FileNotFoundException排查与修复

输出日志

开发Unity游戏模组时反复触发FileNotFoundException,已添加0Harmony、Assembly-CSharp、BepInEx系列及UnityEngine核心DLL引用,确认方法/变量名无误,但模组始终无法通过BepInEx正常加载。

现有代码

Plugin.cs

private readonly Harmony harmony = new Harmony(modGUID);

private static InfiniteSprintMod? Instance;

internal ManualLogSource? logSrc;

void Awake()
{
   if (Instance == null)
   {
     Instance = this;
   }
   logSrc = BepInEx.Logging.Logger.CreateLogSource(modGUID);
   logSrc.LogInfo("Infinite Sprint Mod by th3wst is working");

   harmony.PatchAll(typeof(PlayerControllerPatch));
}

PlayerControllerPatch.cs

class PlayerControllerPatch
{
   [HarmonyPatch("Update")]
   [HarmonyPatch("FixedUpdate")]
   [HarmonyPostfix]
   private static void InfiniteSprintPatch(PlayerController ___instance)
   {
     if (___instance.sprinting)
     {
       ___instance.EnergyCurrent = ___instance.EnergyStart;
     }
     else if (___instance.Sliding)
     {
       ___instance.EnergyCurrent = ___instance.EnergyStart;
     }
   }
}

排查与修复方案

  • 确认DLL引用的准确性
    所有引用的DLL必须从目标游戏的BepInEx/core、BepInEx/plugins或游戏自身Managed文件夹直接复制,禁止使用不同版本的第三方下载文件。同时右键引用DLL→属性,将「复制本地」设为True,确保编译后依赖DLL随模组输出。

  • 修正Harmony补丁的目标定位
    当前补丁仅指定方法名,未明确目标类,Harmony无法精准定位到PlayerController的方法。需修改补丁特性:

    [HarmonyPatch(typeof(PlayerController), "Update")]
    [HarmonyPatch(typeof(PlayerController), "FixedUpdate")]
    

    若PlayerController属于特定命名空间,需添加对应using语句或使用全限定类名。

  • 补全BepInEx插件元数据
    Plugin.cs中的主类必须标记[BepInPlugin]特性,这是BepInEx识别插件的必要条件,示例:

    [BepInPlugin(modGUID, modName, modVersion)]
    public class InfiniteSprintMod : BaseUnityPlugin
    {
        // 现有代码...
    }
    

    需确保modGUID、modName、modVersion是已定义的有效字符串。

  • 验证编译输出结构
    编译后将模组DLL直接放到游戏的BepInEx/plugins目录下,不要嵌套子文件夹;检查输出目录是否包含所有依赖DLL,避免缺失导致加载失败。

  • 查看详细错误日志
    打开游戏根目录BepInEx文件夹下的LogOutput.log,日志会明确标注缺失的文件名,针对性补充或替换该文件即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 03:20:57