Custom Texture Class

Hey guys,

I’ve been trying to create a custom texture asset but with no success.
I created a custom class that inherits from Unity’s Texture class.

Something like this:

[Serializable]
public class MyCustomTexture : Texture
{
    /* My Custom properties */
}

But when I instantiate an object from that class It doesn’t come fully initialized. All the base Texture members are uninitialized and pointing to null.
I’m instantiating an object simply using the new operator.
Something in the likes of this:

MyCustomTexture myCustomTexture = new MyCustomTexture();
(...)
AssetDatabase.CreateAsset( myCustomTexture , mytexturePath );

Is there a more proper way to instantiate this object or what I’m trying to do on inheriting from Unity’s Texture class isn’t even possible?

Thank you for any tips you can share.

Are you correctly calling the base constructor from your derived constructor?

Yes, if no constructor is set then the base one is called, which was my case.

Unfortunately I do think I’m not able to inherit from Texture. I’ve stumbled into this post on which another user is trying to inherit from UnityEngine.Object and is having the same issues I am.

The reason if I’m reading correctly is that those classes have a Managed side, which is the one we have access and can modify in C# and an internal Native side.

When we instantiate those objects on our scripts we are only creating the managed side and there’s no way to properly create the native part, making the whole process useless.

Yes, most classes that come with the Unity engine are not meant to be extended. The only two classes derived from UnityEngine.Object you can extend are MonoBehaviour and ScriptableObject (and EditorWindow on the editor script side. Though EditorWindow is actually derived from ScriptableObject). Unity slowly moves over to a more OOP structure. So we might get more and more access to some parts of the engine. Though the main issue is that the actual engine is still written in C++ and you would always need some sort of communication layer between the two worlds-

I’m wondering what kind of custom texture asset you want to implement. For custom assets it’s usually the best to implement a ScriptableObject class which may hold necessary configuration data and then just store the imported / generated native asset alongside (i.e. AddObjectToAsset). In some sense when importing a model a similar thing happens. The actual imported parts are stored as sub assets (the mesh / meshes, the bone structure, animation clips, …).

I guess you use a custom asset importer?

Keep in mind that Unity essentially uses YAML (or a binary equivalent) to actually store and serialize your assets in the asset database. This has some limitations. Almost all of the build-in types (derived from UnityEngine.Object) are actually implemented on the native side and the managed classes are just hollow wrapper classes. So the serialization of those happens on the native C++ side. The documentation has some pages explaining the YAML format as well the build-in object type list with their corresponding type id. (just check the tree view on the left). The only classes where the native serializer actually cares about the fields inside the managed wrapper are MonoBehaviour and ScriptableObject.

@Bunny83 Thank you so much for such a detailed answer. My main reason to be inheriting from Texture is that I wanted to be able to drag and drop my special texture asset into any of Unity’s UI texture fields.

But yes, I’ve come to terms with that idea and returned to my initial approach, which like you referred, is to implement a Scriptable Object.