• RuntimesUnity
  • is it possible to 'convert' a Skin into a Sprite?

  • Изменено

Hi! Let me explain what I meant by that weird title: can I use a Skin, in some way, to render that skin image? I want to do something that functions like so:

public Image img;

img.sprite = //i want the sprite of this image to be what's rendered in a Skin I have.

I know I can just do it like:

skeletonGraphic.initialSkinName = "mySkin"

but then I'd have to have multiple SkeletonGraphic components in my scene, just to render separate skins that I have in my skeletonData. Whereas I just want to show the skins separately in the scene, with no animation nor anything like that, just the skin image.
As a beginner in the spine-unity workflow, I'd really really appreciate any bit of advice and help on that matter!!

P.S: I'm using Spine's own atlas packing, not Unity's.
P.S²: My skins are made out of multiple attachments.

Related Discussions
...
  • Изменено

Hi and welcome to Spine! 🙂

Do you want to display the complete atlas texture as you see it in the Project panel? If so, then you can use normal means to either change the Texture Type in the Texture Importer settings to Sprite (2D and UI) and set the Sprite Mode to e.g. Single. Then you can assign the sprite at a SpriteRenderer as usual. Please note that by default, Spine will pack your Attachments to atlas pages independently from which Skin they are used by. So you could have e.g. five skins and all attachments fit on two atlas pages.

If you don't need it to be a Sprite specifically, you can also assign the Material for the desired atlas texture page at a Quad object. Note however that the size of the quad will not be adjusted automatically if it's a non-square atlas texture (like 512x256).

If you want to iterate over all atlas pages (not only actively used pages, which you could get via meshRenderer.Materials), you can use the following code:

AtlasAssetBase[] atlasAssets = skeletonAnimation.SkeletonDataAsset.atlasAssets;
for (int i = 0; i < atlasAssets.Length; ++i) {
    foreach (Material material in atlasAssets[i].Materials) {
        if (material.mainTexture != null) {
            // assign material.mainTexture at your desired object
        }
    }
}

You could either change the texture at an existing Sprite (which is used at your SpriteRenderer) or create a new Sprite from the given atlas texture.

If you want to only show attachments that are currently active (attachments of a specific single skin) and want to display them layed out in a grid or line next to each other, I'm afraid this will involve some coding on your side. If that's what you want, please let us know, then we can provide some information on how you could achieve that.

    Harald Thanks for the reply!!! Yes, what I want is to be able to display only the attachments of a specific Skin (but all of them, combined, into a single image). For instance, I'd like to display a Skin called "head", which contains multiple attachments (headbase, eyes, eyebrows etc). Not side by side, but all together in a single image, as if I set my SkeletonGraphic.initialSkinName = "head". Would it be possible to do that without actually requiring a SkeletonGraphic on that object? Is it something somewhat simple to do? Thanks in advance!

    • Harald ответили на это сообщение.

      hiknapolitano
      You could repack the desired skin, this will pack a skin to a new texture, all you would then need to do is display this texture in your desired way.

      See the documentation here, subsection "Runtime Repacking":
      http://esotericsoftware.com/spine-unity#Combining-Skins

        Harald is this the only way? I can't use the repacking feature because I need my atlases to have compression (and the repacking fails with compressed textures)

        • Harald ответили на это сообщение.

          hiknapolitano There are alternative ways, but they involve more programming work on your side. I assume you only need the images to be generated within the Unity Editor and not at runtime, so the easiest way would be to just let your Editor script change the Texture Importer's compression setting temporarily to None, pack the skin to your texture, and then reset compression to the previous value again.

            Harald Thank you! Just have another quick question and was wondering if maybe you could answer me.. Is it possible to set custom materials for specific slots of a skeletongraphic? i know the skeletonrenderercustommaterials component allows that, but for some reason the skeletongraphiccustommaterials one doesn't 🤔