hch
2025-10-17 cb653cf75b17b9bdca0b5b9e6b88edf1ca72e90c
Main/Component/UI/Effect/BattleEffectPlayer.cs
@@ -30,13 +30,51 @@
        }
    }
    public bool isBindBone = false;
    public bool isRedCamp = true;
    public EffectConfig effectConfig;
    public float speedRate = 1.5f;
    private float m_Alpha = 1f;
    public float Alpha
    {
        get
        {
            return m_Alpha;
        }
        set
        {
            if (value == m_Alpha)
                return;
            m_Alpha = value;
            OnAlphaChanged();
        }
    }
    private void OnAlphaChanged()
    {
        if (spineComp != null)
        {
            var skeleton = spineComp.Skeleton;
            skeleton.A = Alpha;
            spineComp.LateUpdate();
        }
        for (int i = 0; i < rendererList.Count; i++)
        {
            var renderer = rendererList[i];
            if (renderer != null && renderer.material != null && renderer.material.HasProperty("_Color"))
            {
                Color color = renderer.material.color;
                color.a = Alpha;
                renderer.material.color = color;
            }
        }
    }
    [Header("播放完毕立即回收")]
    public bool isReleaseImmediately = false;  //界面特效一般不需要自我销毁,跟随界面或者父对象销毁就行
@@ -60,19 +98,23 @@
    protected SkeletonAnimation spineComp;
    protected Spine.AnimationState spineAnimationState;
    protected int heroSetedSortingOrder;
    public GameObjectPoolManager.GameObjectPool pool;
    public Action onComplete;
    private bool isPlaying = false;
    private float timer = 0f;
    protected virtual void OnEnable()
    {
        if (spineComp != null)
        {
            //隐藏,会有静态显示问题
            spineComp.enabled = false;
        }
        // if (spineComp != null)
        // {
        //     //隐藏,会有静态显示问题
        //     spineComp.enabled = false;
        // }
        ApplySortingOrder();
    }
@@ -124,6 +166,8 @@
        {
            rectTrans.localScale *= effectConfig.effectScale;
        }
        spineComp.loop = effectConfig.isLoop != 0;
    }
    protected virtual void Clear()
@@ -157,24 +201,29 @@
        onComplete?.Invoke();
    }
    public void SetSortingOrder(int _heroSetedSortingOrder)
    public Func<bool> funcIsHeroFront;
    public void SetSortingOrder(Func<bool> _isHeroFrontCallback)
    {
        heroSetedSortingOrder = _heroSetedSortingOrder;
        funcIsHeroFront = _isHeroFrontCallback;
        int so = heroSetedSortingOrder;
        ApplySortingOrder();
    }
        if (null != blocker && effectConfig != null)
    public void ApplySortingOrder()
    {
        if (null != blocker && effectConfig != null && funcIsHeroFront != null)
        {
            if (BattleConst.UnactiveHeroSortingOrder == heroSetedSortingOrder)
            {
                so = effectConfig.frontBack == 1 ? BattleConst.UnactiveHeroFrontSortingOrder : BattleConst.UnactiveHeroBackSortingOrder;
            }
            else
            {
                so = effectConfig.frontBack == 1 ? BattleConst.ActiveHeroFrontSortingOrder : BattleConst.ActiveHeroBackSortingOrder;
            }
            bool isEffectFront = effectConfig.frontBack == 1;
            blocker.SetSortingOrder(so);
            bool isHeroFront = funcIsHeroFront();
            int finalSortingOrder = isHeroFront ?
                (isEffectFront ? BattleConst.ActiveHeroActionSortingOrder : BattleConst.ActiveHeroBackSortingOrder) : (isEffectFront ? BattleConst.UnactiveHeroFrontSortingOrder : BattleConst.UnactiveHeroBackSortingOrder);
            blocker.SetSortingOrder(finalSortingOrder);
        }
    }
@@ -212,15 +261,34 @@
            this.gameObject.SetActive(true);
        }
        if (effectConfig.autoDestroy != 0)
        {
            GameObject.Destroy(gameObject, effectConfig.destroyDelay / speedRate);
        }
        isPlaying = true;
        timer = 0f;
        PlayEffect();
    }
    public void Run()
    {
        if (null == effectConfig)
        {
            return;
        }
        if (!isPlaying)
        {
            return;
        }
        if (effectConfig.autoDestroy != 0)
        {
            timer += Time.deltaTime * speedRate;
            if (timer >= effectConfig.destroyDelay)
            {
                GameObject.DestroyImmediate(gameObject);
            }
        }
    }
    protected virtual void PlayEffect()
@@ -232,7 +300,7 @@
        }
        //  如果delay小于等于0 那会立刻执行
        this.DelayTime(effectConfig.delayPlay, () =>
        this.DelayTime(effectConfig.delayPlay / speedRate, () =>
        {
            PlayEffectInternal();
        });
@@ -248,6 +316,7 @@
        {
            PlayUnityEffect();
        }
        OnAlphaChanged();
    }
    protected void PlaySpineEffect()
@@ -265,6 +334,8 @@
        spineComp.Initialize(true);
        spineComp.timeScale = speedRate;
        spineComp.skeleton.A = Alpha;
        spineAnimationState = spineComp.state;
        spineAnimationState.Complete -= OnSpineAnimationComplete;
        spineAnimationState.Complete += OnSpineAnimationComplete;
@@ -273,6 +344,8 @@
        blocker = spineComp.AddMissingComponent<RendererAdjuster>();
        blocker.onSortingChanged = OnSortingChanged;
        ApplySortingOrder();
        spineComp.enabled = true;
@@ -354,7 +427,7 @@
    public string sortingLayer;
    public int sortingOrder;
    private RectTransform rectTrans;
    public RectTransform rectTrans;
    protected void OnSortingChanged(string _sortingLayer, int _sortingOrder)
    {
@@ -451,7 +524,7 @@
    public void Pause()
    {
        if (effectTarget == null) return;
        // if (effectTarget == null) return;
        // Spine动画
        // var spineGraphics = effectTarget.GetComponentsInChildren<SkeletonGraphic>(true);
@@ -459,6 +532,11 @@
        if (spineComp != null)
        {
            spineComp.timeScale = 0f;
        }
        if (spineAnimationState != null)
        {
            spineAnimationState.TimeScale = 0f;
        }
        // Animator动画
@@ -476,11 +554,16 @@
    public void Resume()
    {
        if (effectTarget == null) return;
        // if (effectTarget == null) return;
        if (spineComp != null)
        {
            spineComp.timeScale = speedRate;
        }
        if (spineAnimationState != null)
        {
            spineAnimationState.TimeScale = speedRate;
        }
        // Animator动画
@@ -544,4 +627,30 @@
            animator.speed = speedRate;
        }
    }
    public void BindBone(SkeletonAnimation skeletonAnim, string v)
    {
        Bone bone = skeletonAnim.skeleton.FindBone(v);
        if (null == bone)
        {
             return;
        }
        isBindBone = true;
        BoneFollower boneFollower = gameObject.AddMissingComponent<BoneFollower>();
        boneFollower.boneName = v;
        boneFollower.skeletonRenderer = skeletonAnim;
        boneFollower.followBoneRotation = false;
        boneFollower.followXYPosition = true;
        boneFollower.followZPosition = false;
        boneFollower.followLocalScale = false;
        boneFollower.followParentWorldScale = false;
        boneFollower.followSkeletonFlip = false;
        boneFollower.Initialize();
        boneFollower.LateUpdate();
    }
}