• Unity
  • Help with costume editor tool

Hello!
So recently I have been working on a tool for building skins for characters. It allows the user to choose a character out of a list and select its skin parts, as well as edit the attachments and sub skins those skins are built out of.

My only problem is that I can't seem to find any way to show the visuals for the parts of the skin. Is there a way to show a skin part as sprite? if so how does one do it?

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

You can get a Sprite from an AtlasRegion via the provided extension method in AttachmentTools:

using Spine.Unity.AttachmentTools;

void OnGUI () { // or a similar method
   Skin skin = ...;
   Atlas atlas = ...; // e.g. skeletonAnimation.SkeletonDataAsset.atlasAssets[0].GetAtlas();

   foreach (Skin.SkinEntry skinEntry in skin.Attachments) {
      string name = skinEntry.Name;
      AtlasRegion region = atlas.FindRegion(name);
      Sprite sprite = region.ToSprite();
        ..
   }

If this is not an option, you could extract the Attachment's UVs, or regionOffsetX, regionOffsetY, regionWidth, regionHeight:

foreach (Skin.SkinEntry skinEntry in skin.Attachments) {
   var attachment = skinEntry.Attachment;
   RegionAttachment regionAttachment = attachment as RegionAttachment;
   MeshAttachment meshAttachment = attachment as MeshAttachment;
   float[] uvs;
   if (regionAttachment != null)
      uvs = regionAttachment.UVs; // or get regionOffsetX, regionOffsetY, regionWidth, regionHeight
   else if (meshAttachment != null)
      uvs = meshAttachment.UVs; // or get regionOffsetX, regionOffsetY, regionWidth, regionHeight
   ...

And then use the UV coords or regionOffsetX, regionOffsetY, regionWidth, regionHeight to copy the region pixels to a temporary texture to use with e.g. DrawPreviewTexture:
https://docs.unity3d.com/ScriptReference/EditorGUI.DrawPreviewTexture.html
Or you could use DrawTexture, which allows a sourceRect parameter to define source uv coords.
https://docs.unity3d.com/ScriptReference/Graphics.DrawTexture.html
Note that you then might still run into the problem of packing rotation (which can be 0, 90, 180 or 270 degrees).