EquipsVisualsComponentExample.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated May 1, 2019. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2019, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. * NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. * INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. using Spine.Unity.Modules.AttachmentTools;
  33. namespace Spine.Unity.Examples {
  34. public class EquipsVisualsComponentExample : MonoBehaviour {
  35. public SkeletonAnimation skeletonAnimation;
  36. [SpineSkin]
  37. public string templateSkinName;
  38. Spine.Skin equipsSkin;
  39. Spine.Skin collectedSkin;
  40. public Material runtimeMaterial;
  41. public Texture2D runtimeAtlas;
  42. void Start () {
  43. equipsSkin = new Skin("Equips");
  44. // OPTIONAL: Add all the attachments from the template skin.
  45. var templateSkin = skeletonAnimation.Skeleton.Data.FindSkin(templateSkinName);
  46. if (templateSkin != null)
  47. equipsSkin.AddAttachments(templateSkin);
  48. skeletonAnimation.Skeleton.Skin = equipsSkin;
  49. RefreshSkeletonAttachments();
  50. }
  51. public void Equip (int slotIndex, string attachmentName, Attachment attachment) {
  52. equipsSkin.SetAttachment(slotIndex, attachmentName, attachment);
  53. skeletonAnimation.Skeleton.SetSkin(equipsSkin);
  54. RefreshSkeletonAttachments();
  55. }
  56. public void OptimizeSkin () {
  57. // 1. Collect all the attachments of all active skins.
  58. collectedSkin = collectedSkin ?? new Skin("Collected skin");
  59. collectedSkin.Clear();
  60. collectedSkin.AddAttachments(skeletonAnimation.Skeleton.Data.DefaultSkin);
  61. collectedSkin.AddAttachments(equipsSkin);
  62. // 2. Create a repacked skin.
  63. var repackedSkin = collectedSkin.GetRepackedSkin("Repacked skin", skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial, out runtimeMaterial, out runtimeAtlas);
  64. collectedSkin.Clear();
  65. // 3. Use the repacked skin.
  66. skeletonAnimation.Skeleton.Skin = repackedSkin;
  67. RefreshSkeletonAttachments();
  68. }
  69. void RefreshSkeletonAttachments () {
  70. skeletonAnimation.Skeleton.SetSlotsToSetupPose();
  71. skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton); //skeletonAnimation.Update(0);
  72. }
  73. }
  74. }