| | |
| | | using UnityEngine; |
| | | using Spine; |
| | | using UnityEngine.UI; |
| | | using Cysharp.Threading.Tasks; |
| | | |
| | | //UI特效播放器,spine特效直接加载无需提前做预制体 |
| | | //UI特效大部分情况不会改变特效,无需销毁 |
| | | public class UIEffectPlayer : EffectPlayer |
| | | { |
| | | |
| | | [Header("是否循环播放spine特效")] |
| | | public bool isPlaySpineLoop = false; |
| | | |
| | | [Header("是否在显示时播放")] |
| | | public bool isPlayOnEnable = false; |
| | | |
| | | [Header("延迟播放(毫秒)")] |
| | | public int playDelayTime = 0; |
| | | |
| | | int playSpineAnimIndex = -1; //播放spine特效动画索引, |
| | | |
| | | protected override void OnEnable() |
| | | { |
| | | playSpineAnimIndex = -1; |
| | | if (isPlayOnEnable) |
| | | { |
| | | Play(false); |
| | | PlayAsync(false).Forget(); |
| | | } |
| | | else if (spineComp != null) |
| | | { |
| | |
| | | if (!this.gameObject.activeSelf) |
| | | { |
| | | this.gameObject.SetActive(true); |
| | | } |
| | | if (effectConfig.isSpine != 0) |
| | | { |
| | | PlayerTheSpineAnim(); |
| | | } |
| | | return; |
| | | } |
| | |
| | | spineComp.skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("UIEffect/" + effectConfig.packageName, effectConfig.fxName); |
| | | spineComp.raycastTarget = false; |
| | | spineComp.Initialize(true); |
| | | |
| | | // 检查动画是否有相加模式 |
| | | bool hasAdditiveBlend = CheckForAdditiveBlend(spineComp.Skeleton); |
| | | if (hasAdditiveBlend) |
| | | { |
| | | spineComp.material = ResManager.Instance.LoadAsset<Material>("UIEffect/" + effectConfig.packageName, effectConfig.fxName.Split('_')[0] + "_Material-Additive"); |
| | | } |
| | | else |
| | | { |
| | | spineComp.material = null; |
| | | } |
| | | |
| | | spineAnimationState = spineComp.AnimationState; |
| | | spineAnimationState.Data.DefaultMix = 0f; |
| | | spineAnimationState.Complete -= OnSpineAnimationComplete; |
| | |
| | | } |
| | | |
| | | spineComp.enabled = true; |
| | | PlayerFistSpineAnim(); |
| | | PlayerTheSpineAnim(); |
| | | } |
| | | |
| | | private bool CheckForAdditiveBlend(Spine.Skeleton skeleton) |
| | | { |
| | | // 遍历所有插槽,检查是否有相加模式 |
| | | foreach (var slot in skeleton.Slots) |
| | | { |
| | | if (slot.Data.BlendMode == Spine.BlendMode.Additive) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | // 播放第一个动画(作为默认动画) |
| | | void PlayerFistSpineAnim() |
| | | public void Play(int index, bool showLog = true) |
| | | { |
| | | playSpineAnimIndex = index; |
| | | PlayAsync(showLog).Forget(); |
| | | } |
| | | |
| | | async UniTask PlayAsync(bool showLog = true) |
| | | { |
| | | await UniTask.Delay(playDelayTime); |
| | | Play(showLog); |
| | | } |
| | | |
| | | // 播放指定动画 |
| | | void PlayerTheSpineAnim() |
| | | { |
| | | spineComp.enabled = true; |
| | | var skeletonData = spineComp.Skeleton.Data; |
| | | if (skeletonData.Animations.Count > 0) |
| | | { |
| | | string defaultAnimationName = skeletonData.Animations.Items[0].Name; |
| | | string defaultAnimationName = skeletonData.Animations.Items[playSpineAnimIndex == -1 ? effectConfig.animIndex : playSpineAnimIndex].Name; |
| | | spineAnimationState.SetAnimation(0, defaultAnimationName, isPlaySpineLoop); |
| | | } |
| | | else |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | // 创建后的特效会自动隐藏 需要手动调用Play才能播放 |
| | | public static UIEffectPlayer CreateEffect(int effectId, Transform parent, bool createNewChild = false) |
| | | { |