MixAndMatch.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using UnityEngine;
  31. using Spine.Unity.Modules.AttachmentTools;
  32. using System.Collections;
  33. namespace Spine.Unity.Examples {
  34. // This is an example script that shows you how to change images on your skeleton using UnityEngine.Sprites.
  35. public class MixAndMatch : MonoBehaviour {
  36. #region Inspector
  37. [SpineSkin]
  38. public string templateAttachmentsSkin = "base";
  39. public Material sourceMaterial; // This will be used as the basis for shader and material property settings.
  40. [Header("Visor")]
  41. public Sprite visorSprite;
  42. [SpineSlot] public string visorSlot;
  43. [SpineAttachment(slotField:"visorSlot", skinField:"baseSkinName")] public string visorKey = "goggles";
  44. [Header("Gun")]
  45. public Sprite gunSprite;
  46. [SpineSlot] public string gunSlot;
  47. [SpineAttachment(slotField:"gunSlot", skinField:"baseSkinName")] public string gunKey = "gun";
  48. [Header("Runtime Repack")]
  49. public bool repack = true;
  50. public BoundingBoxFollower bbFollower;
  51. [Header("Do not assign")]
  52. public Texture2D runtimeAtlas;
  53. public Material runtimeMaterial;
  54. #endregion
  55. Skin customSkin;
  56. void OnValidate () {
  57. if (sourceMaterial == null) {
  58. var skeletonAnimation = GetComponent<SkeletonAnimation>();
  59. if (skeletonAnimation != null)
  60. sourceMaterial = skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial;
  61. }
  62. }
  63. IEnumerator Start () {
  64. yield return new WaitForSeconds(1f); // Delay for one second before applying. For testing.
  65. Apply();
  66. }
  67. void Apply () {
  68. var skeletonAnimation = GetComponent<SkeletonAnimation>();
  69. var skeleton = skeletonAnimation.Skeleton;
  70. // STEP 0: PREPARE SKINS
  71. // Let's prepare a new skin to be our custom skin with equips/customizations. We get a clone so our original skins are unaffected.
  72. customSkin = customSkin ?? new Skin("custom skin"); // This requires that all customizations are done with skin placeholders defined in Spine.
  73. //customSkin = customSkin ?? skeleton.UnshareSkin(true, false, skeletonAnimation.AnimationState); // use this if you are not customizing on the default skin.
  74. var templateSkin = skeleton.Data.FindSkin(templateAttachmentsSkin);
  75. // STEP 1: "EQUIP" ITEMS USING SPRITES
  76. // STEP 1.1 Find the original/template attachment.
  77. // Step 1.2 Get a clone of the original/template attachment.
  78. // Step 1.3 Apply the Sprite image to the clone.
  79. // Step 1.4 Add the remapped clone to the new custom skin.
  80. // Let's do this for the visor.
  81. int visorSlotIndex = skeleton.FindSlotIndex(visorSlot); // You can access GetAttachment and SetAttachment via string, but caching the slotIndex is faster.
  82. Attachment templateAttachment = templateSkin.GetAttachment(visorSlotIndex, visorKey); // STEP 1.1
  83. Attachment newAttachment = templateAttachment.GetRemappedClone(visorSprite, sourceMaterial); // STEP 1.2 - 1.3
  84. customSkin.SetAttachment(visorSlotIndex, visorKey, newAttachment); // STEP 1.4
  85. // And now for the gun.
  86. int gunSlotIndex = skeleton.FindSlotIndex(gunSlot);
  87. Attachment templateGun = templateSkin.GetAttachment(gunSlotIndex, gunKey); // STEP 1.1
  88. Attachment newGun = templateGun.GetRemappedClone(gunSprite, sourceMaterial); // STEP 1.2 - 1.3
  89. if (newGun != null) customSkin.SetAttachment(gunSlotIndex, gunKey, newGun); // STEP 1.4
  90. // customSkin.RemoveAttachment(gunSlotIndex, gunKey); // To remove an item.
  91. // customSkin.Clear()
  92. // Use skin.Clear() To remove all customizations.
  93. // Customizations will fall back to the value in the default skin if it was defined there.
  94. // To prevent fallback from happening, make sure the key is not defined in the default skin.
  95. // STEP 3: APPLY AND CLEAN UP.
  96. // Recommended: REPACK THE CUSTOM SKIN TO MINIMIZE DRAW CALLS
  97. // Repacking requires that you set all source textures/sprites/atlases to be Read/Write enabled in the inspector.
  98. // Combine all the attachment sources into one skin. Usually this means the default skin and the custom skin.
  99. // call Skin.GetRepackedSkin to get a cloned skin with cloned attachments that all use one texture.
  100. // Under the hood, this relies on
  101. if (repack) {
  102. var repackedSkin = new Skin("repacked skin");
  103. repackedSkin.Append(skeleton.Data.DefaultSkin); // Include the "default" skin. (everything outside of skin placeholders)
  104. repackedSkin.Append(customSkin); // Include your new custom skin.
  105. repackedSkin = repackedSkin.GetRepackedSkin("repacked skin", sourceMaterial, out runtimeMaterial, out runtimeAtlas); // Pack all the items in the skin.
  106. skeleton.SetSkin(repackedSkin); // Assign the repacked skin to your Skeleton.
  107. if (bbFollower != null) bbFollower.Initialize(true);
  108. } else {
  109. skeleton.SetSkin(customSkin); // Just use the custom skin directly.
  110. }
  111. skeleton.SetSlotsToSetupPose(); // Use the pose from setup pose.
  112. skeletonAnimation.Update(0); // Use the pose in the currently active animation.
  113. Resources.UnloadUnusedAssets();
  114. }
  115. }
  116. }