SpineLoader.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #if FAIRYGUI_SPINE
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Spine.Unity;
  5. namespace FairyGUI
  6. {
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. public partial class GLoader3D : GObject
  11. {
  12. SkeletonAnimation _spineAnimation;
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. /// <value></value>
  17. public SkeletonAnimation spineAnimation
  18. {
  19. get { return _spineAnimation; }
  20. }
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. /// <param name="asset"></param>
  25. /// <param name="width"></param>
  26. /// <param name="height"></param>
  27. /// <param name="anchor"></param>
  28. public void SetSpine(SkeletonDataAsset asset, int width, int height, Vector2 anchor)
  29. {
  30. SetSpine(asset, width, height, anchor, true);
  31. }
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. /// <param name="asset"></param>
  36. /// <param name="width"></param>
  37. /// <param name="height"></param>
  38. /// <param name="anchor"></param>
  39. /// <param name="cloneMaterial"></param>
  40. public void SetSpine(SkeletonDataAsset asset, int width, int height, Vector2 anchor, bool cloneMaterial)
  41. {
  42. if (_spineAnimation != null)
  43. FreeSpine();
  44. _content.customCloneMaterials = MaterialOverride;
  45. _content.customRecoverMaterials = CleanMaterialOverride;
  46. _spineAnimation = SkeletonRenderer.NewSpineGameObject<SkeletonAnimation>(asset);
  47. _spineAnimation.gameObject.name = asset.name;
  48. Spine.SkeletonData dat = asset.GetSkeletonData(false);
  49. _spineAnimation.gameObject.transform.localScale = new Vector3(1 / asset.scale, 1 / asset.scale, 1);
  50. _spineAnimation.gameObject.transform.localPosition = new Vector3(anchor.x, -anchor.y, 0);
  51. SetWrapTarget(_spineAnimation.gameObject, cloneMaterial, width, height);
  52. _spineAnimation.skeleton.R = _color.r;
  53. _spineAnimation.skeleton.G = _color.g;
  54. _spineAnimation.skeleton.B = _color.b;
  55. OnChangeSpine(null);
  56. }
  57. protected void LoadSpine()
  58. {
  59. SkeletonDataAsset asset = (SkeletonDataAsset)_contentItem.skeletonAsset;
  60. if (asset == null)
  61. return;
  62. SetSpine(asset, _contentItem.width, _contentItem.height, _contentItem.skeletonAnchor);
  63. }
  64. protected void OnChangeSpine(string propertyName)
  65. {
  66. if (_spineAnimation == null)
  67. return;
  68. if (propertyName == "color")
  69. {
  70. _spineAnimation.skeleton.R = _color.r;
  71. _spineAnimation.skeleton.G = _color.g;
  72. _spineAnimation.skeleton.B = _color.b;
  73. return;
  74. }
  75. var skeletonData = _spineAnimation.skeleton.Data;
  76. var state = _spineAnimation.AnimationState;
  77. Spine.Animation animationToUse = !string.IsNullOrEmpty(_animationName) ? skeletonData.FindAnimation(_animationName) : null;
  78. if (animationToUse != null)
  79. {
  80. var trackEntry = state.GetCurrent(0);
  81. if (trackEntry == null || trackEntry.Animation.Name != _animationName || trackEntry.IsComplete && !trackEntry.Loop)
  82. trackEntry = state.SetAnimation(0, animationToUse, _loop);
  83. else
  84. trackEntry.Loop = _loop;
  85. if (_playing)
  86. trackEntry.TimeScale = 1;
  87. else
  88. {
  89. trackEntry.TimeScale = 0;
  90. trackEntry.TrackTime = Mathf.Lerp(0, trackEntry.AnimationEnd - trackEntry.AnimationStart, _frame / 100f);
  91. }
  92. }
  93. else
  94. state.ClearTrack(0);
  95. var skin = !string.IsNullOrEmpty(skinName) ? skeletonData.FindSkin(skinName) : skeletonData.DefaultSkin;
  96. if (skin == null && skeletonData.Skins.Count > 0)
  97. skin = skeletonData.Skins.Items[0];
  98. if (_spineAnimation.skeleton.Skin != skin)
  99. {
  100. _spineAnimation.skeleton.SetSkin(skin);
  101. _spineAnimation.skeleton.SetSlotsToSetupPose();
  102. }
  103. }
  104. protected void FreeSpine()
  105. {
  106. if (_spineAnimation != null)
  107. {
  108. if (Application.isPlaying)
  109. GameObject.Destroy(_spineAnimation.gameObject);
  110. else
  111. GameObject.DestroyImmediate(_spineAnimation.gameObject);
  112. _content.customCloneMaterials = null;
  113. _content.customRecoverMaterials = null;
  114. }
  115. }
  116. protected void OnUpdateSpine(UpdateContext context)
  117. {
  118. if (_spineAnimation != null)
  119. _spineAnimation.skeleton.A = context.alpha * _content.alpha;
  120. }
  121. private void MaterialOverride(Dictionary<Material, Material> materials)
  122. {
  123. if (_spineAnimation != null)
  124. {
  125. foreach (var kv in materials)
  126. {
  127. _spineAnimation.CustomMaterialOverride[kv.Key] = kv.Value;
  128. }
  129. }
  130. }
  131. private void CleanMaterialOverride()
  132. {
  133. if (_spineAnimation != null)
  134. _spineAnimation.CustomMaterialOverride.Clear();
  135. }
  136. }
  137. }
  138. #endif