Unity
Unity games run their C# on Mono or IL2CPP, which Wraith reads by name. Finding classes, calling methods and keeping objects work the same as in any C# game. This page is what’s only Unity’s.
For the camera, posing, game speed and Unity’s main thread, use the Unity plugin, which builds on everything here.
Engine calls
Section titled “Engine calls”Unity’s engine calls are the extern methods behind things like Transform.position and Time.deltaTime. They’re native code, the same under Mono and IL2CPP, so they’re called directly. In schemas, mark them [Icall]:
[Icall("UnityEngine.Time::get_deltaTime")] f32 deltaTime();
[Managed("UnityEngine.Transform")]struct Transform { [Icall("get_position_Injected")] void position(inout Vec3 at);}Unlike [Managed] methods, Wraith doesn’t convert what they take, so objects and strings are their addresses. Not running yet? Using one fails, saying so, and works once it is.
As you go, managed.icall finds one, and native from "salamander" calls it, given its signature. That’s quicker than invoke, for calls you make every frame.
import { Wraith } from "wraith";import { native } from "salamander";
const deltaTime = native("fn f32 ()", Wraith.managed().icall("UnityEngine.Time", "get_deltaTime"));local Wraith = require("wraith").Wraithlocal sal = require("salamander")local deltaTime = sal.native("fn f32 ()", Wraith.managed():icall("UnityEngine.Time", "get_deltaTime"))Cast the address to a function pointer.
const delta_time: *const fn () callconv(.c) f32 = @ptrFromInt(try ctx.wraith.managed().icall("UnityEngine.Time", "get_deltaTime"));Cast the address to a function pointer, in unsafe code.
var deltaTime = (delegate* unmanaged<float>)Managed.Instance.Icall("UnityEngine.Time", "get_deltaTime");Some engine calls make C# objects, so they need a thread the runtime knows. managed.callNative(address) runs a function that takes nothing and gives back a pointer-sized value on Wraith’s own attached thread and waits for it. It’s still not Unity’s main thread, so keep it to calls that don’t care.
Unity’s objects
Section titled “Unity’s objects”Hold what you keep between frames. Unity can still destroy what you hold, like a character that’s despawned. Its m_CachedPtr field, Unity’s own object, goes to 0 when that happens, so check it before using one on a later frame.
Unity’s main thread
Section titled “Unity’s main thread”Unity’s own methods and engine calls only work on its main thread. With the Unity plugin loaded, that’s where threads.onTick runs, and calls from there run right there. See Game and render threads.
wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
