Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to change Material import settings on a custom script?

Discussion in 'Scripting' started by DrVanillaCookie, Aug 21, 2023.

  1. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    39
    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:

    Code (CSharp):
    1.  
    2. private SpriteRenderer first {
    3.            get {
    4.                if (_first == null && renderers.Length > 0)
    5.                    _first = renderers [0];
    6.                return _first;
    7.            }
    8.  
    9. public Material Material {
    10.             get { return (first != null) ? first.sharedMaterial : null; }
    11.             set { DoForAll (x => x.sharedMaterial = value); }
    12.         }
    Here's the script in its entirety:
    https://github.com/Dharengo/Spriter...ets/Spriter2UnityDX/Runtime/EntityRenderer.cs

    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.
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Not sure what that is, or why the sprite atlases can't be modified after the fact, but a quick google search keeps referring to use spriterdotnet. as one poster mentioned:
    "The creator for spriter2unity(DX) messaged me on github and suggested I not use that extension. He told me to use spriterdotnet."
     
  3. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    39
    I'm pretty sure I've tried spriterdotnet before and it didn't work it all, I'll try it again tomorrow. Looks like it's even older than spriter2unity. Also I'm pretty sure spriter2unity doesn't import as sprite atlases, just separate sprites. Basically it's pretty messy, I've already had a ton of problems with it.

    Anyway, I think I got the cloning working by doing this:
    Code (CSharp):
    1. Material material = GetComponent<EntityRenderer>().Material;
    2. GetComponent<EntityRenderer>().Material = Instantiate(material);
    No idea how bad it is on performance, I guess time will tell.
     
    Last edited: Aug 22, 2023