以时下最流行的多人在线游戏Lethal Company举例,首先。

准备工作

  1. 第一步,先进入Mod开发者的社区寻找资料WIKI。根据文档描述,下载VSStudio2022用作代码编辑器(IDE)。
  2. 然后下载代码反编译器,方便寻找适当的时机插入自己写好的代码。反编译器ILSpy
  3. 然后下载Mod模板,该模板是使用BepInEx5的规则制作的,添加对应的NeGut包,添加游戏的Assembly-CSharp.dll引用。
  4. 最后使用ILSpy打开Lethal Company_Data/Managed/Assembly-CSharp.dll,寻找你需要的模块查看代码。

实战

ILSpy中,假设我想要找小女孩的相关配置文件,在Dll中搜索Girl,可以找到一个叫做DressGirlAI的类,作者将属于她的属性都写在这里。假设我们想要修改小女孩出现的音效,只需要找到对应的方法之前,把音效替换的代码插入即可。
这是一个简单的例子

using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using System.Collections;
using System.Net;
using System.Net.Http;
using UnityEngine;
using UnityEngine.Networking;

namespace ToxicStar.ChangeGirlGhostAudio
{
    [BepInPlugin("ModGUID", "ModName", "ModVersion")]
    public class ToxicStarPlugin : BaseUnityPlugin
    {
        private const string modGUID = "ToxicStar.ChangeGirlGhostAudio";

        private const string modName = "ChangeGirlGhostAudio";

        private const string modVersion = "1.0.0.0";

        private readonly Harmony harmony = new Harmony("ToxicStar.ChangeGirlGhostAudio");

        private static ToxicStarPlugin Instance;

        internal static AudioClip[] newSFX;

        private void Awake()
        {
            if ((Object)(object)Instance == (Object)null)
            {
                Instance = this;
            }
            StartCoroutine(LoadAudio());
        }

        private IEnumerator LoadAudio()
        {
            //寻找路径并加载音频到内存
            var path = $"{Application.dataPath.Substring(0, Application.dataPath.Length - 20)}/BepInEx/plugins/{modGUID}/WinnerWinnerChickenDinner.mp3";
            Logger.LogInfo("音频存放路径:" + path);
            var uwr = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.MPEG);
            yield return uwr.SendWebRequest();
            //转换
            var audioClip = DownloadHandlerAudioClip.GetContent(uwr);
            if (audioClip == null)
            {
                Logger.LogError("加载音频失败");
                yield break;
            }
            //???
            newSFX = new AudioClip[1] { audioClip };
            Logger.LogInfo(newSFX.Length);
            Logger.LogInfo(newSFX[0].name);
            Logger.LogInfo(newSFX[0].ToString());
            harmony.PatchAll(typeof(GhostGirlAudioPatch));
            harmony.PatchAll(typeof(ToxicStarPlugin));
        }
    }

    [HarmonyPatch(typeof(DressGirlAI))]
    internal class GhostGirlAudioPatch
    {
        [HarmonyPatch("Update")]
        [HarmonyPrefix]
        private static void patchM(ref AudioClip ___breathingSFX)
        {
            AudioClip[] newSFX = ToxicStarPlugin.newSFX;
            if (newSFX != null && newSFX.Length >= 0)
            {
                AudioClip val = newSFX[0];
                ___breathingSFX = val;
            }
        }
    }
}

运行之后将bin文件夹下制作好的Dll丢到游戏根目录下BepInEx/plugins文件夹下即可使用。
最后,记得将BenpInEx/config/BepInEx.cfg里的[Logging.Console]下的Enabled属性改为true,方便启动Mod后查看调试信息。

最后修改:2024 年 05 月 16 日
如果觉得我的文章对你有用,请随意赞赏