MixAndMatchGraphic.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 MixAndMatchGraphic : MonoBehaviour {
  36. #region Inspector
  37. [SpineSkin]
  38. public string baseSkinName = "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 Required!!")]
  49. public bool repack = true;
  50. [Header("Do not assign")]
  51. public Texture2D runtimeAtlas;
  52. public Material runtimeMaterial;
  53. #endregion
  54. Skin customSkin;
  55. void OnValidate () {
  56. if (sourceMaterial == null) {
  57. var skeletonGraphic = GetComponent<SkeletonGraphic>();
  58. if (skeletonGraphic != null)
  59. sourceMaterial = skeletonGraphic.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial;
  60. }
  61. }
  62. IEnumerator Start () {
  63. yield return new WaitForSeconds(1f); // Delay for 1 second. For testing.
  64. Apply();
  65. }
  66. [ContextMenu("Apply")]
  67. void Apply () {
  68. var skeletonGraphic = GetComponent<SkeletonGraphic>();
  69. var skeleton = skeletonGraphic.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 and don't plan to remove
  74. // Next let's get the skin that contains our source attachments. These are the attachments that
  75. var baseSkin = skeleton.Data.FindSkin(baseSkinName);
  76. // STEP 1: "EQUIP" ITEMS USING SPRITES
  77. // STEP 1.1 Find the original attachment.
  78. // Step 1.2 Get a clone of the original attachment.
  79. // Step 1.3 Apply the Sprite image to it.
  80. // Step 1.4 Add the remapped clone to the new custom skin.
  81. // Let's do this for the visor.
  82. int visorSlotIndex = skeleton.FindSlotIndex(visorSlot); // You can access GetAttachment and SetAttachment via string, but caching the slotIndex is faster.
  83. Attachment baseAttachment = baseSkin.GetAttachment(visorSlotIndex, visorKey); // STEP 1.1
  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.FindSlotIndex(gunSlot);
  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.Append(skeleton.Data.DefaultSkin);
  104. repackedSkin.Append(customSkin);
  105. repackedSkin = repackedSkin.GetRepackedSkin("repacked skin", sourceMaterial, out runtimeMaterial, out runtimeAtlas);
  106. skeleton.SetSkin(repackedSkin);
  107. } else {
  108. skeleton.SetSkin(customSkin);
  109. }
  110. //skeleton.SetSlotsToSetupPose();
  111. skeleton.SetToSetupPose();
  112. skeletonGraphic.Update(0);
  113. skeletonGraphic.OverrideTexture = runtimeAtlas;
  114. Resources.UnloadUnusedAssets();
  115. }
  116. }
  117. }