Home > Net >  Using unity classes outside of unity
Using unity classes outside of unity

Time:07-31

I like unity's rigid body transformation functions and I need them in a C# application that has nothing to do with unity. How can I use the Quaternion class outside of unity?

I've seen this but I'm not sure if it applies to me since I'm still using C#.

CodePudding user response:

Much of Unity's functionality is actually written in C and is just exposed in C# as an internal call. This is Quaternion.Slerp for example:


[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Slerp_Injected(ref Quaternion a, ref Quaternion b, float t, out Quaternion ret);

[FreeFunction("QuaternionScripting::Slerp", IsThreadSafe = true)]
public static Quaternion Slerp(Quaternion a, Quaternion b, float t)
{
    Slerp_Injected(ref a, ref b, t, out var ret);
    return ret;
}

So, it depends which classes you want to use as you'd have to find a pure C# implementation of each - your regular C# application is not going to be able to call internal C Unity functionality if not inside Unity.

Fortunately if it's transform related functionality you're after, there is a built in implementation of Quaternion and Vector3 in the System.Numerics namespace of C# which has much of the same functionality as what is in Unity which might give you what you're after:

using System.Numerics;


var axis = Vector3.UnitX;
var angle = 2f; // This is in radians
Quaternion q = Quaternion.CreateFromAxisAngle(rotAxis, rotAngle);
  • Related