首先找一个Unity游戏,本文使用Hollow knight(空洞骑士)作为案例,
第一步老样子先分析Assembly-CSharp.dll
Dnspy
搜索attack查看关键词可以找到
右键分析一下在哪被赋值和使用
首先一个if判断了是否装备了叫什么快速劈砍的护符,装备了就用ATTACK_COOLDOWN_TIME_CH的值,没装就是ATTACK_COOLDOWN_TIME;所以我们这里改ATTACK_COOLDOWN_TIME或者直接attack_cooldown都是可以的;下面那些就是攻击的方向逻辑了
BepinEx
在github下载BepinEx [Releases · BepInEx/BepInEx] 安装到游戏以加载我们写的dll
下载后打开将winhttp.dll,doorstop_config.ini,BepInEx两个文件加一个文件夹解压缩到游戏根目录
打开游戏BepinEx目录下出现LogOutput.log即加载成功
Visual Studio
打开Visual Studio创建一个类库项目,
先添加引用,将游戏Hollow Knight\hollow_knight_Data\Managed目录下的Assmbly-Csharp和一些UnityEngine库都加上,然后再加上Hollow Knight\BepInEx\core的dll
using BepInEx;
using GlobalEnums;
using HarmonyLib;
using UnityEngine;
[BepInPlugin(pluginGuid, pluginName, pluginVersion)]
public class AttackMod : BaseUnityPlugin
{
private const string pluginGuid = "com.attack.test";
private const string pluginName = "AttackMod";
private const string pluginVersion = "1.0.0";
private readonly Harmony _harmony = new Harmony(pluginGuid);
private void Awake()
{
_harmony.PatchAll();
Logger.LogInfo("攻击Mod加载成功!");
}
}
[HarmonyPatch(typeof(HeroController))]
class HeroControllerPatches
{
[HarmonyPatch("DoAttack")]
[HarmonyPostfix]
static void DoAttackPostfix(HeroController __instance)
{
var traverse = Traverse.Create(__instance);
traverse.Field("ATTACK_COOLDOWN_TIME").SetValue(0.1f);
traverse.Field("ATTACK_COOLDOWN_TIME_CH").SetValue(0.01f);
}
[HarmonyPatch("CanAttack")]
[HarmonyPostfix]
static void CanAttackPostfix(HeroController __instance, ref bool __result)
{
var traverse = Traverse.Create(__instance);
float attackCooldown = traverse.Field("attack_cooldown").GetValue<float>();
__result = attackCooldown <= 0f
&& !__instance.cState.dead
&& !__instance.cState.hazardDeath
&& !__instance.cState.hazardRespawning
&& !__instance.controlReqlinquished
&& __instance.hero_state != ActorStates.no_input
&& __instance.hero_state != ActorStates.hard_landing
&& __instance.hero_state != ActorStates.dash_landing;
}
}
语法这里就不多讲了,可以看看harmonylib的文档,如果目标是private就要使用traverse来访问资源,比如private void DoAttack,所以用traverse,我这里修改了ATTACK_COOLDOWN_TIME发现没生效
然后发现是这个方法限制了,所以我这里还重写了CanAttack方法,将attacking和dashing的判断删除了
Ctrl+B编译,将dll拖进BepinEx的plugin目录查看log显示加载成功,修改生效