• RuntimesUnity
  • Changing spine material at runtime (Unity)

Hello everyone,

My situation is the following (Unity):
we have our character (it has lots of skins), and want to have the character silhouette (with same skin) show when it is behind walls.
I read that we can use the stencil buffer to do this (walls write a number to the stencil buffer and then our character's silhouette material can compare to that number and tell the stencil wether to render it or not).
What I'm afraid is that I would have to duplicate my character so the original material renders normally and the copy as a silhouette.
Spine automatically detects and applies at runtime and at all times the corresponding material to the character and its skin on Unity, so I can't manually change what material to use. If I duplicate the character and make changes to its material at runtime (I'm using spine/sprite shader) for example to tint it red and also to make the stencil comparison it applies to both character and silhouette as they share the same material.
How can I change through code the material of the duplicate without affecting the original character? (both share the same spine material assigned by its skin/atlas/etc)
I'm afraid and somehow sure that its not a real solution to duplicate the whole character folder in order to have a silhouette material for the corresponding skin and the original material.

I hope my question is clear to understand.

  • Misaki님이 이에 답장했습니다.
    Related Discussions
    ...

    @alcyongames Instead of duplicating the whole skeleton, you can use the RenderExistingMesh component to render the same skeleton Mesh (also with a custom material), with minimal overhead.

    This component is used in the com.esotericsoftware.spine.URP-shaders/Examples/Outline Shaders URP example scene of the Spine URP Shaders UPM package to render the identical skeleton behind itself using an outline-only shader.

    2년 후

    I am using this code to replace a specific sprite.
    However, when I create two instances of the same skeleton and switch the sprite, the first skeleton is unexpectedly affected.
    I would like to resolve this issue so that changing the sprite in one instance does not impact the other.

    public void SetAttachment(string slotName, string attachmentName, Sprite sprite = null, float scale = 0)
    {
        var slot = _skeletonAnimation.Skeleton.FindSlot(slotName); 
        var attachment = _skeletonAnimation.Skeleton.Skin.GetAttachment(slot.Data.Index, attachmentName);
        var newAttachment = attachment.GetRemappedClone(sprite, attachment.GetMaterial(), false, false, false, false) as RegionAttachment;
        newAttachment?.SetScale(Vector2.one * scale);
        newAttachment?.UpdateRegion();
        _skeletonAnimation.Skeleton.Skin.SetAttachment(slot.Data.Index, attachmentName, newAttachment);
        slot.SetToSetupPose();
    }

    I suspect that the issue might be caused by shared materials or attachments between instances.
    What would be the best way to ensure that each skeleton instance maintains its own separate sprite replacement?

    @riki Likely you are calling _skeletonAnimation.Skeleton.Skin.SetAttachment on a shared skin, e.g. skeleton.SkeletonData.FindSkin("yourskin") or the default skin.

    To create a skin instance, you can just create a Skin customSkin = new Skin() and call customSkin.AddSkin(..); as shown in the Spine Examples/Other Examples/Mix and Match Skins example scene.

    See also the documentation here:
    https://esotericsoftware.com/spine-unity-main-components#Setting-Skins
    https://esotericsoftware.com/spine-unity-main-components#Combining-Skins

    • riki님이 이에 답장했습니다.

      Harald

      To create a skin instance, you can just create a Skin customSkin = new Skin() and call customSkin.AddSkin(..); as shown in the Spine Examples/Other Examples/Mix and Match Skins example scene.

      Thank you so much, Your explanation was very helpful.

      I was indeed modifying a shared skin, and after creating a new Skin instance and using customSkin.AddSkin(..), everything worked perfectly.

      I really appreciate your support! 🙌