hch
2025-10-17 cb653cf75b17b9bdca0b5b9e6b88edf1ca72e90c
Main/Component/UI/Effect/UIEffectPlayer.cs
@@ -5,129 +5,27 @@
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;
    protected override void OnEnable()
    //spine里的第几个动画,!!closePMA参数无效暂时留着
    public void Play(int index, bool showLog = true, bool closePMA = false)
    {
        if (isPlayOnEnable)
        {
            Play(false);
        }
        else if (spineComp != null)
        {
            //隐藏,会有静态显示问题
            spineComp.enabled = false;
        }
        playSpineAnimIndex = index;
        PlayAsync(showLog, closePMA).Forget();
    }
    public override void Play(bool showLog = true)
    //配置动画组数组索引
    public void PlayByArrIndex(int index, bool showLog = true, bool closePMA = false)
    {
        if (!isInit)
        {
            InitComponent(showLog);
            isInit = true;
        }
        else
        {
            //避免重复创建
            if (!this.gameObject.activeSelf)
            {
                this.gameObject.SetActive(true);
            }
            return;
        }
        if (EffectMgr.IsNotShowBySetting(effectId))
        {
            return;
        }
        if (null != effectTarget)
        {
            if (pool != null)
                pool.Release(effectTarget);
            effectTarget = null;
        }
        if (!this.gameObject.activeSelf)
        {
            this.gameObject.SetActive(true);
        }
        // 加载spine特效资源
        if (effectConfig.isSpine != 0)
        {
            PlaySpineEffect();
        }
        else
        {
            PlayerEffect(false);
        }
    }
    protected override void PlaySpineEffect()
    {
        if (spineComp.skeletonDataAsset == null)
        {
            //LoadAsset 已经有缓存SkeletonDataAsset
            spineComp.skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("UIEffect/" + effectConfig.packageName, effectConfig.fxName);
            spineComp.raycastTarget = false;
            spineComp.Initialize(true);
            spineAnimationState = spineComp.AnimationState;
            spineAnimationState.Complete -= OnSpineAnimationComplete;
            spineAnimationState.Complete += OnSpineAnimationComplete;
        }
        spineComp.enabled = true;
        PlayerFistSpineAnim();
    }
    // 播放第一个动画(作为默认动画)
    void PlayerFistSpineAnim()
    {
        spineComp.enabled = true;
        var skeletonData = spineComp.Skeleton.Data;
        if (skeletonData.Animations.Count > 0)
        {
            string defaultAnimationName = skeletonData.Animations.Items[0].Name;
            spineComp.AnimationState.SetAnimation(0, defaultAnimationName, isPlaySpineLoop);
        }
        else
        {
            Debug.LogError("Spine 数据中没有找到任何动画!" + effectConfig.id);
        }
    }
    //单次播放完毕就会触发,即使是循环
    protected override void OnSpineAnimationComplete(Spine.TrackEntry trackEntry)
    {
        if (!isPlaySpineLoop)
        {
            spineComp.enabled = false;
            if (isReleaseImmediately)
            {
                Stop();
            }
            else
            {
                onComplete?.Invoke();
            }
        }
        var config = EffectConfig.Get(effectId);
        playSpineAnimIndex = index < config.animIndex.Length ? config.animIndex[index] : 0;
        PlayAsync(showLog, closePMA).Forget();
    }
@@ -151,4 +49,13 @@
        return effectPlayer;
    }
   public void SetEnabled(bool isEnable)
   {
      if (spineComp == null)
      {
         return;
      }
      spineComp.enabled = isEnable;
   }
}