modifying resources at runtime is not discouraged in general, it's fully supported! What I meant was just that usually it's not what you want
I think we solved this in the chat, but just for the public: Instead of using a Material
for rendering directly, then modifying it at runtime and ending up with changes in the editor, you could use a BatchInfo
object that was created from that Material and modify this one instead.
- BatchInfo: Describes how a batch of vertices is rendered.
- Material: A BatchInfo that is persistently stored as a resource.
Instead of using the Material directly, you can always retrieve material.Info
to generate a BatchInfo with the exact same settings that you can modify without affecting the Material it was created from. Store it somewhere and use it for rendering instead of the Material itself. The usual drawing APIs accept both BatchInfo
If you're only using predefined renderers such as SpriteRenderer
and others, you'd simply dragdrop the Material to their CustomMaterial
(BatchInfo) slot instead of the SharedMaterial
(Material) slot, creating a local copy of that Material just for this single renderer instance. Or you'll make the switch at runtime in some init method, so your changes to the Material will still be propagated to all objects using it.