• RuntimesUnity
  • unity spine material Individual allocation

I assigned a custom material to Spine in Unity.
However, when I copy this character and change the value of the material, the values ​​of all copied characters change.
This is because the materials in the characters are the same.

I want each copied character to have its own material. How can I efficiently have each character have their own material? Is there an internal function or C# code provided by Spine?

I think there should be a separate function for situations like mine, but it's hard to find.

Related Discussions
...

To ensure each copied character in Unity has its own material when using Spine, you can use the MaterialPropertyBlock or create unique material instances for each character. Here are the steps to achieve this:

  1. Using MaterialPropertyBlock:
    This method allows you to change material properties for individual instances without creating new material instances. Here's an example of how to use it:

       using UnityEngine;
    
       public class ChangeMaterialProperties : MonoBehaviour {
           private MaterialPropertyBlock propertyBlock;
           private Renderer renderer;
    
           void Start() {
               renderer = GetComponent<Renderer>();
               propertyBlock = new MaterialPropertyBlock();
           }
    
           void Update() {
               propertyBlock.SetColor("_Color", Color.red); // Change the color property
               renderer.SetPropertyBlock(propertyBlock);
           }
       }
  2. Creating Unique Material Instances:
    If you need to create unique material instances for each character, you can duplicate the material for each instance. Here’s how you can do it:

       using UnityEngine;
    
       public class DuplicateMaterial : MonoBehaviour {
           private Renderer renderer;
    
           void Start() {
               renderer = GetComponent<Renderer>();
               renderer.material = new Material(renderer.material); // Create a new instance of the material
           }
    
           void Update() {
               renderer.material.color = Color.red; // Change the color property
           }
       }
  3. Using Spine's CustomMaterialOverride:
    Spine provides a way to override materials per instance. You can use the SkeletonRendererCustomMaterials component to assign custom materials. Here’s an example:

       using Spine.Unity;
    
       public class CustomMaterialOverrideExample : MonoBehaviour {
           public Material customMaterial;
           private SkeletonRendererCustomMaterials customMaterials;
    
           void Start() {
               customMaterials = GetComponent<SkeletonRendererCustomMaterials>();
               customMaterials.customMaterialOverrides.Add(new SkeletonRendererCustomMaterials.SlotMaterialOverride {
                   slotName = "slotName", // Replace with your slot name
                   material = customMaterial
               });
           }
       }

For more detailed information, you can refer to the Changing Materials Per Instance section in the spine-unity documentation.

    Spinebot


    I tried this method and it didn't work, so I did some experiments.

    Before running the game, the same custom slot material was inserted, and when I ran the game and inserted another material into the custom slot material, I found that it had individual materials.

    Can't I use another material this way?

    Using this method, if you could tell me how to assign the materials of the custom slot material individually, I would appreciate it.

      TaeWoong I tried this method and it didn't work

      What did you try, and what exactly did "not work"? What was your expected result and what did you receive instead?

      TaeWoong Before running the game, the same custom slot material was inserted, and when I ran the game and inserted another material into the custom slot material, I found that it had individual materials.

      I'm afraid I don't understand what you mean by the above. Please always describe precisely what materials you assigned where, "materials were assigned" is too ambiguous.

      Regarding "I found that it had individual materials.": If you override the material at one slot only, then the material of this slot is one material, and in addition to the material of the other slot that makes two.

      If you don't want only a single slot being affected and instead replace the material at all slots: you set the Custom Slot Material of the Body slot. As the name suggests, it replaces the material for a single slot. If you want to replace a material entirely, you need to use the Custom Material Overrides below Custom Slot Materials instead.

        Harald

        Let me explain in detail what I want.

        As you can see, there are two characters with the same materials.

        In this situation, if I change the value of "1_Doi_Material(URP)" of the custom slot material, both characters will be affected by the material, and the bodies of both characters will change.

        What I want is for both characters to have their own ""1_Doi_Material(URP)"".

        So, if I change the value of one character's material, the shader of one character's body will change.

        I'm not sure which method is the most correct and the solution.

        Harald

        I got what I wanted.

        Thank you so much for your reply.

        @TaeWoong Glad to hear you've figured it out, thanks for letting us know.