To add a custom skin at runtime, you can follow these steps:
Load the Texture: First, load the texture that the player wants to add. This can be done using your game engine's texture loading functions.
Create Attachments: Create new attachments for the texture. This involves creating a RegionAttachment
or MeshAttachment
depending on your needs.
Create a Skin: Create a new Skin
object and add the attachments to it.
Set the Skin: Apply the new skin to the skeleton.
Here is an example in C# for Unity:
// Load the texture
Texture2D newTexture = LoadTexture("path/to/texture.png");
// Create a new region attachment
AtlasRegion region = new AtlasRegion();
region.name = "newRegion";
region.page = new AtlasPage();
region.page.rendererObject = newTexture;
region.u = 0;
region.v = 0;
region.u2 = 1;
region.v2 = 1;
region.width = newTexture.width;
region.height = newTexture.height;
region.originalWidth = newTexture.width;
region.originalHeight = newTexture.height;
region.offsetX = 0;
region.offsetY = 0;
region.rotate = false;
RegionAttachment attachment = new RegionAttachment("newAttachment");
attachment.SetRegion(region);
attachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate);
attachment.RegionWidth = region.width;
attachment.RegionHeight = region.height;
attachment.Width = region.width;
attachment.Height = region.height;
attachment.UpdateOffset();
// Create a new skin and add the attachment
Skin newSkin = new Skin("customSkin");
int slotIndex = skeleton.FindSlotIndex("slotName");
newSkin.SetAttachment(slotIndex, "attachmentName", attachment);
// Apply the new skin to the skeleton
skeleton.SetSkin(newSkin);
skeleton.SetSlotsToSetupPose();
skeletonAnimation.Update(0);
This example assumes you have a function LoadTexture
to load the texture from a file path. You will need to adapt this to your specific runtime and game engine.
For more detailed information, you can refer to the Spine Runtime Skins Guide and the Mix and Match example.