• RuntimesUnity
  • How to add custom skin at runtime?

I want to make a mod function that player can add a texture, and read it as a skin. Some way do make it work?

  • इस पर Harald ने जवाब दिया।
    Related Discussions
    ...

    To add a custom skin at runtime, you can follow these steps:

    1. 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.

    2. Create Attachments: Create new attachments for the texture. This involves creating a RegionAttachment or MeshAttachment depending on your needs.

    3. Create a Skin: Create a new Skin object and add the attachments to it.

    4. 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.

    JustHitIt I want to make a mod function that player can add a texture, and read it as a skin. Some way do make it work?

    The question is what you want to support for customization: Shall your game only support replacing one or more atlas textures with textures following the identical layout (a "paintover")? If so, you could utilize whatever Unity has to offer in terms of loading a custom texture at runtime (e.g. during startup from a pre-defined location), and then swap-out the texture at the existing material. This would save you the work of creating the full logic for creating attachments and skins.

    • इस पर JustHitIt ने जवाब दिया।

      Harald solved with Mix and Match.

      • Nate ओर Harald ने इसे लाइक किया।