注入Unity mono游戏过程详解
AppNinja
(8条消息) 注入Unity mono游戏过程详解_AppNinja的博客-CSDN博客
1、用 dnspy 查看 Assembly-CSharp.dll 使用的.net框架版本。
如图为.NET4

2、用 vs2010编译.net4的C# 类库工程,编译后生成 UnityPluginDemo.dll
Cheat.cs代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
-
- namespace UnityPluginDemo
- {
- public class Cheat
- {
- static void WriteLog(string content)
- {
- string path = "D:/unityplugin.txt";
- FileStream fs = null;
- if (File.Exists(path))
- {
- fs = new FileStream(path, FileMode.Append, FileAccess.Write);
- }
- else
- {
- fs = new FileStream(path, FileMode.Create, FileAccess.Write);
- }
- StreamWriter sw = new StreamWriter(fs);
- sw.WriteLine(content);
- sw.Close();
- fs.Close();
- }
- public static void Entry()
- {
- WriteLog("FirstUnityPlugin Enter!");
- }
- }
- }
3、使用 SharpMonoInjector.Gui,输入类库信息,注入到目标。查看日志 D:/unityplugin.txt 。

4、调用游戏逻辑类
在UnityPluginDemo工程添加游戏的3个引用:Assembly-CSharp.dll、UnityEngine.CoreModule.dll、UnityEngine.dll。
在Cheat.cs中引用头文件:using UnityEngine;
添加获取游戏对象的代码,查看游戏自己的日志输出。
- public static void Entry()
- {
- WriteLog("FirstUnityPlugin Enter!");
-
- //GameObject player =
- PlayerManager.WriteLog("FirstUnityPlugin hook call PlayerManager");
-
- }
5、使用 MelonLoader install和将UnityExplorer插件放入Mods文件夹查看GameObjects列表。
工具:MelonLoader.Installer.exe、MelonLoader.x86.zip、UnityExplorer.MelonLoader.Mono
6、枚举GameObjects,在GameObjectLabel,实现透视游戏对象。
加载 UnityEngine.IMGUIModule,在OnGUI中实现代码:
- public void OnGUI()
- {
- GUI.Label(new Rect(0, 0, 200, 20), "Hello");
-
- GameObject[] Objs = GameObject.FindObjectsOfType
(); - foreach (GameObject curObj in Objs)
- {
- // 3D坐标转变为屏幕坐标
- Vector3 pos = curObj.transform.position;
- Camera camera = Camera.main;
- // 获取屏幕坐标系
- Vector3 screenPos = camera.WorldToScreenPoint(pos);
- // z是负数,在摄像机的后面,就不需要画了。
- if (screenPos.z >= 0)
- {
- // UGUI坐标系[0,0]在左上角,屏幕坐标系[0,0]在左下角,所以Unity的Y,需要屏幕高-pos.Y
- GUI.Label(new Rect(screenPos.x, Screen.height - screenPos.y, 500, 500), curObj.name + ",ScreenH="+Screen.height.ToString() + ",Y=" + screenPos.y.ToString());
- }
-
- }
-
- }
7、使用dnSpy调试游戏