Super specific problem here: I have to import animations (from Spriter) into Unity by using custom Spriter2Unity script. Instead of SpriteRenderer It creates EntityRenderer component, which has similar properties. But here is where problems start, the script is pretty old and doesn’t have material/sharedMaterial thing, it’s all shared. My guess it’s because it has to apply material to all the sprites in the animation.
So I have a custom material (Shader Graph) and some of its properties are changed through code. But since it’s a shared material, all the changes are made to ALL instances, which kinda sucks.
I have found a workaround by instantiating a copy of the material and using that instead. It took me quite a while and isn’t completely done since I’m a massive C# scrub, but it also seems that creating a clone material for every object isn’t ideal.
SO is it possible to modify import settings on the script in a way that a material is still applied to all the sprites on the current object and only to the CURRENT instance of this object? Here’s how import settings on the material look like:
private SpriteRenderer first {
get {
if (_first == null && renderers.Length > 0)
_first = renderers [0];
return _first;
}
public Material Material {
get { return (first != null) ? first.sharedMaterial : null; }
set { DoForAll (x => x.sharedMaterial = value); }
}
Here’s the script in its entirety:
I tried changing first.sharedMaterial to first.material, but as expected the material is applied to only one sprite. And also causes this error in the editor:
“Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead”
I also tried Override Property Declaration in the Shader Graph, doesn’t do anything.
If it’s pretty much impossible, I could use any other advice on how to make it work, like how would you properly instantiate a copy of the material and apply it to the object. Thanks in advance.