• Unity
  • How to replace texture of a Spine Entity in Game at runtime

  • Editado
Related Discussions
...

According to this page

, I''ve changed all part of my character which contains head,body,hand,and foot, and when I instantiate another character which exactly the same as the first,but I don't want dealing with the second character using same way, for the second character,it same as the first, can I use the first character textrue for the second character?

You should be able to assign a repacked skin to multiple skeletons, just call skeleton.SetSkin(mySharedSkin) on multiple skeletons instead of once.

Harald escribió

You should be able to assign a repacked skin to multiple skeletons, just call skeleton.SetSkin(mySharedSkin) on multiple skeletons instead of once.

I called skeleton.SetSkin(mySharedSkin) and then second character is missing material, So I did the following processing,still not working:

//save the first character skin and texture 

repackedSkin=repackedSkin.GetRepackedSkin(templateSkinName, sourceMaterial, out runtimeMaterial, out runtimeAtlas);
skeleton.SetSkin(repackedSkin);

//get the clone of the first character texture not reference
Texture2D copyTexture = new Texture2D(runtimeAtlas.width, runtimeAtlas.height);
copyTexture.SetPixels(runtimeAtlas.GetPixels());
copyTexture.Apply();

//put first character skin and texture in an object with id "1"
DuckSkinPool.AddSkin("1",new DuckSkin(repackedSkin,copyTexture));


//////////////////////////////////////////////////////////////////

//set second character skin
DuckSkin duckSkin = DuckSkinPool.GetSkin("1");
skeleton.SetSkin(duckSkin.Skin);

//not working
//GetComponent<MeshRenderer>().material = duckSkin.Material;
GetComponent<SkeletonMecanim>().CustomMaterialOverride.Add(runtimeMaterial, duckSkin.Material);
GetComponent<SkeletonMecanim>().CustomMaterialOverride.Remove(runtimeMaterial); 


//////////////////////////////////////////////////////////////////

public class DuckSkin
{
     public Skin Skin;
     public Texture2D Texture;

 public DuckSkin(Skin skin,Texture2D texture)
 {
      Skin = skin;
      Texture = texture;
 }

 public Material Material
 {
      get
      {
           var m = new Material(Shader.Find("Spine/Skeleton"));
           m.mainTexture = Texture;
           return m;
      }
 }
}

Unfortunately I could not reproduce your problem, adding the following lines to the MixAndMatchSkinsExample.cs file (used in the Mix and Match Skins example scene) worked without any issues:

// Use the repacked skin. // Original code
skeletonAnimation.Skeleton.Skin = repackedSkin;
skeletonAnimation.Skeleton.SetSlotsToSetupPose();
skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton);

// Added code assigning the `repackedSkin` at other `SkeletonAnimation` objects as well
foreach (SkeletonAnimation otherSkeletonAnimation in skeletonAnimationList) {
   Skeleton otherSkeleton = otherSkeletonAnimation.Skeleton;
   otherSkeleton.Skin = repackedSkin;
   otherSkeleton.SetSlotsToSetupPose();
   otherSkeletonAnimation.AnimationState.Apply(otherSkeletonAnimation.Skeleton);
}

(Note that the repack operation is triggered when hitting the Done button in the example scene)

Harald escribió

Your script change other characters skeleton skin immediately after the first character generare the repacked skin, In my case, when I trigger some button, other character apply the repacked skin which is generated by first character , and they will all lose material.


Harald escribió

Unfortunately I could not reproduce your problem, adding the following lines to the MixAndMatchSkinsExample.cs file (used in the Mix and Match Skins example scene) worked without any issues:

// Use the repacked skin. // Original code
skeletonAnimation.Skeleton.Skin = repackedSkin;
skeletonAnimation.Skeleton.SetSlotsToSetupPose();
skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton);

// Added code assigning the `repackedSkin` at other `SkeletonAnimation` objects as well
foreach (SkeletonAnimation otherSkeletonAnimation in skeletonAnimationList) {
   Skeleton otherSkeleton = otherSkeletonAnimation.Skeleton;
   otherSkeleton.Skin = repackedSkin;
   otherSkeleton.SetSlotsToSetupPose();
   otherSkeletonAnimation.AnimationState.Apply(otherSkeletonAnimation.Skeleton);
}

(Note that the repack operation is triggered when hitting the Done button in the example scene)

Actually, my real purpose is,I want keep holding the the texture in memory which is generated by fisrt character , and at the right time I will map the texture to another skeleton. How can Do it.

Just assign the repacked skin later then.

Harald escribió

Just assign the repacked skin later then.

Assign the repacked skin is working, Thanks Harald, I have found the reason for the missing material. :grinteeth:

Very glad to hear you've figured it out!