From b0a5d4688f1af73b5ad03ccc2df11c9aac1523a9 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期二, 29 七月 2025 16:56:23 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.1.20:10010/r/Project_SG_scripts

---
 Main/Component/UI/Effect/EffectPlayer.cs |  245 ++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 193 insertions(+), 52 deletions(-)

diff --git a/Main/Component/UI/Effect/EffectPlayer.cs b/Main/Component/UI/Effect/EffectPlayer.cs
index 7e5122f..afd31a9 100644
--- a/Main/Component/UI/Effect/EffectPlayer.cs
+++ b/Main/Component/UI/Effect/EffectPlayer.cs
@@ -6,15 +6,43 @@
 using Spine;
 using UnityEngine.UI;
 
+// 鐗规晥鎾斁鍣紝瀵硅薄姹犵鐞嗭細unity鐗规晥鍜宻pine鐗规晥閮芥槸鍋氬ソ鐨勯鍒朵綋
+// unity鐗规晥棰勫埗浣撴槸鐗规晥甯堝湪鍒朵綔鐨勬椂鍊欑敓鎴愮殑锛寀nity鐗规晥绾﹀畾鎸傝浇璁剧疆鎾斁鏃堕暱鑴氭湰
+// spine鐗规晥鏄壒鏁堝笀鍒朵綔鐨勫姩鐢绘枃浠跺彲鐩存帴鍔犺浇鐢紝鍒朵綔鎴愰鍒朵綋鍙鍔燘oneFollower涔嬬被鐨勮繘琛岄�昏緫澶勭悊
+// 闈濽I鐗规晥浣跨敤锛孶I鐗规晥UIEffectPlayer缁ф壙EffectPlayer锛屽悗缁鏋滈�昏緫鍐茬獊澶у垯涓嶇敤缁ф壙
 public class EffectPlayer : MonoBehaviour
 {
-    public int effectId;
+    [SerializeField]
+    private int m_EffectID;
+    public int effectId
+    {
+        get
+        {
+            return m_EffectID;
+        }
+        set
+        {
+            if (value != m_EffectID)
+            {
+                isInit = false;
+                m_EffectID = value;
+            }
+        }
+    }
+
+
 
     public EffectConfig effectConfig;
 
-    public Action<EffectPlayer> onDestroy;
-
     public float speedRate = 1f;
+
+
+
+    
+    [Header("鎾斁瀹屾瘯绔嬪嵆鍥炴敹")]
+    public bool isReleaseImmediately = true;  //鐣岄潰鐗规晥涓�鑸笉闇�瑕佽嚜鎴戦攢姣侊紝璺熼殢鐣岄潰鎴栬�呯埗瀵硅薄閿�姣佸氨琛�
+
+    public Action<EffectPlayer> onDestroy;
 
     [HideInInspector] public Canvas canvas = null;
 
@@ -31,21 +59,33 @@
     protected List<Renderer> rendererList = new List<Renderer>();
 
     protected SkeletonGraphic spineComp;
+    protected Spine.AnimationState spineAnimationState;
 
-    protected void Start()
+    public GameObjectPoolManager.GameObjectPool pool;
+
+    public Action onComplete;
+
+    protected virtual void OnEnable()
     {
-        ReStart();
+        if (spineComp != null)
+        {
+            //闅愯棌锛屼細鏈夐潤鎬佹樉绀洪棶棰�
+            spineComp.enabled = false;
+        }
     }
 
-    protected void InitCompnent()
+    protected void InitComponent(bool showLog = true)
     {
         if (effectId <= 0)
         {
             effectConfig = null;
-            Debug.LogError("EffectPlayer effectId is not set");
 #if UNITY_EDITOR
-            UnityEditor.Selection.activeGameObject = gameObject;
-            UnityEditor.EditorGUIUtility.PingObject(gameObject);
+            if (showLog)
+            { 
+                Debug.LogError("EffectPlayer effectId is not set");
+                UnityEditor.Selection.activeGameObject = gameObject;
+                UnityEditor.EditorGUIUtility.PingObject(gameObject);
+            }
 #endif
             return;
         }
@@ -54,8 +94,8 @@
 
         if (null == effectConfig)
         {
-            Debug.LogError("could not find effect config, effect id is " + effectId);
 #if UNITY_EDITOR
+            Debug.LogError("could not find effect config, effect id is " + effectId);
             UnityEditor.Selection.activeGameObject = gameObject;
             UnityEditor.EditorGUIUtility.PingObject(gameObject);
 #endif
@@ -63,69 +103,93 @@
         }
 
 
+        Clear();
+        return;
+    }
+
+    protected virtual void Clear()
+    {
         particleList.Clear();
         animatorList.Clear();
         rendererList.Clear();
         spineComp = null;
-
-        if (effectConfig.isSpine != 0)
-        {
-            spineComp = gameObject.GetComponentInChildren<SkeletonGraphic>(true);
-        }
-        else
-        {
-            //  鏀堕泦缁勪欢
-            particleList.AddRange(gameObject.GetComponentsInChildren<ParticleSystem>(true));
-            animatorList.AddRange(gameObject.GetComponentsInChildren<Animator>(true));
-        }
-        rendererList.AddRange(gameObject.GetComponentsInChildren<Renderer>(true));
+        pool = null;
     }
 
-    public void Stop()
+    public virtual void Stop()
     {
         if (null != effectTarget)
         {
-            DestroyImmediate(effectTarget);
+            // 濡傛灉浣跨敤浜嗙壒鏁堥鍒朵綋姹狅紝鍒欏綊杩樺埌姹犱腑
+            if (pool != null)
+            {
+                pool.Release(effectTarget);
+            }
             effectTarget = null;
         }
 
         isInit = false;
 
-        particleList.Clear();
-        animatorList.Clear();
-        rendererList.Clear();
-        spineComp = null;
+        Clear();
+        onComplete?.Invoke();
     }
 
-    public void Play()
-    {
-        ReStart();
-    }
-
-
-    protected void ReStart()
+    public virtual void Play(bool showLog = true)
     {
         if (!isInit)
         {
+            InitComponent(showLog);
             isInit = true;
-            InitCompnent();
+        }
+        else
+        {
+            //閬垮厤閲嶅鍒涘缓
+            if (!this.gameObject.activeSelf)
+            {
+                this.gameObject.SetActive(true);
+            }
+            return;
         }
 
-        if (EffectMgr.Instance.IsNotShowBySetting(effectId))
+        if (EffectMgr.IsNotShowBySetting(effectId))
         {
             return;
         }
 
         if (null != effectTarget)
         {
-            DestroyImmediate(effectTarget);
+            if (pool != null)
+                pool.Release(effectTarget);
             effectTarget = null;
         }
 
-        //	YYL TODO
-        //	鍦ㄨ繖閲岃�冭檻鐢ㄦ睜鐨勮瘽鍙兘璧伴厤缃ソ涓�鐐� 鍘熸湰鐨勬槸鏃犺濡備綍閮借蛋姹� 浣嗘槸瀹為檯涓婃湁浜涚壒鏁堝苟涓嶉渶瑕�
+        if (!this.gameObject.activeSelf)
+        {
+            this.gameObject.SetActive(true);
+        }
 
-        // 鍔犺浇鐗规晥璧勬簮
+        PlayerEffect(true);
+
+    }
+
+
+    protected virtual void PlaySpineEffect()
+    {
+        spineComp = gameObject.GetComponentInChildren<SkeletonGraphic>(true);
+        spineComp.raycastTarget = false;
+        spineComp.Initialize(true);
+        spineAnimationState = spineComp.AnimationState;
+        spineAnimationState.Complete -= OnSpineAnimationComplete;
+        spineAnimationState.Complete += OnSpineAnimationComplete;
+
+        // 澶栧眰鎺у埗鍏蜂綋鎾斁鍝釜鍔ㄧ敾
+        spineComp.enabled = true;
+
+        return;
+    }
+
+    protected virtual void PlayerEffect(bool playSpine)
+    { 
         var effectPrefab = ResManager.Instance.LoadAsset<GameObject>("UIEffect/" + effectConfig.packageName, effectConfig.fxName);
         if (effectPrefab == null)
         {
@@ -133,14 +197,43 @@
             return;
         }
 
-        // 瀹炰緥鍖栫壒鏁�
-        effectTarget = Instantiate(effectPrefab, transform);
+        if (effectConfig.isSpine == 0)
+        {
+            //unity鐗规晥 鍒ゆ柇棰勫埗浣撴槸鍚︽寕杞紼ffectTime
+            var timeObj = effectPrefab.GetComponent<EffectTime>();
+            if (null == timeObj)
+            {
+                Debug.LogError($"{effectPrefab.name}棰勫埗浣撴病鏈夋寕杞紼ffectTime");
+                return;
+            }
+        }
+
+        // 浠庣壒鏁堥鍒朵綋姹犺幏鍙栫壒鏁�
+        pool = GameObjectPoolManager.Instance.RequestPool(effectPrefab);
+        effectTarget = pool.Request();
+        // 璁剧疆鐖惰妭鐐瑰拰浣嶇疆
+        effectTarget.transform.SetParent(transform);
+        effectTarget.transform.localPosition = Vector3.zero;
+        effectTarget.transform.localScale = Vector3.one;
+        effectTarget.transform.localRotation = Quaternion.identity;
         effectTarget.name = $"Effect_{effectConfig.fxName}";
 
-        //  鎬濊�冧竴涓嬪湪娌℃湁鎸傚湪鑺傜偣鐨勬椂鍊�
+        if (effectConfig.isSpine != 0 && playSpine)
+        {
+            //棰勫埗浣�
+            PlaySpineEffect();
+        }
+        else
+        {
+            //鎸傝浇缁勪欢鍚� 寮�濮嬫敹闆�
+            particleList.AddRange(gameObject.GetComponentsInChildren<ParticleSystem>(true));
+            animatorList.AddRange(gameObject.GetComponentsInChildren<Animator>(true));
+            rendererList.AddRange(gameObject.GetComponentsInChildren<Renderer>(true));
+            OnUnityAnimationComplete();
+        }
+
         if (null == canvas)
             canvas = GetComponentInParent<Canvas>();
-
 
         // 娣诲姞鐗规晥绌块�忛樆鎸″櫒
         blocker = effectTarget.AddMissingComponent<EffectPenetrationBlocker>();
@@ -149,12 +242,6 @@
         if (canvas != null)
         {
             blocker.SetParentCanvas(canvas);
-        }
-
-        // 鑷姩閿�姣�
-        if (effectConfig.autoDestroy != 0)
-        {
-            Destroy(gameObject, effectConfig.destroyDelay);
         }
     }
 
@@ -166,11 +253,51 @@
             onDestroy.Invoke(this);
             onDestroy = null;
         }
