#What's happening under the covers in Component.Transform?

1 messages · Page 1 of 1 (latest)

iron pike
#

Greetings, I'm hoping someone from Unity can clarify something I've been pondering.

In Unity 5, Unity added this optimization:

#

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
dapper hazel
#

Are you decompiling Unity?

iron pike
#

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

iron pike
# dapper hazel Are you decompiling Unity?

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;
}
hollow widget
#

We don't know the specific about how exactly they are caching it

iron pike
hollow widget
#

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.

dapper hazel
hollow widget
dapper hazel
#

I see very little activity from Unity staff here unless its one of the events

iron pike
#

Thanks!