From 8df61a1638922c3be24887da905a297d92473f75 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期一, 23 六月 2025 14:23:26 +0800
Subject: [PATCH] 特效问题

---
 Main/System/UIBase/UIBase.cs                  |  136 +--------------------------------
 Main/System/Main/MainWin.cs                   |    4 
 Main/Component/UI/Effect/EffectPlayer.cs      |   77 +++++++++++++++++++
 Main/Component/UI/Effect/EffectPlayer.cs.meta |   11 ++
 4 files changed, 97 insertions(+), 131 deletions(-)

diff --git a/Main/Component/UI/Effect/EffectPlayer.cs b/Main/Component/UI/Effect/EffectPlayer.cs
new file mode 100644
index 0000000..4370458
--- /dev/null
+++ b/Main/Component/UI/Effect/EffectPlayer.cs
@@ -0,0 +1,77 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class EffectPlayer : MonoBehaviour
+{
+	public int effectId;
+
+	public bool autoDestroy = false;
+
+	public float destroyDelay = 0f;
+
+	public Canvas canvas = null;
+
+	public GameObject effectTarget = null;
+
+	protected void Start()
+	{
+		if (EffectMgr.Instance.IsNotShowBySetting(effectId))
+		{
+			return;
+		}
+
+		if (null != effectTarget)
+		{
+			DestroyImmediate(effectTarget);
+			effectTarget = null;
+		}
+
+        EffectConfig effectCfg = EffectConfig.Get(effectId);
+
+        if (null == effectCfg)
+        {
+            return;
+        }
+
+        //	YYL TODO
+        //	鍦ㄨ繖閲岃�冭檻鐢ㄦ睜鐨勮瘽鍙兘璧伴厤缃ソ涓�鐐� 鍘熸湰鐨勬槸鏃犺濡備綍閮借蛋姹� 浣嗘槸瀹為檯涓婃湁浜涚壒鏁堝苟涓嶉渶瑕�
+
+        // 鍔犺浇鐗规晥璧勬簮
+        var effectPrefab = ResManager.Instance.LoadAsset<GameObject>("UIEffect/" + effectCfg.packageName, effectCfg.fxName);
+        if (effectPrefab == null)
+        {
+            Debug.LogError($"鍔犺浇UI鐗规晥澶辫触: {effectCfg.packageName}");
+            return;
+        }
+        
+        // 瀹炰緥鍖栫壒鏁�
+        effectTarget = Instantiate(effectPrefab, transform);
+        effectTarget.name = $"Effect_{effectCfg.packageName}";
+        
+        if (null == canvas)
+	        canvas = GetComponentInParent<Canvas>();
+
+	    if (null == canvas)
+	    {
+	    	Debug.LogError("can not find canvas for UIEffect " + effectId);
+	    	return;
+	    }
+
+        // 娣诲姞鐗规晥绌块�忛樆鎸″櫒
+        EffectPenetrationBlocker blocker = effectTarget.AddComponent<EffectPenetrationBlocker>();
+        blocker.parentCanvas = canvas;
+
+        //  寤惰繜涓�甯ф墠鐢熸晥
+        this.DelayFrame(blocker.UpdateSortingOrder);
+
+        // blocker.UpdateSortingOrder();
+        
+        // 鑷姩閿�姣�
+        if (autoDestroy)
+        {
+            Destroy(effectTarget, destroyDelay);
+        }
+	}
+
+}
diff --git a/Main/Component/UI/Effect/EffectPlayer.cs.meta b/Main/Component/UI/Effect/EffectPlayer.cs.meta
new file mode 100644
index 0000000..ff103b2
--- /dev/null
+++ b/Main/Component/UI/Effect/EffectPlayer.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 9fd18e7f2fdcf8a4ab668f35a4d6817d
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Main/System/Main/MainWin.cs b/Main/System/Main/MainWin.cs
index dc8fd7f..76a04cc 100644
--- a/Main/System/Main/MainWin.cs
+++ b/Main/System/Main/MainWin.cs
@@ -13,7 +13,7 @@
     // 搴曢儴鎸夐挳缁�
     public Button[] bottomTabButtons;
 
-    private GameObject[] bottomTabEffects;
+    private EffectPlayer[] bottomTabEffects;
     
     // 褰撳墠閫変腑鐨勫簳閮ㄦ爣绛剧储寮�
     private int currentTabIndex = 0;
