SpineLoader.cs 4.6 KB

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