using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Spine;
using Spine.Unity;
using UnityEngine;
public class SetSpineAnimations
{
// TODO 脚本编写制作者:小也
// TODO 此脚本方法是根据测试得来的效果,如果未明白其中原理勿动其中方法,实际情况会继续增加勿动谢谢配合!
/// <summary>
/// 默认播放方法单一播放
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <param name="loop">是否循环播放</param>
public void SinglePlayAnimation(SkeletonAnimation skeletonAnimation, string animationName, bool loop)
{
skeletonAnimation.AnimationState.SetAnimation(0, animationName, loop);
}
/// <summary>
/// 混合播放动画
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <param name="loop">是否循环播放</param>
/// <param name="mixDuration">混合持续时间</param>
public void MixPlayAnimation(SkeletonAnimation skeletonAnimation, string animationName, bool loop, float mixDuration)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
if (currentTrack != null)
{
skeletonAnimation.AnimationState.AddAnimation(0, animationName, loop, mixDuration);
}
else
{
skeletonAnimation.AnimationState.SetAnimation(0, animationName, loop);
}
}
/// <summary>
/// 添加动画到队列
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <param name="loop">是否循环播放</param>
/// <param name="delay">延迟时间</param>
public void AddAnimationToQueue(SkeletonAnimation skeletonAnimation, string animationName, bool loop, float delay)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
if (currentTrack != null)
{
skeletonAnimation.AnimationState.AddAnimation(0, animationName, loop, delay);
}
else
{
skeletonAnimation.AnimationState.SetAnimation(0, animationName, loop);
}
}
/// <summary>
/// 清空动画队列
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
public void ClearAnimationQueue(SkeletonAnimation skeletonAnimation)
{
skeletonAnimation.AnimationState.ClearTrack(0);
}
/// <summary>
/// 播放空动画
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
public void PlayEmptyAnimation(SkeletonAnimation skeletonAnimation)
{
skeletonAnimation.AnimationState.ClearTrack(0);
skeletonAnimation.AnimationState.SetEmptyAnimation(0, 0f);
}
/// <summary>
/// 获取动画持续时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <returns>动画持续时间</returns>
public float GetAnimationDuration(SkeletonAnimation skeletonAnimation, string animationName)
{
return skeletonAnimation.Skeleton.Data.FindAnimation(animationName).Duration;
}
/// <summary>
/// 暂停动画播放
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
public void PauseAnimation(SkeletonAnimation skeletonAnimation)
{
skeletonAnimation.AnimationState.TimeScale = 0f;
}
/// <summary>
/// 恢复动画播放
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
public void ResumeAnimation(SkeletonAnimation skeletonAnimation)
{
skeletonAnimation.AnimationState.TimeScale = 1f;
}
/// <summary>
/// 设置动画播放速度
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="speed">播放速度</param>
public void SetAnimationSpeed(SkeletonAnimation skeletonAnimation, float speed)
{
skeletonAnimation.AnimationState.TimeScale = speed;
}
/// <summary>
/// 设置动画播放进度
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="progress">播放进度 (0-1)</param>
public void SetAnimationProgress(SkeletonAnimation skeletonAnimation, float progress)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
if (currentTrack != null)
{
currentTrack.TrackTime = currentTrack.AnimationEnd * progress;
}
}
/// <summary>
/// 获取当前播放的动画名称
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <returns>当前播放的动画名称</returns>
public string GetCurrentAnimationName(SkeletonAnimation skeletonAnimation)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
if (currentTrack != null)
{
return currentTrack.Animation.Name;
}
return string.Empty;
}
/// <summary>
/// 判断动画是否正在播放
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <returns>是否正在播放</returns>
public bool IsAnimationPlaying(SkeletonAnimation skeletonAnimation, string animationName)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
if (currentTrack != null && currentTrack.Animation.Name == animationName)
{
return true;
}
return false;
}
/// <summary>
/// 设置动画事件回调
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="eventName">事件名称</param>
/// <param name="callback">回调函数</param>
public void SetAnimationEventCallback(SkeletonAnimation skeletonAnimation, string eventName, System.Action callback)
{
skeletonAnimation.AnimationState.Event += (trackEntry, e) =>
{
if (e.Data.Name == eventName)
{
callback?.Invoke();
}
};
}
/// <summary>
/// 在指定轨道上播放动画
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="animationName">动画名称</param>
/// <param name="loop">是否循环播放</param>
public void PlayAnimationOnTrack(SkeletonAnimation skeletonAnimation, int trackIndex, string animationName, bool loop)
{
skeletonAnimation.AnimationState.SetAnimation(trackIndex, animationName, loop);
}
/// <summary>
/// 清空指定轨道上的动画
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
public void ClearAnimationOnTrack(SkeletonAnimation skeletonAnimation, int trackIndex)
{
skeletonAnimation.AnimationState.ClearTrack(trackIndex);
}
/// <summary>
/// 设置动画混合模式
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="fromAnimationName">起始动画名称</param>
/// <param name="toAnimationName">目标动画名称</param>
/// <param name="mixDuration">混合持续时间</param>
public void SetAnimationMixMode(SkeletonAnimation skeletonAnimation, string fromAnimationName, string toAnimationName, float mixDuration)
{
skeletonAnimation.AnimationState.Data.SetMix(fromAnimationName, toAnimationName, mixDuration);
}
/// <summary>
/// 设置动画权重
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="weight">权重值</param>
public void SetAnimationWeight(SkeletonAnimation skeletonAnimation, int trackIndex, float weight)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (currentTrack != null)
{
currentTrack.Alpha = weight;
}
}
/// <summary>
/// 获取动画总时长
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <returns>动画总时长</returns>
public float GetAnimationTotalDuration(SkeletonAnimation skeletonAnimation)
{
float totalDuration = 0f;
foreach (var animation in skeletonAnimation.Skeleton.Data.Animations)
{
totalDuration += animation.Duration;
}
return totalDuration;
}
/// <summary>
/// 设置动画完成事件回调
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <param name="callback">回调函数</param>
public void SetAnimationCompleteCallback(SkeletonAnimation skeletonAnimation, string animationName, System.Action callback)
{
skeletonAnimation.AnimationState.Complete += (trackEntry) =>
{
if (trackEntry.Animation.Name == animationName)
{
callback?.Invoke();
}
};
}
/// <summary>
/// 设置动画开始事件回调
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <param name="callback">回调函数</param>
public void SetAnimationStartCallback(SkeletonAnimation skeletonAnimation, string animationName, System.Action callback)
{
skeletonAnimation.AnimationState.Start += (trackEntry) =>
{
if (trackEntry.Animation.Name == animationName)
{
callback?.Invoke();
}
};
}
/// <summary>
/// 设置动画中断事件回调
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <param name="callback">回调函数</param>
public void SetAnimationInterruptCallback(SkeletonAnimation skeletonAnimation, string animationName, System.Action callback)
{
skeletonAnimation.AnimationState.Interrupt += (trackEntry) =>
{
if (trackEntry.Animation.Name == animationName)
{
callback?.Invoke();
}
};
}
/// <summary>
/// 设置动画结束事件回调
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <param name="callback">回调函数</param>
public void SetAnimationEndCallback(SkeletonAnimation skeletonAnimation, string animationName, System.Action callback)
{
skeletonAnimation.AnimationState.End += (trackEntry) =>
{
if (trackEntry.Animation.Name == animationName)
{
callback?.Invoke();
}
};
}
/// <summary>
/// 设置动画销毁事件回调
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <param name="callback">回调函数</param>
public void SetAnimationDisposeCallback(SkeletonAnimation skeletonAnimation, string animationName, System.Action callback)
{
skeletonAnimation.AnimationState.Dispose += (trackEntry) =>
{
if (trackEntry.Animation.Name == animationName)
{
callback?.Invoke();
}
};
}
/// <summary>
/// 获取当前动画的播放时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <returns>当前动画的播放时间</returns>
public float GetCurrentAnimationTime(SkeletonAnimation skeletonAnimation)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
if (currentTrack != null)
{
return currentTrack.TrackTime;
}
return 0f;
}
/// <summary>
/// 获取当前动画的剩余时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <returns>当前动画的剩余时间</returns>
public float GetCurrentAnimationRemainingTime(SkeletonAnimation skeletonAnimation)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
if (currentTrack != null)
{
return currentTrack.AnimationEnd - currentTrack.TrackTime;
}
return 0f;
}
/// <summary>
/// 获取当前动画的循环次数
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <returns>当前动画的循环次数</returns>
public int GetCurrentAnimationLoopCount(SkeletonAnimation skeletonAnimation)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
if (currentTrack != null)
{
return currentTrack.Loop ? int.MaxValue : 1;
}
return 0;
}
/// <summary>
/// 设置动画的循环次数
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="loopCount">循环次数</param>
public void SetAnimationLoopCount(SkeletonAnimation skeletonAnimation, int trackIndex, int loopCount)
{
var currentTrack = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (currentTrack != null)
{
currentTrack.Loop = loopCount == int.MaxValue;
}
}
/// <summary>
/// 判断当前是否有动画正在播放
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <returns>是否有动画正在播放</returns>
public bool IsAnyAnimationPlaying(SkeletonAnimation skeletonAnimation)
{
return skeletonAnimation.AnimationState.GetCurrent(0) != null;
}
/// <summary>
/// 获取所有可用的动画名称
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <returns>所有可用的动画名称</returns>
public List<string> GetAllAnimationNames(SkeletonAnimation skeletonAnimation)
{
return new List<string>(skeletonAnimation.Skeleton.Data.Animations.Select(a => a.Name));
}
/// <summary>
/// 判断指定名称的动画是否存在
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="animationName">动画名称</param>
/// <returns>指定名称的动画是否存在</returns>
public bool AnimationExists(SkeletonAnimation skeletonAnimation, string animationName)
{
return skeletonAnimation.Skeleton.Data.FindAnimation(animationName) != null;
}
/// <summary>
/// 获取当前正在播放的所有动画的名称
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <returns>当前正在播放的所有动画的名称</returns>
public List<string> GetCurrentPlayingAnimationNames(SkeletonAnimation skeletonAnimation)
{
return skeletonAnimation.AnimationState.Tracks
.Where(t => t != null)
.Select(t => t.Animation.Name)
.ToList();
}
/// <summary>
/// 设置动画的播放次数(非循环)
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="animationName">动画名称</param>
/// <param name="playTimes">播放次数</param>
public void PlayAnimationWithTimes(SkeletonAnimation skeletonAnimation, int trackIndex, string animationName, int playTimes)
{
skeletonAnimation.AnimationState.SetAnimation(trackIndex, animationName, false);
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.TrackEnd = track.AnimationEnd * playTimes;
}
}
/// <summary>
/// 清空所有轨道上的动画
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
public void ClearAllTracks(SkeletonAnimation skeletonAnimation)
{
skeletonAnimation.AnimationState.ClearTracks();
}
/// <summary>
/// 设置动画的起始时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="startTime">起始时间</param>
public void SetAnimationStartTime(SkeletonAnimation skeletonAnimation, int trackIndex, float startTime)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.TrackTime = startTime;
}
}
/// <summary>
/// 获取指定动画的播放次数
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>指定动画的播放次数</returns>
public int GetAnimationPlayTimes(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return Mathf.FloorToInt(track.TrackEnd / track.AnimationEnd);
}
return 0;
}
/// <summary>
/// 设置动画的结束事件回调
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="callback">回调函数</param>
public void SetAnimationEndEventCallback(SkeletonAnimation skeletonAnimation, int trackIndex, System.Action callback)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.Complete += (trackEntry) =>
{
callback?.Invoke();
};
}
}
/// <summary>
/// 设置动画的自定义事件回调
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="eventName">事件名称</param>
/// <param name="callback">回调函数</param>
public void SetAnimationCustomEventCallback(SkeletonAnimation skeletonAnimation, int trackIndex, string eventName, System.Action<Spine.Event> callback)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.Event += (trackEntry, e) =>
{
if (e.Data.Name == eventName)
{
callback?.Invoke(e);
}
};
}
}
/// <summary>
/// 获取当前动画的混合时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的混合时间</returns>
public float GetAnimationMixTime(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.MixTime;
}
return 0f;
}
/// <summary>
/// 获取当前动画的混合持续时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的混合持续时间</returns>
public float GetAnimationMixDuration(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.MixDuration;
}
return 0f;
}
/// <summary>
/// 设置动画的播放时间比例
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="timeScale">时间比例</param>
public void SetAnimationTimeScale(SkeletonAnimation skeletonAnimation, int trackIndex, float timeScale)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.TimeScale = timeScale;
}
}
/// <summary>
/// 获取当前动画的播放方向
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的播放方向</returns>
public bool GetAnimationPlayDirection(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.TimeScale >= 0;
}
return true;
}
/// <summary>
/// 设置动画的播放方向
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="isForward">是否正向播放</param>
public void SetAnimationPlayDirection(SkeletonAnimation skeletonAnimation, int trackIndex, bool isForward)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.TimeScale = isForward ? Mathf.Abs(track.TimeScale) : -Mathf.Abs(track.TimeScale);
}
}
/// <summary>
/// 获取当前动画的播放次数
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的播放次数</returns>
public int GetAnimationPlayCount(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return Mathf.FloorToInt(track.TrackTime / track.AnimationEnd);
}
return 0;
}
/// <summary>
/// 获取当前动画的总时长
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的总时长</returns>
public float GetAnimationTotalDuration(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.AnimationEnd;
}
return 0f;
}
/// <summary>
/// 判断当前动画是否已完成
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画是否已完成</returns>
public bool IsAnimationComplete(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.TrackTime >= track.TrackEnd;
}
return true;
}
/// <summary>
/// 获取当前动画的混合权重
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的混合权重</returns>
public float GetAnimationMixWeight(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.Alpha;
}
return 0f;
}
/// <summary>
/// 设置动画的混合权重
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="mixWeight">混合权重</param>
public void SetAnimationMixWeight(SkeletonAnimation skeletonAnimation, int trackIndex, float mixWeight)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.Alpha = mixWeight;
}
}
/// <summary>
/// 获取当前动画的延迟时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的延迟时间</returns>
public float GetAnimationDelay(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.Delay;
}
return 0f;
}
/// <summary>
/// 设置动画的延迟时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="delay">延迟时间</param>
public void SetAnimationDelay(SkeletonAnimation skeletonAnimation, int trackIndex, float delay)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.Delay = delay;
}
}
/// <summary>
/// 判断当前动画是否正在淡入
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画是否正在淡入</returns>
public bool IsAnimationFadingIn(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.TimeScale > 0 && track.TrackTime < track.MixDuration;
}
return false;
}
/// <summary>
/// 判断当前动画是否正在淡出
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画是否正在淡出</returns>
public bool IsAnimationFadingOut(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.TimeScale < 0 && track.TrackTime > track.TrackEnd - track.MixDuration;
}
return false;
}
/// <summary>
/// 获取当前动画的事件
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的事件列表</returns>
public List<Spine.Event> GetAnimationEvents(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.Animation.Timelines.OfType<EventTimeline>().SelectMany(t => t.Events).ToList();
}
return new List<Spine.Event>();
}
/// <summary>
/// 判断当前动画是否包含指定的事件
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="eventName">事件名称</param>
/// <returns>当前动画是否包含指定的事件</returns>
public bool HasAnimationEvent(SkeletonAnimation skeletonAnimation, int trackIndex, string eventName)
{
var events = GetAnimationEvents(skeletonAnimation, trackIndex);
return events.Exists(e => e.Data.Name == eventName);
}
/// <summary>
/// 获取当前动画的时间轴
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的时间轴列表</returns>
public List<Timeline> GetAnimationTimelines(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.Animation.Timelines.ToList();
}
return new List<Timeline>();
}
/// <summary>
/// 获取当前动画的循环次数
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的循环次数</returns>
public int GetAnimationLoop(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
if (track.Loop)
{
return 0;
}
else
{
return Mathf.FloorToInt(track.TrackTime / track.AnimationEnd);
}
}
return 0;
}
/// <summary>
/// 设置当前动画的混合时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="mixTime">混合时间</param>
public void SetAnimationMixTime(SkeletonAnimation skeletonAnimation, int trackIndex, float mixTime)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.MixTime = mixTime;
}
}
/// <summary>
/// 设置当前动画的混合持续时间
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <param name="mixDuration">混合持续时间</param>
public void SetAnimationMixDuration(SkeletonAnimation skeletonAnimation, int trackIndex, float mixDuration)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
track.MixDuration = mixDuration;
}
}
/// <summary>
/// 获取当前动画的时间比例
/// </summary>
/// <param name="skeletonAnimation">骨骼动画组件</param>
/// <param name="trackIndex">轨道索引</param>
/// <returns>当前动画的时间比例</returns>
public float GetAnimationTimeScale(SkeletonAnimation skeletonAnimation, int trackIndex)
{
var track = skeletonAnimation.AnimationState.GetCurrent(trackIndex);
if (track != null)
{
return track.TimeScale;
}
return 1f;
}
}
unity spine ugui不正常
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
下一篇:jQuery API参考
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
d多态不正常
d多态不正常
d 多态 ide 子类