已包含所有依赖仍遇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
相关产品推荐
相关产品推荐

