MixAndMatchSkinsExample.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.Generic;
  31. using UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. public class MixAndMatchSkinsExample : MonoBehaviour {
  34. // character skins
  35. [SpineSkin] public string baseSkin = "skin-base";
  36. [SpineSkin] public string eyelidsSkin = "eyelids/girly";
  37. // here we use arrays of strings to be able to cycle between them easily.
  38. [SpineSkin] public string[] hairSkins = { "hair/brown", "hair/blue", "hair/pink", "hair/short-red", "hair/long-blue-with-scarf" };
  39. public int activeHairIndex = 0;
  40. [SpineSkin] public string[] eyesSkins = { "eyes/violet", "eyes/green", "eyes/yellow" };
  41. public int activeEyesIndex = 0;
  42. [SpineSkin] public string[] noseSkins = { "nose/short", "nose/long" };
  43. public int activeNoseIndex = 0;
  44. // equipment skins
  45. public enum ItemType {
  46. Cloth,
  47. Pants,
  48. Bag,
  49. Hat
  50. }
  51. [SpineSkin] public string clothesSkin = "clothes/hoodie-orange";
  52. [SpineSkin] public string pantsSkin = "legs/pants-jeans";
  53. [SpineSkin] public string bagSkin = "";
  54. [SpineSkin] public string hatSkin = "accessories/hat-red-yellow";
  55. SkeletonAnimation skeletonAnimation;
  56. // This "naked body" skin will likely change only once upon character creation,
  57. // so we store this combined set of non-equipment Skins for later re-use.
  58. Skin characterSkin;
  59. // for repacking the skin to a new atlas texture
  60. public Material runtimeMaterial;
  61. public Texture2D runtimeAtlas;
  62. void Awake () {
  63. skeletonAnimation = this.GetComponent<SkeletonAnimation>();
  64. }
  65. void Start () {
  66. UpdateCharacterSkin();
  67. UpdateCombinedSkin();
  68. }
  69. public void NextHairSkin () {
  70. activeHairIndex = (activeHairIndex + 1) % hairSkins.Length;
  71. UpdateCharacterSkin();
  72. UpdateCombinedSkin();
  73. }
  74. public void PrevHairSkin () {
  75. activeHairIndex = (activeHairIndex + hairSkins.Length - 1) % hairSkins.Length;
  76. UpdateCharacterSkin();
  77. UpdateCombinedSkin();
  78. }
  79. public void NextEyesSkin () {
  80. activeEyesIndex = (activeEyesIndex + 1) % eyesSkins.Length;
  81. UpdateCharacterSkin();
  82. UpdateCombinedSkin();
  83. }
  84. public void PrevEyesSkin () {
  85. activeEyesIndex = (activeEyesIndex + eyesSkins.Length - 1) % eyesSkins.Length;
  86. UpdateCharacterSkin();
  87. UpdateCombinedSkin();
  88. }
  89. public void NextNoseSkin () {
  90. activeNoseIndex = (activeNoseIndex + 1) % noseSkins.Length;
  91. UpdateCharacterSkin();
  92. UpdateCombinedSkin();
  93. }
  94. public void PrevNoseSkin () {
  95. activeNoseIndex = (activeNoseIndex + noseSkins.Length - 1) % noseSkins.Length;
  96. UpdateCharacterSkin();
  97. UpdateCombinedSkin();
  98. }
  99. public void Equip (string itemSkin, ItemType itemType) {
  100. switch (itemType) {
  101. case ItemType.Cloth:
  102. clothesSkin = itemSkin;
  103. break;
  104. case ItemType.Pants:
  105. pantsSkin = itemSkin;
  106. break;
  107. case ItemType.Bag:
  108. bagSkin = itemSkin;
  109. break;
  110. case ItemType.Hat:
  111. hatSkin = itemSkin;
  112. break;
  113. default:
  114. break;
  115. }
  116. UpdateCombinedSkin();
  117. }
  118. public void OptimizeSkin () {
  119. // Create a repacked skin.
  120. var previousSkin = skeletonAnimation.Skeleton.Skin;
  121. // Note: materials and textures returned by GetRepackedSkin() behave like 'new Texture2D()' and need to be destroyed
  122. if (runtimeMaterial)
  123. Destroy(runtimeMaterial);
  124. if (runtimeAtlas)
  125. Destroy(runtimeAtlas);
  126. Skin repackedSkin = previousSkin.GetRepackedSkin("Repacked skin", skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial, out runtimeMaterial, out runtimeAtlas);
  127. previousSkin.Clear();
  128. // Use the repacked skin.
  129. skeletonAnimation.Skeleton.Skin = repackedSkin;
  130. skeletonAnimation.Skeleton.SetSlotsToSetupPose();
  131. skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton);
  132. // `GetRepackedSkin()` and each call to `GetRemappedClone()` with parameter `premultiplyAlpha` set to `true`
  133. // cache necessarily created Texture copies which can be cleared by calling AtlasUtilities.ClearCache().
  134. // You can optionally clear the textures cache after multiple repack operations.
  135. // Just be aware that while this cleanup frees up memory, it is also a costly operation
  136. // and will likely cause a spike in the framerate.
  137. AtlasUtilities.ClearCache();
  138. Resources.UnloadUnusedAssets();
  139. }
  140. void UpdateCharacterSkin () {
  141. var skeleton = skeletonAnimation.Skeleton;
  142. var skeletonData = skeleton.Data;
  143. characterSkin = new Skin("character-base");
  144. // Note that the result Skin returned by calls to skeletonData.FindSkin()
  145. // could be cached once in Start() instead of searching for the same skin
  146. // every time. For demonstration purposes we keep it simple here.
  147. characterSkin.AddSkin(skeletonData.FindSkin(baseSkin));
  148. characterSkin.AddSkin(skeletonData.FindSkin(noseSkins[activeNoseIndex]));
  149. characterSkin.AddSkin(skeletonData.FindSkin(eyelidsSkin));
  150. characterSkin.AddSkin(skeletonData.FindSkin(eyesSkins[activeEyesIndex]));
  151. characterSkin.AddSkin(skeletonData.FindSkin(hairSkins[activeHairIndex]));
  152. }
  153. void AddEquipmentSkinsTo (Skin combinedSkin) {
  154. var skeleton = skeletonAnimation.Skeleton;
  155. var skeletonData = skeleton.Data;
  156. combinedSkin.AddSkin(skeletonData.FindSkin(clothesSkin));
  157. combinedSkin.AddSkin(skeletonData.FindSkin(pantsSkin));
  158. if (!string.IsNullOrEmpty(bagSkin)) combinedSkin.AddSkin(skeletonData.FindSkin(bagSkin));
  159. if (!string.IsNullOrEmpty(hatSkin)) combinedSkin.AddSkin(skeletonData.FindSkin(hatSkin));
  160. }
  161. void UpdateCombinedSkin () {
  162. var skeleton = skeletonAnimation.Skeleton;
  163. var resultCombinedSkin = new Skin("character-combined");
  164. resultCombinedSkin.AddSkin(characterSkin);
  165. AddEquipmentSkinsTo(resultCombinedSkin);
  166. skeleton.SetSkin(resultCombinedSkin);
  167. skeleton.SetSlotsToSetupPose();
  168. }
  169. }
  170. }