Hey guys, thanks a lot for the response and the help.
@Nate I might be wrong but it is not possible to create a runtime skin from a sprite or texture not defined in the Spine project or skeleton data.
Even in the example given, you find the skin from the skeleton data, and then create a new one with it.
Skin newSkin = new Skin("new-skin"); // 1. Create a new empty skin
newSkin.addSkin(skeletonData.findSkin("shirt/pink"); // 2. Add items
newSkin.addSkin(skeletonData.findSkin("pants/green");
newSkin.addSkin(skeletonData.findSkin("shoes/sneakers");
So this means that I would have to have all possible sprites on the skeleton data, for me to be able to find it and add it to a new skin? I am trying to avoid this.
As for attachments, we are not using attachments at all currently, so this is not helping, unless I am again missing something.
I am currently working on creating a new Atlas, but I have a problem, what I am doing:
I store all atlas regions into a rect[]
, then I update the regions with texture2d.SetPixel()
afterwords I make a new texture with all the updated regions, with the same size as the Atlas. This works but the issue is that the rects are flipped on the Y axis.
`private void SwitchAtlas()
{
var atlas = skeletonAnimation.skeletonDataAsset.atlasAssets[0].GetAtlas().Regions;
rects = new Rect[skeletonAnimation.skeletonDataAsset.atlasAssets[0].GetAtlas().Regions.Count];
textures = new Texture2D[rects.Length];
for (int i = 0; i < rects.Length; i++)
{
//textures.Add(atlas[i].ToTexture());
textures[i] = atlas[i].ToTexture();
textures[i].Apply();
rects[i] = new Rect(new Vector2(atlas[i].x,
atlas[i].y),
new Vector2(atlas[i].packedWidth,
atlas[i].packedHeight));
rects[i].y += skeletonAnimation.skeletonDataAsset.atlasAssets[0].PrimaryMaterial.mainTexture.height;
}
var editedTexture = SetAtlasTexture(textures[39],
textures[12]);
texture = editedTexture;
textures[39] = editedTexture;
newAtlas = new Texture2D(skeletonAnimation.skeletonDataAsset.atlasAssets[0].PrimaryMaterial.mainTexture.width,
skeletonAnimation.skeletonDataAsset.atlasAssets[0].PrimaryMaterial.mainTexture.height);
for (int i = 0; i < rects.Length; i++)
{
for (int x = 0; x < rects[i].width; x++)
{
for(int y = 0; y < rects[i].height; y++)
{
Color newPixel = textures[i].GetPixel(x, y);
newAtlas.SetPixel(x + (int)rects[i].position.x, y + (int)rects[i].position.y, newPixel);
}
}
newAtlas.Apply();
}
skeletonAnimation.skeletonDataAsset.atlasAssets[0].PrimaryMaterial.mainTexture = newAtlas;
}`
public Texture2D SetAtlasTexture(Texture2D background, Texture2D overlay)
{
for (int x = 0; x < background.width; x++)
{
for(int y = 0; y < background.height; y++)
{
Color bgColor = background.GetPixel(x, y);
Color wmColor = overlay.GetPixel(x, y);
//Color final_color = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);
Color replacedColor = wmColor;
background.SetPixel(x, y, replacedColor);
}
}
background.Apply();
return background;
}
I am yet to use the texture packer you mentioned, perhaps this would solve my issue?
Thank you for your time once again.