Inheritance from Material

Hi, I have a question!

I create my class and inherit it from class Material. And trying to instantiate it as shown in the documentation Unity - Scripting API: Material.Material with a simple call to new

Material material = new Material (Shader.Find(“Standard”)).

At the same time, I get the error: Class couldn’t be created. The script class needs to derive from ScriptableObject.

Questions.

1.Why can a Material be created with this way, because it does not inherits from ScriptableObject?
2.Can I write something to my class so that it can behave like a Material?

Now I have wrapped Material in my ScriptableObject, but the best solution would be direct inheritance.

Many thanks

1 Like

I’m a bit surprised the Material class isn’t sealed and I don’t think you’re supposed to extend it. The whole architecture of Unity isn’t built so much on inheritance but on composition and most engine types are sealed. Material not being sealed might just be an oversight.

In order to extend the functionality of engine types, you need to create a ScriptableObject, MonoBehaviour or custom class that wraps them, like you have done.

3 Likes

Hi, Adrian!

Thank you very much for your answer.

I tried in my head to develop my idea of inheriting from the material class, but as a result I got a non-working architecture with a lot of non effective relationships and links between classes.

A better solution was to use composition, just as you said.

I thank you for your answer and for your attention to my post,

sincerely

Hey, just want to give you a heads-up that due to the way Unity strips unused assets, if you have nothing else in your project that uses this “Standard” shader, Unity will actually strip it out.

Then when the above code line executes, the shader won’t be found and you will get a pink material.

2 Likes

Hi,

I apologize for the delay in replying.

Kurt-Dekker, thank you very much for your advice, I did not know it.

Explicitly, I do not use shader and address it through the parameters _EmissionColor and _Color, but so far he seems to work reliably and I don’t get the pink material:

public Material set(int fragments)
            {
            _materials = new UnityEngine.Material[fragments];

            for (int i = 0; i < fragments; i ++)
                {
                _materials[i] = new UnityEngine.Material(Shader.Find("Standard"));
                _materials[i].SetColor("_Color", _albedo);
                }
        
            return this;
            }
        
        public UnityEngine.Material [] r(int [] texturelength)
            {
            _fields = new Texture2D[texturelength.Length];

            for (int i = 0; i < texturelength.Length; i ++)
                {
                if (texturelength[i] == 0)
                    continue;
            
                _materials[i].EnableKeyword("_EMISSION");
                            
                //Texture2D t = _fields[i] = new Texture2D(texturelength[i], 1);

                _materials[i].SetColor("_EmissionColor", _albedo);
                _materials[i].SetTexture("_EmissionMap", _fields[i] = new Texture2D(texturelength[i], 1));
                }

            return _materials;
            }

sincerely

Yeah, since it’s the standard shader, you probably have used it a ton of places, so you’re good.

If you had a very special shader that was only used though instantiating it from script, then Kurt’s issue would apply.