I know that some of forums found on Reddit, Stack Overflow and even Unity said that we can’t override Transform (no links with animation) because it’s the most basic component in Unity.
If RectTransform can override Transformfor UI GameObjects (Images below show the override), so why we can’t do this in the same way as RectTransform by creating another Transform class ?
Becomes
All i want to do is a complete Transform2D Component that overrides default Transform for 2D projects because we never modify position (except for the Camera) and scale on Z axis. As for the rotation, only the Z axis is used.
Here’s the main idea of Transform2D shown in images below (Custom Inspector will help only after success of overriding) :
Becomes (without custom inspector)
Example of Transform2D class
RectTransform has a way to override Transform for UI GameObjects, so my Transform2D must have a way to override Transform for 2D GameObjects.
Sadly, i don’t know that way.
Transform and RectTransform are only pretty thin C# wrappers for C++ objects and all the important bits are happening in the compiled Unity runtime. There’s no reasonable way to access, modify or extend the native parts of Unity and so there’s no way to replicate what RectTransform is doing.
(Well, except buying a Unity source code license and modifying the C++ parts of Unity, but I assume you wouldn’t be asking the question here if that was an option.)
However, you can create a custom editor for Transform and customize the UI to your liking. This might loose you some functionality or you’ll have to dig into the C# internals to replicate them. I suggest you search on Google or GitHub and you’ll find plenty of custom Transform editors people have created over the years.
I already tried a Custom editor for Transform and it worked, but i don’t like doing this way because i still have to create Vector3s for position, rotation and scale whereas some of values are “useless” (and my Editor class wasn’t very clean). I conclude that Custom Editor is the only way to have a Transform2D inspector.
By the way, I can’t rename Transform to Transform 2D in the inspector. It can be an good option even if we still use Transform in C# scripts, but I think that rename is impossible because inspector will only show main component’s name. (I know FormerlySerializedAsAttribute but it only works on fields)
Thanks for the help, even if the way with C++ can’t be followed.
I hope overriding Unity’s Transform class with custom Transform class will be possible in future releases.
As mentioned it’s just a wrapper around the native side. Most everything in them is to do some handy calculations for us, or points to stuff happening in the C++ side.
Sure they could add an API for us to implement our own transform stuff and… wait, we already have that. It’s called the Transform component!
Thus, if you want your own special transform, just make it its own component and reference the regular Transform component. I’ve done this before with a ‘LocalRigidbody’ when I needed a rigid body that could handle multiple gravity sources.