EquipsVisualsComponentExample.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, 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. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using Spine.Unity.AttachmentTools;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using UnityEngine;
  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.AddSkin(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.AddSkin(skeletonAnimation.Skeleton.Data.DefaultSkin);
  61. collectedSkin.AddSkin(equipsSkin);
  62. // 2. Create a repacked skin.
  63. // Note: materials and textures returned by GetRepackedSkin() behave like 'new Texture2D()' and need to be destroyed
  64. if (runtimeMaterial)
  65. Destroy(runtimeMaterial);
  66. if (runtimeAtlas)
  67. Destroy(runtimeAtlas);
  68. var repackedSkin = collectedSkin.GetRepackedSkin("Repacked skin", skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial,
  69. out runtimeMaterial, out runtimeAtlas, maxAtlasSize: 1024, clearCache: false);
  70. collectedSkin.Clear();
  71. // You can optionally clear the textures cache after each ore multiple repack operations are done.
  72. //AtlasUtilities.ClearCache();
  73. //Resources.UnloadUnusedAssets();
  74. // 3. Use the repacked skin.
  75. skeletonAnimation.Skeleton.Skin = repackedSkin;
  76. RefreshSkeletonAttachments();
  77. }
  78. void RefreshSkeletonAttachments () {
  79. skeletonAnimation.Skeleton.SetSlotsToSetupPose();
  80. skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton); //skeletonAnimation.Update(0);
  81. }
  82. }
  83. }