Hi,
I want to inherit from the Material class, in order to set some material properties for the material upon deserialization/creation.
More precisely I want to call Material.SetFloatArray(), which sets an array of float that will be constant to the material instance and should be persistent i.e. survive serialization.
If I define MyMaterial, which inherits from Material, instantiate it and create an asset, the asset file will contain an object of type Material, and not MyMaterial.
MyMaterial mat = new MyMaterial(some_args);
AssetDatabase.CreateAsset(mat, "mymat.mat");
Greetings
Inheriting simply for the purposes of setting some default data isn’t really a good candidate for using inheritance. Given that you’re using this in the Editor is there any reason you couldn’t just create a menu entry that creates a regular Material and sets the float array accordingly?
Hi, tanks for the quick response.
The problem is, that whenever (for example) the scene is saved (or the editor is relaunched), the assigned float array is discarded in the material.
Our current workaround is, that we have a GameObject in the scene, which has the Material and the float array, and sets the float array in the Update() method.
This does work, however there will be a lot of materials and “real” objects in the scene, and having all these additional objects pollutes the scene browser and one can easily forget to add the additional GameObject to scenes…
Inheriting from Material, (or setting the array somehow persistently) would be an ideal solution, since we do not depend on the additional GameObjects and the material would be selfcontained.
So you’re not using a custom shader? Otherwise the property should just be exposed in the Material’s inspector.
The other option would be to wrap it in a ScriptableObject.
Yes I use a custom shader, but all sources I found online indicate, that FloatArrays are not supported by the Properties section in the shader, and therefore are not shown in the Material’s inspector (and therefore cannot be set in a persistent way).
That’s interesting. I learned something new!
So basically you’re looking to create a custom asset type in which case the general answer from Unity is to use a ScriptableObject. Custom Asset Files - Questions & Answers - Unity Discussions
Will using a texture instead of a float array work?
1 Like
I’m back 
@BoredMrmon:
Thank you, that actually works (and I can also tweak the precision the values are stored :P)
Are there 1d textures and can I sample them at specific pixels?
@KelsoMRK :
Okay, but in which way would I attach my asset to the material, or the other way round.
You can use a texture one pixel wide. Or you can simply convert the 2D coordinates into a 1D index.
The material and the float array would just be fields in your scriptable object.
Sounds like @Kiwasi has a better solution anyway.