#What's happening under the covers in Component.Transform?
1 messages · Page 1 of 1 (latest)
If you decompile the transform property you see that it is a wrapper for an external C++ native code GetTransform() method.
So I'm wondering what exactly is happening in that GetTransform() method.
You can see this in this IL snippet that rotates a transform:
.method public hidebysig
instance void MyMethod2 () cil managed
{
// Method begins at RVA 0x2826
// Code size 27 (0x1b)
.maxstack 8
// base.transform.Rotate(0f, 0f, 0f);
IL_0000: ldarg.0
IL_0001: call instance class [UnityEngine.CoreModule]UnityEngine.Transform [UnityEngine.CoreModule]UnityEngine.Component::get_transform()
IL_0006: ldc.r4 0.0
IL_000b: ldc.r4 0.0
IL_0010: ldc.r4 0.0
IL_0015: callvirt instance void [UnityEngine.CoreModule]UnityEngine.Transform::Rotate(float32, float32, float32)
// }
IL_001a: ret
} // end of method ForIL::MyMethod2
Are you decompiling Unity?
So my question is: What exactly is happening in GetTransform()? I am assuming that if Unity is caching the transform component "on the c# side" that means they've made a copy of a struct or something? It seems that everytime we access the transform we are making a C++ native call to get whatever this "cached" object is? Are they marshaling a struct into a C# transform?
Help me understand what magic is happening here 🙂
Thanks
What? No! That is the IL from a C# method call that rotates a transform. However, if you hover over the transform property in your IDE and hit F12, it will show you the decompiled C# code for the transform property:
/// <summary>
/// <para>Base class for everything attached to GameObjects.</para>
/// </summary>
[RequiredByNativeCode]
[NativeClass("Unity::Component")]
[NativeHeader("Runtime/Export/Scripting/Component.bindings.h")]
public class Component : Object
{
/// <summary>
/// <para>The Transform attached to this GameObject.</para>
/// </summary>
public extern Transform transform { [FreeFunction("GetTransform", HasExplicitThis = true, ThrowsException = true), MethodImpl(MethodImplOptions.InternalCall)] get;
}
That quote is either just outdated or incorrect. It's cached on the native side.
We don't know the specific about how exactly they are caching it
That's what I thought but I'm trying to get confirmation because I product Unity game dev tutorials and I don't want to publish inaccurate information. I'm hoping someone from Unity will chime in on this.
This would have been a good question to ask on the last Dev Blitz Day. Outside of those, it's unlikely you're going to get an answer from anyone at Unity.
My bad for jumping into conclusions. But yeah that ^ and you could ask in the forums as well so it doesn't get lost in here
There's a discussion about this, including that quote, here:
https://forum.unity.com/threads/cache-transform-really-needed.356875/
I see very little activity from Unity staff here unless its one of the events
Thanks!