EquipsVisualsComponentExample.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Spine.Unity.Modules.AttachmentTools;
  5. namespace Spine.Unity.Examples {
  6. public class EquipsVisualsComponentExample : MonoBehaviour {
  7. public SkeletonAnimation skeletonAnimation;
  8. [SpineSkin]
  9. public string templateSkinName;
  10. Spine.Skin equipsSkin;
  11. Spine.Skin collectedSkin;
  12. public Material runtimeMaterial;
  13. public Texture2D runtimeAtlas;
  14. void Start () {
  15. equipsSkin = new Skin("Equips");
  16. // OPTIONAL: Add all the attachments from the template skin.
  17. var templateSkin = skeletonAnimation.Skeleton.Data.FindSkin(templateSkinName);
  18. if (templateSkin != null)
  19. equipsSkin.Append(templateSkin);
  20. skeletonAnimation.Skeleton.Skin = equipsSkin;
  21. RefreshSkeletonAttachments();
  22. }
  23. public void Equip (int slotIndex, string attachmentName, Attachment attachment) {
  24. equipsSkin.AddAttachment(slotIndex, attachmentName, attachment);
  25. skeletonAnimation.Skeleton.SetSkin(equipsSkin);
  26. RefreshSkeletonAttachments();
  27. }
  28. public void OptimizeSkin () {
  29. // 1. Collect all the attachments of all active skins.
  30. collectedSkin = collectedSkin ?? new Skin("Collected skin");
  31. collectedSkin.Clear();
  32. collectedSkin.Append(skeletonAnimation.Skeleton.Data.DefaultSkin);
  33. collectedSkin.Append(equipsSkin);
  34. // 2. Create a repacked skin.
  35. var repackedSkin = collectedSkin.GetRepackedSkin("Repacked skin", skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial, out runtimeMaterial, out runtimeAtlas);
  36. collectedSkin.Clear();
  37. // 3. Use the repacked skin.
  38. skeletonAnimation.Skeleton.Skin = repackedSkin;
  39. RefreshSkeletonAttachments();
  40. }
  41. void RefreshSkeletonAttachments () {
  42. skeletonAnimation.Skeleton.SetSlotsToSetupPose();
  43. skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton); //skeletonAnimation.Update(0);
  44. }
  45. }
  46. }