MixAndMatchGraphic.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. // This is an example script that shows you how to change images on your skeleton using UnityEngine.Sprites.
  34. public class MixAndMatchGraphic : MonoBehaviour {
  35. #region Inspector
  36. [SpineSkin]
  37. public string baseSkinName = "base";
  38. public Material sourceMaterial; // This will be used as the basis for shader and material property settings.
  39. [Header("Visor")]
  40. public Sprite visorSprite;
  41. [SpineSlot] public string visorSlot;
  42. [SpineAttachment(slotField: "visorSlot", skinField: "baseSkinName")] public string visorKey = "goggles";
  43. [Header("Gun")]
  44. public Sprite gunSprite;
  45. [SpineSlot] public string gunSlot;
  46. [SpineAttachment(slotField: "gunSlot", skinField: "baseSkinName")] public string gunKey = "gun";
  47. [Header("Runtime Repack Required!!")]
  48. public bool repack = true;
  49. [Header("Do not assign")]
  50. public Texture2D runtimeAtlas;
  51. public Material runtimeMaterial;
  52. #endregion
  53. Skin customSkin;
  54. void OnValidate () {
  55. if (sourceMaterial == null) {
  56. var skeletonGraphic = GetComponent<SkeletonGraphic>();
  57. if (skeletonGraphic != null)
  58. sourceMaterial = skeletonGraphic.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial;
  59. }
  60. }
  61. IEnumerator Start () {
  62. yield return new WaitForSeconds(1f); // Delay for 1 second. For testing.
  63. Apply();
  64. }
  65. [ContextMenu("Apply")]
  66. void Apply () {
  67. var skeletonGraphic = GetComponent<SkeletonGraphic>();
  68. var skeleton = skeletonGraphic.Skeleton;
  69. // STEP 0: PREPARE SKINS
  70. // Let's prepare a new skin to be our custom skin with equips/customizations. We get a clone so our original skins are unaffected.
  71. customSkin = customSkin ?? new Skin("custom skin"); // This requires that all customizations are done with skin placeholders defined in Spine.
  72. // Next let's get the skin that contains our source attachments. These are the attachments that
  73. var baseSkin = skeleton.Data.FindSkin(baseSkinName);
  74. // STEP 1: "EQUIP" ITEMS USING SPRITES
  75. // STEP 1.1 Find the original attachment.
  76. // Step 1.2 Get a clone of the original attachment.
  77. // Step 1.3 Apply the Sprite image to it.
  78. // Step 1.4 Add the remapped clone to the new custom skin.
  79. // Let's do this for the visor.
  80. int visorSlotIndex = skeleton.Data.FindSlot(visorSlot).Index; // You can access GetAttachment and SetAttachment via string, but caching the slotIndex is faster.
  81. Attachment baseAttachment = baseSkin.GetAttachment(visorSlotIndex, visorKey); // STEP 1.1
  82. // Note: Each call to `GetRemappedClone()` with parameter `premultiplyAlpha` set to `true` creates
  83. // a cached Texture copy which can be cleared by calling AtlasUtilities.ClearCache() as done below.
  84. Attachment newAttachment = baseAttachment.GetRemappedClone(visorSprite, sourceMaterial); // STEP 1.2 - 1.3
  85. customSkin.SetAttachment(visorSlotIndex, visorKey, newAttachment); // STEP 1.4
  86. // And now for the gun.
  87. int gunSlotIndex = skeleton.Data.FindSlot(gunSlot).Index;
  88. Attachment baseGun = baseSkin.GetAttachment(gunSlotIndex, gunKey); // STEP 1.1
  89. Attachment newGun = baseGun.GetRemappedClone(gunSprite, sourceMaterial); // STEP 1.2 - 1.3
  90. if (newGun != null) customSkin.SetAttachment(gunSlotIndex, gunKey, newGun); // STEP 1.4
  91. // customSkin.RemoveAttachment(gunSlotIndex, gunKey); // To remove an item.
  92. // customSkin.Clear()
  93. // Use skin.Clear() To remove all customizations.
  94. // Customizations will fall back to the value in the default skin if it was defined there.
  95. // To prevent fallback from happening, make sure the key is not defined in the default skin.
  96. // STEP 3: APPLY AND CLEAN UP.
  97. // Recommended: REPACK THE CUSTOM SKIN TO MINIMIZE DRAW CALLS
  98. // Repacking requires that you set all source textures/sprites/atlases to be Read/Write enabled in the inspector.
  99. // Combine all the attachment sources into one skin. Usually this means the default skin and the custom skin.
  100. // call Skin.GetRepackedSkin to get a cloned skin with cloned attachments that all use one texture.
  101. if (repack) {
  102. var repackedSkin = new Skin("repacked skin");
  103. repackedSkin.AddSkin(skeleton.Data.DefaultSkin);
  104. repackedSkin.AddSkin(customSkin);
  105. // Note: materials and textures returned by GetRepackedSkin() behave like 'new Texture2D()' and need to be destroyed
  106. if (runtimeMaterial)
  107. Destroy(runtimeMaterial);
  108. if (runtimeAtlas)
  109. Destroy(runtimeAtlas);
  110. repackedSkin = repackedSkin.GetRepackedSkin("repacked skin", sourceMaterial, out runtimeMaterial, out runtimeAtlas);
  111. skeleton.SetSkin(repackedSkin);
  112. } else {
  113. skeleton.SetSkin(customSkin);
  114. }
  115. //skeleton.SetSlotsToSetupPose();
  116. skeleton.SetToSetupPose();
  117. skeletonGraphic.Update(0);
  118. skeletonGraphic.OverrideTexture = runtimeAtlas;
  119. // `GetRepackedSkin()` and each call to `GetRemappedClone()` with parameter `premultiplyAlpha` set to `true`
  120. // cache necessarily created Texture copies which can be cleared by calling AtlasUtilities.ClearCache().
  121. // You can optionally clear the textures cache after multiple repack operations.
  122. // Just be aware that while this cleanup frees up memory, it is also a costly operation
  123. // and will likely cause a spike in the framerate.
  124. AtlasUtilities.ClearCache();
  125. Resources.UnloadUnusedAssets();
  126. }
  127. }
  128. }