MixAndMatchGraphic.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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].materials[0];
  59. }
  60. }
  61. void Start () {
  62. Apply();
  63. }
  64. void Apply () {
  65. var skeletonGraphic = GetComponent<SkeletonGraphic>();
  66. var skeleton = skeletonGraphic.Skeleton;
  67. // STEP 0: PREPARE SKINS
  68. // Let's prepare a new skin to be our custom skin with equips/customizations. We get a clone so our original skins are unaffected.
  69. customSkin = customSkin ?? new Skin("custom skin"); // This requires that all customizations are done with skin placeholders defined in Spine.
  70. //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
  71. // Next let's
  72. var baseSkin = skeleton.Data.FindSkin(baseSkinName);
  73. // STEP 1: "EQUIP" ITEMS USING SPRITES
  74. // STEP 1.1 Find the original attachment.
  75. // Step 1.2 Get a clone of the original attachment.
  76. // Step 1.3 Apply the Sprite image to it.
  77. // Step 1.4 Add the remapped clone to the new custom skin.
  78. // Let's do this for the visor.
  79. int visorSlotIndex = skeleton.FindSlotIndex(visorSlot); // You can access GetAttachment and SetAttachment via string, but caching the slotIndex is faster.
  80. Attachment baseAttachment = baseSkin.GetAttachment(visorSlotIndex, visorKey); // STEP 1.1
  81. Attachment newAttachment = baseAttachment.GetRemappedClone(visorSprite, sourceMaterial); // STEP 1.2 - 1.3
  82. customSkin.SetAttachment(visorSlotIndex, visorKey, newAttachment); // STEP 1.4
  83. // And now for the gun.
  84. int gunSlotIndex = skeleton.FindSlotIndex(gunSlot);
  85. Attachment baseGun = baseSkin.GetAttachment(gunSlotIndex, gunKey); // STEP 1.1
  86. Attachment newGun = baseGun.GetRemappedClone(gunSprite, sourceMaterial); // STEP 1.2 - 1.3
  87. if (newGun != null) customSkin.SetAttachment(gunSlotIndex, gunKey, newGun); // STEP 1.4
  88. // customSkin.RemoveAttachment(gunSlotIndex, gunKey); // To remove an item.
  89. // customSkin.Clear()
  90. // Use skin.Clear() To remove all customizations.
  91. // Customizations will fall back to the value in the default skin if it was defined there.
  92. // To prevent fallback from happening, make sure the key is not defined in the default skin.
  93. // STEP 3: APPLY AND CLEAN UP.
  94. // Recommended: REPACK THE CUSTOM SKIN TO MINIMIZE DRAW CALLS
  95. // Repacking requires that you set all source textures/sprites/atlases to be Read/Write enabled in the inspector.
  96. // Combine all the attachment sources into one skin. Usually this means the default skin and the custom skin.
  97. // call Skin.GetRepackedSkin to get a cloned skin with cloned attachments that all use one texture.
  98. // Under the hood, this relies on
  99. if (repack) {
  100. var repackedSkin = new Skin("repacked skin");
  101. repackedSkin.Append(skeleton.Data.DefaultSkin);
  102. repackedSkin.Append(customSkin);
  103. repackedSkin = repackedSkin.GetRepackedSkin("repacked skin", sourceMaterial, out runtimeMaterial, out runtimeAtlas);
  104. skeleton.SetSkin(repackedSkin);
  105. } else {
  106. skeleton.SetSkin(customSkin);
  107. }
  108. skeleton.SetSlotsToSetupPose();
  109. skeletonGraphic.Update(0);
  110. skeletonGraphic.OverrideTexture = runtimeAtlas;
  111. Resources.UnloadUnusedAssets();
  112. }
  113. }
  114. }