注入Unity mono游戏过程详解

注入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<GameObject>();
            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调试游戏

dnSpy/dnSpy-Unity-mono: Fork of Unity mono that's used to compile mono.dll with debugging support enabled (github.com)

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity中的mono是一种跨平台解析和运行C#代码的工具,它负责管理C#代码所占用的内存,也被称为mono内存。通过monoUnity可以在不同的平台上运行相同的游戏逻辑代码。C#代码通过mono解析执行,并由mono分配和管理内存。 Unity之所以能够跨平台,是因为它使用了通用中间语言(CIL)。CIL是一种面向对象的语言,它与具体CPU中的寄存器无关,基于堆栈操作。CIL代码在Mono运行时(虚拟机)上运行,可以在任何支持CLI的平台上运行,包括.NET和Mono。这使得Unity可以实现跨平台的能力。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Unity3D - Unity游戏Mono内存管理与泄漏](https://blog.csdn.net/PAL717/article/details/78655299)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Mono简介、Unity的跨平台、Unity程序从编辑到运行。](https://blog.csdn.net/Michaelia_hu/article/details/88198247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AppNinja

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值