1- // GameFrameX 组织下的以及组织衍生的项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
2- //
3- // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE 文件。
4- //
5- // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
6-
7- using System ;
8- using UnityEngine ;
9-
10- namespace Spine . Unity
11- {
12- public static class SkeletonAnimationExtension
13- {
14- /// <summary>
15- /// 播放Spine骨骼动画
16- /// </summary>
17- /// <param name="skeletonAnimation">Spine骨骼动画组件</param>
18- /// <param name="animationName">要播放的动画名称</param>
19- /// <param name="loop">是否循环播放动画,true为循环播放,false为播放一次</param>
20- /// <param name="onComplete">动画播放完成时的回调函数,仅在非循环模式下生效</param>
21- public static void PlayAnimation ( this SkeletonAnimation skeletonAnimation , string animationName , bool loop = false , Action onComplete = null )
22- {
23- PlayAnimation ( skeletonAnimation , animationName , 0 , loop , onComplete ) ;
24- }
25-
26- /// <summary>
27- /// 在指定轨道上播放Spine骨骼动画
28- /// </summary>
29- /// <param name="skeletonAnimation">Spine骨骼动画组件</param>
30- /// <param name="animationName">要播放的动画名称</param>
31- /// <param name="trackIndex">动画轨道索引,用于混合多个动画,默认为0</param>
32- /// <param name="loop">是否循环播放动画,true为循环播放,false为播放一次</param>
33- /// <param name="onComplete">动画播放完成时的回调函数,仅在非循环模式下生效</param>
34- public static void PlayAnimation ( this SkeletonAnimation skeletonAnimation , string animationName , int trackIndex = 0 , bool loop = false , Action onComplete = null )
35- {
36- // 定义动画完成时的回调处理函数
37- void StateOnComplete ( TrackEntry trackEntry )
38- {
39- // 移除事件监听,避免重复触发
40- trackEntry . Complete -= StateOnComplete ;
41- // 调用用户传入的完成回调
42- onComplete ? . Invoke ( ) ;
43- }
44-
45- if ( skeletonAnimation . AnimationState != null )
46- {
47- // 确保移除之前可能存在的完成事件监听
48- skeletonAnimation . AnimationState . Complete -= StateOnComplete ;
49- // 非循环模式下添加完成事件监听
50- if ( ! loop )
51- {
52- skeletonAnimation . AnimationState . Complete += StateOnComplete ;
53- }
54-
55- // 设置并播放指定轨道上的动画
56- skeletonAnimation . AnimationState . SetAnimation ( trackIndex , animationName , loop ) ;
57- }
58- else
59- {
60- Debug . LogError ( "AnimationState Is null" ) ;
61- }
62- }
63- }
1+ // GameFrameX 组织下的以及组织衍生的项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
2+ //
3+ // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE 文件。
4+ //
5+ // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
6+
7+ using System ;
8+ using UnityEngine ;
9+
10+ namespace Spine . Unity
11+ {
12+ public static class SkeletonAnimationExtension
13+ {
14+ /// <summary>
15+ /// 获取SkeletonAnimation的渲染排序层级
16+ /// </summary>
17+ /// <param name="skeletonAnimation">SkeletonAnimation组件</param>
18+ /// <returns>返回Renderer的sortingOrder;若未找到Renderer则返回默认值0</returns>
19+ public static int GetSortingOrder ( this SkeletonAnimation skeletonAnimation )
20+ {
21+ var render = skeletonAnimation . GetComponent < Renderer > ( ) ;
22+ if ( render == null )
23+ {
24+ return default ;
25+ }
26+
27+ return render . sortingOrder ;
28+ }
29+
30+ /// <summary>
31+ /// 设置SkeletonAnimation的渲染排序层级
32+ /// </summary>
33+ /// <param name="skeletonAnimation">SkeletonAnimation组件</param>
34+ /// <param name="sortingOrder">要设置的排序层级数值</param>
35+ public static void SetSortingOrder ( this SkeletonAnimation skeletonAnimation , int sortingOrder )
36+ {
37+ var render = skeletonAnimation . GetComponent < Renderer > ( ) ;
38+ if ( render == null )
39+ {
40+ return ;
41+ }
42+
43+ render . sortingOrder = sortingOrder ;
44+ }
45+
46+ /// <summary>
47+ /// 播放Spine骨骼动画
48+ /// </summary>
49+ /// <param name="skeletonAnimation">Spine骨骼动画组件</param>
50+ /// <param name="animationName">要播放的动画名称</param>
51+ /// <param name="loop">是否循环播放动画,true为循环播放,false为播放一次</param>
52+ /// <param name="onComplete">动画播放完成时的回调函数,仅在非循环模式下生效</param>
53+ public static void PlayAnimation ( this SkeletonAnimation skeletonAnimation , string animationName , bool loop = false , Action onComplete = null )
54+ {
55+ PlayAnimation ( skeletonAnimation , animationName , 0 , loop , onComplete ) ;
56+ }
57+
58+ /// <summary>
59+ /// 在指定轨道上播放Spine骨骼动画
60+ /// </summary>
61+ /// <param name="skeletonAnimation">Spine骨骼动画组件</param>
62+ /// <param name="animationName">要播放的动画名称</param>
63+ /// <param name="trackIndex">动画轨道索引,用于混合多个动画,默认为0</param>
64+ /// <param name="loop">是否循环播放动画,true为循环播放,false为播放一次</param>
65+ /// <param name="onComplete">动画播放完成时的回调函数,仅在非循环模式下生效</param>
66+ public static void PlayAnimation ( this SkeletonAnimation skeletonAnimation , string animationName , int trackIndex = 0 , bool loop = false , Action onComplete = null )
67+ {
68+ // 定义动画完成时的回调处理函数
69+ void StateOnComplete ( TrackEntry trackEntry )
70+ {
71+ // 移除事件监听,避免重复触发
72+ trackEntry . Complete -= StateOnComplete ;
73+ // 调用用户传入的完成回调
74+ onComplete ? . Invoke ( ) ;
75+ }
76+
77+ if ( skeletonAnimation . AnimationState != null )
78+ {
79+ // 确保移除之前可能存在的完成事件监听
80+ skeletonAnimation . AnimationState . Complete -= StateOnComplete ;
81+ // 非循环模式下添加完成事件监听
82+ if ( ! loop )
83+ {
84+ skeletonAnimation . AnimationState . Complete += StateOnComplete ;
85+ }
86+
87+ // 设置并播放指定轨道上的动画
88+ skeletonAnimation . AnimationState . SetAnimation ( trackIndex , animationName , loop ) ;
89+ }
90+ else
91+ {
92+ Debug . LogError ( "AnimationState Is null" ) ;
93+ }
94+ }
95+ }
6496}
0 commit comments