@@ -68,7 +68,7 @@
     protected override void OnPreOpen()
     {
         base.OnPreOpen();
-        bottomTabEffects = new GameObject[bottomTabButtons.Length];
+        bottomTabEffects = new EffectPlayer[bottomTabButtons.Length];
         for (int i = 0; i < bottomTabButtons.Length; i++)
         {
             bottomTabEffects[i] = PlayUIEffect(1004, bottomTabButtons[i].transform, false);
diff --git a/Main/System/UIBase/UIBase.cs b/Main/System/UIBase/UIBase.cs
index 35878f9..0e12e51 100644
--- a/Main/System/UIBase/UIBase.cs
+++ b/Main/System/UIBase/UIBase.cs
@@ -329,143 +329,21 @@
     /// <param name="autoDestroy">鏄惁鑷姩閿�姣侊紝榛樿涓簍rue</param>
     /// <param name="destroyDelay">鑷姩閿�姣佸欢杩熸椂闂达紝榛樿涓�5绉�</param>
     /// <returns>鐗规晥娓告垙瀵硅薄</returns>
-    public GameObject PlayUIEffect(int id, Transform parent = null, bool autoDestroy = true, float destroyDelay = 5f)
+    public EffectPlayer PlayUIEffect(int id, Transform parent = null, bool autoDestroy = true, float destroyDelay = 5f)
     {
         // 浣跨敤榛樿鍊�
         if (parent == null) parent = transform;
         
-        EffectConfig effectCfg = EffectConfig.Get(id);
+        EffectPlayer player = parent.gameObject.AddComponent<EffectPlayer>();
 
-        if (null == effectCfg)
-        {
-            return null;
-        }
+        player.effectId = id;
+        player.autoDestroy = autoDestroy;
+        player.destroyDelay = destroyDelay;
+        player.canvas = canvas;
 
-        // 鍔犺浇鐗规晥璧勬簮
-        var effectPrefab = ResManager.Instance.LoadAsset<GameObject>("UIEffect/" + effectCfg.packageName, effectCfg.fxName);
-        if (effectPrefab == null)
-        {
-            Debug.LogError($"鍔犺浇UI鐗规晥澶辫触: {effectCfg.packageName}");
-            return null;
-        }
-        
-        // 瀹炰緥鍖栫壒鏁�
-        GameObject effectObj = Instantiate(effectPrefab, parent);
-        effectObj.name = $"Effect_{effectCfg.packageName}";
-        
-        // 娣诲姞鐗规晥绌块�忛樆鎸″櫒
-        EffectPenetrationBlocker blocker = effectObj.AddComponent<EffectPenetrationBlocker>();
-        blocker.parentCanvas = canvas;
-
-        //  寤惰繜涓�甯ф墠鐢熸晥
-        this.DelayFrame(blocker.UpdateSortingOrder);
-
-        // blocker.UpdateSortingOrder();
-        
-        // 鑷姩閿�姣�
-        if (autoDestroy)
-        {
-            Destroy(effectObj, destroyDelay);
-        }
-        
-        return effectObj;
+        return player;
     }
     
-    /// <summary>
-    /// 鍦ㄤ袱涓猆I鍏冪礌涔嬮棿鎾斁鐗规晥锛堟寜鐓ortingOrder鐨勪腑闂村�硷級
-    /// </summary>
-    /// <param name="effectName">鐗规晥璧勬簮鍚嶇О</param>
-    /// <param name="frontElement">鍓嶆櫙UI鍏冪礌锛圛mage鎴朢awImage锛�</param>
-    /// <param name="backElement">鑳屾櫙UI鍏冪礌锛圛mage鎴朢awImage锛�</param>
-    /// <param name="autoDestroy">鏄惁鑷姩閿�姣侊紝榛樿涓簍rue</param>
-    /// <param name="destroyDelay">鑷姩閿�姣佸欢杩熸椂闂达紝榛樿涓�5绉�</param>
-    /// <returns>鐗规晥娓告垙瀵硅薄</returns>
-    public async UniTask<GameObject> PlayEffectBetweenUIElements(string effectName, Graphic frontElement, Graphic backElement, bool autoDestroy = true, float destroyDelay = 5f)
-    {
-        if (frontElement == null || backElement == null)
-        {
-            Debug.LogError("鍓嶆櫙鎴栬儗鏅疷I鍏冪礌涓虹┖");
-            return null;
-        }
-        
-        // 纭繚UI鍏冪礌鍦ㄥ綋鍓峌IBase鐨凜anvas涓�
-        if (frontElement.canvas != canvas || backElement.canvas != canvas)
-        {
-            Debug.LogError("UI鍏冪礌涓嶅湪褰撳墠UIBase鐨凜anvas涓�");
-            return null;
-        }
-        
-        // 鍔犺浇鐗规晥璧勬簮
-        GameObject effectPrefab = ResManager.Instance.LoadAsset<GameObject>("UIEffect", effectName);
-        if (effectPrefab == null)
-        {
-            Debug.LogError($"鍔犺浇UI鐗规晥澶辫触: {effectName}");
-            return null;
-        }
-        
-        // 鍒涘缓涓�涓柊鐨凣ameObject浣滀负鐗规晥瀹瑰櫒
-        GameObject container = new GameObject($"EffectContainer_{effectName}");
-        container.transform.SetParent(transform, false);
-        
-        // 璁剧疆瀹瑰櫒浣嶇疆
-        RectTransform containerRect = container.AddComponent<RectTransform>();
-        containerRect.anchorMin = new Vector2(0.5f, 0.5f);
-        containerRect.anchorMax = new Vector2(0.5f, 0.5f);
-        containerRect.pivot = new Vector2(0.5f, 0.5f);
-        containerRect.anchoredPosition = Vector2.zero;
-        containerRect.sizeDelta = new Vector2(100, 100); // 榛樿澶у皬锛屽彲浠ユ牴鎹渶瑕佽皟鏁�
-        
-        // 鑾峰彇鍓嶆櫙鍜岃儗鏅厓绱犵殑siblingIndex
-        int frontIndex = frontElement.transform.GetSiblingIndex();
-        int backIndex = backElement.transform.GetSiblingIndex();
-        
-        // 璁剧疆鐗规晥瀹瑰櫒鐨剆iblingIndex鍦ㄤ袱鑰呬箣闂�
-        if (frontIndex > backIndex)
-        {
-            // 鍓嶆櫙鍦ㄨ儗鏅箣鍚庯紝鐗规晥搴旇鍦ㄤ腑闂�
-            container.transform.SetSiblingIndex((frontIndex + backIndex) / 2 + 1);
-        }
-        else
-        {
-            // 鑳屾櫙鍦ㄥ墠鏅箣鍚庯紝鐗规晥搴旇鍦ㄤ腑闂�
-            container.transform.SetSiblingIndex((frontIndex + backIndex) / 2);
-        }
-        
-        // 瀹炰緥鍖栫壒鏁�
-        GameObject effectObj = Instantiate(effectPrefab, container.transform);
-        effectObj.name = $"Effect_{effectName}";
-        
-        // 娣诲姞鐗规晥绌块�忛樆鎸″櫒
-        EffectPenetrationBlocker blocker = effectObj.AddComponent<EffectPenetrationBlocker>();
-        
-        // 鐩存帴璁剧疆鐗规晥娓叉煋鍣ㄧ殑鎺掑簭椤哄簭
-        Renderer[] renderers = effectObj.GetComponentsInChildren<Renderer>(true);
-        foreach (Renderer renderer in renderers)
-        {
-            renderer.sortingOrder = canvas.sortingOrder;
-            renderer.sortingLayerName = canvas.sortingLayerName;
-        }
-        
-        // 璁剧疆绮掑瓙绯荤粺娓叉煋鍣ㄧ殑鎺掑簭椤哄簭
-        ParticleSystem[] particleSystems = effectObj.GetComponentsInChildren<ParticleSystem>(true);
-        foreach (ParticleSystem ps in particleSystems)
-        {
-            ParticleSystemRenderer psRenderer = ps.GetComponent<ParticleSystemRenderer>();
-            if (psRenderer != null)
-            {
-                psRenderer.sortingOrder = canvas.sortingOrder;
-                psRenderer.sortingLayerName = canvas.sortingLayerName;
-            }
-        }
-        
-        // 鑷姩閿�姣�
-        if (autoDestroy)
-        {
-            Destroy(container, destroyDelay);
-        }
-        
-        return effectObj;
-    }
     #endregion
 
     public bool raycastTarget

--
Gitblit v1.8.0