top of page
Search

Material Instancing for Variation in Similar Elements

Updated: Jan 29, 2022

Problem - you've got a sweet material effect for a repeated element but it looks janky when it's doing the exact same thing as it's neighbours.



Solution - Material instancing! We'll make a quick script that creates an instance of the material when the object is loaded and add some variation to each instance.



In this instance I've opted to access the material that is on the same game object and create a new copy of it. Then I adjust the material's properties and reassign it to the image.


This will work for many applications but if your material needs to be inside a mask you may run into issues - particularly if the properties need to be updated repeatedly instead of just on initialization. If you look at the object while the game is running you'll notice the material is greyed out, uneditable and if you were to cache the material reference for future use - updates to it would simply not work.


It turns out that Unity creates a new material behind the scenes that adds in stenciling when the texture needs to be masked so we need to make sure we're referencing the correct material. In this case accessing the .materialForRendering when masked instead of .material will give us the result we're looking for.



No more repetition!

コメント


bottom of page