EquipSystemExample.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 System.Collections.Generic;
  32. using UnityEngine;
  33. namespace Spine.Unity.Examples {
  34. public class EquipSystemExample : MonoBehaviour, IHasSkeletonDataAsset {
  35. // Implementing IHasSkeletonDataAsset allows Spine attribute drawers to automatically detect this component as a skeleton data source.
  36. public SkeletonDataAsset skeletonDataAsset;
  37. SkeletonDataAsset IHasSkeletonDataAsset.SkeletonDataAsset { get { return this.skeletonDataAsset; } }
  38. public Material sourceMaterial;
  39. public bool applyPMA = true;
  40. public List<EquipHook> equippables = new List<EquipHook>();
  41. public EquipsVisualsComponentExample target;
  42. public Dictionary<EquipAssetExample, Attachment> cachedAttachments = new Dictionary<EquipAssetExample, Attachment>();
  43. [System.Serializable]
  44. public class EquipHook {
  45. public EquipType type;
  46. [SpineSlot]
  47. public string slot;
  48. [SpineSkin]
  49. public string templateSkin;
  50. [SpineAttachment(skinField: "templateSkin")]
  51. public string templateAttachment;
  52. }
  53. public enum EquipType {
  54. Gun,
  55. Goggles
  56. }
  57. public void Equip (EquipAssetExample asset) {
  58. var equipType = asset.equipType;
  59. EquipHook howToEquip = equippables.Find(x => x.type == equipType);
  60. var skeletonData = skeletonDataAsset.GetSkeletonData(true);
  61. int slotIndex = skeletonData.FindSlot(howToEquip.slot).Index;
  62. var attachment = GenerateAttachmentFromEquipAsset(asset, slotIndex, howToEquip.templateSkin, howToEquip.templateAttachment);
  63. target.Equip(slotIndex, howToEquip.templateAttachment, attachment);
  64. }
  65. Attachment GenerateAttachmentFromEquipAsset (EquipAssetExample asset, int slotIndex, string templateSkinName, string templateAttachmentName) {
  66. Attachment attachment;
  67. cachedAttachments.TryGetValue(asset, out attachment);
  68. if (attachment == null) {
  69. var skeletonData = skeletonDataAsset.GetSkeletonData(true);
  70. var templateSkin = skeletonData.FindSkin(templateSkinName);
  71. Attachment templateAttachment = templateSkin.GetAttachment(slotIndex, templateAttachmentName);
  72. attachment = templateAttachment.GetRemappedClone(asset.sprite, sourceMaterial, premultiplyAlpha: this.applyPMA);
  73. // Note: Each call to `GetRemappedClone()` with parameter `premultiplyAlpha` set to `true` creates
  74. // a cached Texture copy which can be cleared by calling AtlasUtilities.ClearCache() as shown in the method below.
  75. cachedAttachments.Add(asset, attachment); // Cache this value for next time this asset is used.
  76. }
  77. return attachment;
  78. }
  79. public void Done () {
  80. target.OptimizeSkin();
  81. // `GetRepackedSkin()` and each call to `GetRemappedClone()` with parameter `premultiplyAlpha` set to `true`
  82. // creates cached Texture copies which can be cleared by calling AtlasUtilities.ClearCache().
  83. // You can optionally clear the textures cache after multiple repack operations.
  84. // Just be aware that while this cleanup frees up memory, it is also a costly operation
  85. // and will likely cause a spike in the framerate.
  86. //AtlasUtilities.ClearCache();
  87. //Resources.UnloadUnusedAssets();
  88. }
  89. }
  90. }