# UnityMonotor
A unity application for use of monitoring other mono based unity games

The demo is in the `unitymonotor` folder, it can scan all running processes and filter out mono based Unity3D games, and then inject required codes into the game to create a virtual camera for users to inspect the scene without prior knowledge of the game. In `example_game` folder is a scene roaming sample with no special code created by Unity3D engine for testing.

By clicking `Attach` button, two C++ dlls will be injected into the selected game process(Namedpipe.dll and ExMonoInjector.dll in StreamingAssets folder). This is done with the help of Blackbone(memory hacking library) and my own Injector library.

Namedpipe.dll is a helper dll for Apache Thrift(Remote Procedure Call, RPC) for use of Inter-Process Communication(IPC), and ExMonoInjector.dll is another injection library that can use the pre-exist mono.dll inside the process to load other .NET(C#) dll into the target process. With ExMonoInjector, we can then inject other dll, Thrift.dll, SharpDX libraries(SharpDX.dll, SharpDX.Direct3D11.dll and SharpDX.DXGI.dll) and finally ExDetour.dll(contain our code for execution) into our target process.

When ExDetour.dll is being loaded and executed, it will try to create a connection with unitymonotor process by modified Apache Thrift. UnityMonotor main process will create a Direct3D11 shared texture by Unity native plugin and send the shared handle to the target process. The target process can then open the shared texture by SharpDX and render a new camera content to it. The camera's gemometry is also controlled and synchronized by IPC. 

This project can be used for creating Unity plugin without the help of the developers, such as plugins for VR games.

Issues:

1. If you have multiple GPUs, when UnityMonotor and the target process are not rendered by the same GPU, UnityMonotor cannot access the shared texture created by another GPU correctly and will have a memory access violation exception. Make sure they both rendered by same GPU.

2. If you close UnityMonotor before detach, because Unity does not allow the users to control the cleanup sequence, it is possible that both UnityMonotor and the target app will both hang because of a wrong cleanup order. Detach before quit the app to avoid this issue.