Hello people, I'm having some problems with the "MixAndMatch" script.
My objective is to create a custom player, which is very customizable, you can change gloves, shoes etc.. and I want to increase the number of things in the future, so I do not want to be limited by the size of an atlas. So I was thinking in implement this on the fallowing way.
I'm thinking of having only one simple atlas with the basic character, and all the other parts I would put in a Resource folder as an isolated Sprites, so I'll do not have a limited number of parts to store.
When I change the sprites on Editor everything works just fine, but when I try to use this method on runtime, mainly when I repack the atlas more than one time, the character Atlas starts to become distorted.
This the actual Script I'm using, I'm using a button to access the "SetSprite" method repeatedly.
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using Spine;
using Spine.Unity;
using Spine.Unity.Modules;
using Spine.Unity.Modules.AttachmentTools;
using UnityEngine;
public class CustomPlayerManager : MonoBehaviour
{
public GameObject SpinePlayer;
private SkeletonAnimation _skeletonAnimation;
private Skeleton _skeleton;
private Skin newSkin;
[Header("Sprite lists")] public Sprite[] Eyes;
[Header("Sprite lists")]public Sprite[] Mouth;
[Header("Runtime Repack")]
public Shader RepackedShader;
[Header("Não fazer nada")]
public Texture2D RutimeAtlas;
public Material RuntimeMaterial;
[SpineSlot] public string spineSlot;
public void Start()
{
Eyes = Resources.LoadAll<Sprite>("Sprites/Player/Eye");
print(Eyes.Length);
Mouth = Resources.LoadAll<Sprite>("Sprites/Player/Mouth");
print(Mouth.Length);
_skeletonAnimation = SpinePlayer.GetComponent<SkeletonAnimation>();
_skeleton = _skeletonAnimation.skeleton;
if (newSkin == null) newSkin = new Skin("My Generated Skin");
}
public void SetSprite()
{
//var newSkin = _skeleton.UnshareSkin(true, false, _skeletonAnimation.AnimationState);
//attaching Sprite
string slot = "eye_L";
Sprite newSprite = Eyes[3];
RegionAttachment r = newSprite.ToRegionAttachmentPMAClone(Shader.Find("Spine/Skeleton"));
int slotIndex = _skeleton.FindSlotIndex(slot);
newSkin.AddAttachment(slotIndex, newSprite.name, r);
//Repacking
newSkin = newSkin.GetRepackedSkin("repacked", RepackedShader, out RuntimeMaterial, out RutimeAtlas);
_skeleton.SetSkin(newSkin);
_skeleton.SetToSetupPose();
_skeleton.SetAttachment(slot, newSprite.name);
Resources.UnloadUnusedAssets();
}
public void SetSprite2()
{
//var newSkin = _skeleton.UnshareSkin(true, false, _skeletonAnimation.AnimationState);
//attaching Sprite
string slot = "mouth";
Sprite newSprite = Mouth[1];
RegionAttachment r = newSprite.ToRegionAttachmentPMAClone(Shader.Find("Spine/Skeleton"));
int slotIndex = _skeleton.FindSlotIndex(slot);
newSkin.AddAttachment(slotIndex, newSprite.name, r);
//Repacking
newSkin = newSkin.GetRepackedSkin("repacked", RepackedShader, out RuntimeMaterial, out RutimeAtlas);
_skeleton.SetSkin(newSkin);
_skeleton.SetToSetupPose();
_skeleton.SetAttachment(slot, newSprite.name);
Resources.UnloadUnusedAssets();
}
}
24 May 2017 4:26 am
I noticed another thing, I'm sure I'm doing something wrong... but when I change one sprite, I noticed that the previous one that I changed before back to its original sprite like I was alternating between skins, and I need that the changes be accumulative, although the repacked atlas it seems to be increasing of size.