+        // 鍋滄鐗规晥骞跺綊杩樺埌姹犱腑
+        Stop();
+
+        if (spineAnimationState != null)
+        {
+            spineAnimationState.Complete -= OnSpineAnimationComplete;
+        }
+
+
     }
+
+    //鍗曟鎾斁瀹屾瘯灏变細瑙﹀彂锛屽嵆浣挎槸寰幆
+    protected virtual void OnSpineAnimationComplete(Spine.TrackEntry trackEntry)
+    {
+        if (isReleaseImmediately)
+        { 
+            spineComp.enabled = false;   
+            Stop();
+        }
+    }
+
+
+    private void OnUnityAnimationComplete()
+    {
+        var timeObj = effectTarget.GetComponent<EffectTime>();
+        if (!timeObj.isLoop)
+        {
+            this.SetWait(timeObj.playTime);
+            this.DoWaitRestart();
+            this.OnWaitCompelete(OnEffectComplete);
+        }
+    }
+
+    private void OnEffectComplete(Component comp)
+    {
+        this.DoWaitStop();
+        if (isReleaseImmediately)
+            Stop();
+    }
+
 
     //  鍒涘缓鍚庣殑鐗规晥浼氳嚜鍔ㄩ殣钘� 闇�瑕佹墜鍔ㄨ皟鐢≒lay鎵嶈兘鎾斁
     public static EffectPlayer Create(int effectId, Transform parent, bool createNewChild = false)
     {
+        // 鐩存帴鍒涘缓鐗规晥鎾斁鍣紝涓嶄娇鐢ㄥ璞℃睜
         EffectPlayer effectPlayer = null;
 
         if (createNewChild)
@@ -182,11 +309,23 @@
         else
         {
             effectPlayer = parent.AddMissingComponent<EffectPlayer>();
-            effectPlayer.effectId = effectId;
         }
+
+        effectPlayer.effectId = effectId;
         effectPlayer.SetActive(true);
         return effectPlayer;
     }
+
+    /// <summary>
+    /// 璁剧疆娓告垙瀵硅薄婵�娲荤姸鎬�
+    // /// </summary>
+    // public void SetActive(bool active)
+    // {
+    //     if (gameObject != null)
+    //     {
+    //         gameObject.SetActive(active);
+    //     }
+    // }
 
     public void Pause()
     {
@@ -284,4 +423,6 @@
             return;
         }
     }
+    
+
 }

--
Gitblit v1.8.0