18 子 2D卡牌客户端搭建 / 2D卡牌客户端搭建 特效问题调整Canvas:overlay->camera
4个文件已修改
4个文件已添加
132 ■■■■ 已修改文件
Main/Component/UI/Common/MountCanvasCamera.cs 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/Component/UI/Common/MountCanvasCamera.cs.meta 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/System/Main/MainWin.cs 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/System/UIBase/UIBase.cs 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/Utility/CameraManager.cs 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/Utility/EffectPenetrationBlocker.cs 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/Utility/UniTaskExtension.cs 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/Utility/UniTaskExtension.cs.meta 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/Component/UI/Common/MountCanvasCamera.cs
New file
@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MountCanvasCamera : MonoBehaviour
{
    void Awake()
    {
        Canvas canvas = GetComponent<Canvas>();
        if (null != canvas)
        {
            canvas.worldCamera = CameraManager.uiCamera;
        }
    }
}
Main/Component/UI/Common/MountCanvasCamera.cs.meta
New file
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e331f42a91da7ef4c84f7c5472adbeb7
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant:
Main/System/Main/MainWin.cs
@@ -12,6 +12,8 @@
    // 底部按钮组
    private Button[] bottomTabButtons;
    private GameObject[] bottomTabEffects;
    
    // 当前选中的底部标签索引
    private int currentTabIndex = 0;
@@ -29,11 +31,13 @@
        windowBackground = transform.Find("RawImgBackground").gameObject;
        bottomTabButtons = new Button[5];
        for (int i = 1; i <= 5; i++)
        {
            string buttonName = "Buttons/Button" + i;
            bottomTabButtons[i-1] = transform.Find(buttonName).GetComponent<Button>();
            #if UNITY_EDITOR
            //测试代码
            #endif
        }
        // 初始化UI组件事件
@@ -53,6 +57,7 @@
                OnBottomTabButtonClicked(index);
            });
        }
    }
    
    protected override void OnOpen()
@@ -71,6 +76,26 @@
        UpdatePlayerInfo();
        UpdateCurrency();
    }
    protected override void OnPreOpen()
    {
        base.OnPreOpen();
        bottomTabEffects = new GameObject[bottomTabButtons.Length];
        for (int i = 0; i < bottomTabButtons.Length; i++)
        {
            bottomTabEffects[i] = PlayUIEffect(1004, bottomTabButtons[i].transform, false);
        }
    }
    protected override void OnPreClose()
    {
        base.OnPreClose();
        foreach (var effectGO in bottomTabEffects)
        {
            DestroyImmediate(effectGO);
        }
        bottomTabEffects = null;
    }
    
    /// <summary>
    /// 更新玩家信息
Main/System/UIBase/UIBase.cs
@@ -172,6 +172,8 @@
        // 设置Canvas属性
        canvas.overrideSorting = true;
        canvas.worldCamera = CameraManager.uiCamera;
        // 获取或添加CanvasGroup组件
        canvasGroup = GetComponent<CanvasGroup>();
        if (canvasGroup == null)
@@ -317,7 +319,7 @@
    /// <param name="autoDestroy">是否自动销毁,默认为true</param>
    /// <param name="destroyDelay">自动销毁延迟时间,默认为5秒</param>
    /// <returns>特效游戏对象</returns>
    public async UniTask<GameObject> PlayUIEffect(int id, Transform parent = null, bool autoDestroy = true, float destroyDelay = 5f)
    public GameObject PlayUIEffect(int id, Transform parent = null, bool autoDestroy = true, float destroyDelay = 5f)
    {
        // 使用默认值
        if (parent == null) parent = transform;
@@ -344,7 +346,11 @@
        // 添加特效穿透阻挡器
        EffectPenetrationBlocker blocker = effectObj.AddComponent<EffectPenetrationBlocker>();
        blocker.parentCanvas = canvas;
        blocker.UpdateSortingOrder();
        //  延迟一帧才生效
        this.DelayFrame(blocker.UpdateSortingOrder);
        // blocker.UpdateSortingOrder();
        
        // 自动销毁
        if (autoDestroy)
Main/Utility/CameraManager.cs
@@ -1,6 +1,7 @@
using UnityEngine;
using System.Collections;
public class CameraManager
{
    /// <summary>
@@ -11,20 +12,11 @@
    {
        get
        {
            if (null == m_UICamera)
            {
                m_UICamera = GameObject.Find("UICamera").GetComponent<Camera>();
            }
            return m_UICamera;
        }
        set
        {
            m_UICamera = value;
        }
    }
    private static Camera m_SceneCamera = null;
    public static Camera sceneCamera
    {
        get { return m_SceneCamera; }
        set { m_SceneCamera = value; }
    }
}
Main/Utility/EffectPenetrationBlocker.cs
@@ -26,9 +26,9 @@
    public Canvas parentCanvas;
    
    [Tooltip("特效在Canvas中的排序偏移量")]
    public int sortingOrderOffset = 1;
    public int _sortingOrderOffset = 0;
    
    private int canvasSortingOrder = 0;
    public int canvasSortingOrder = 0;
    
    private void Awake()
    {
@@ -88,7 +88,7 @@
        }
        
        // 将特效的排序顺序设置为Canvas排序顺序加上偏移量
        ApplySortingSettings(canvasSortingOrder + sortingOrderOffset, customSortingLayer);
        ApplySortingSettings(canvasSortingOrder + _sortingOrderOffset, customSortingLayer);
    }
    
    /// <summary>
@@ -96,12 +96,15 @@
    /// </summary>
    private void ApplySortingSettings(int sortingOrder, string sortingLayer)
    {
        // const int overlayRQ = 4000;
        // 应用到渲染器
        foreach (Renderer renderer in effectRenderers)
        {
            if (renderer != null)
            {
                renderer.sortingOrder = sortingOrder;
                // renderer.material.renderQueue = overlayRQ;
                if (!string.IsNullOrEmpty(sortingLayer))
                {
                    renderer.sortingLayerName = sortingLayer;
@@ -118,6 +121,7 @@
                if (psRenderer != null)
                {
                    psRenderer.sortingOrder = sortingOrder;
                    // psRenderer.material.renderQueue = overlayRQ;
                    if (!string.IsNullOrEmpty(sortingLayer))
                    {
                        psRenderer.sortingLayerName = sortingLayer;
@@ -153,4 +157,12 @@
            UpdateSortingOrder();
        }
    }
#if UNITY_EDITOR
    [ContextMenu("Apply")]
    public void Apply()
    {
        UpdateSortingOrder();
    }
#endif
}
Main/Utility/UniTaskExtension.cs
New file
@@ -0,0 +1,22 @@
using UnityEngine;
using Cysharp.Threading.Tasks;
using System;
public static class UniTaskExtension
{
    public static void DelayFrame(this GameObject go, Action action)
    {
        DelayFrameInternal(action);
    }
    public static void DelayFrame(this Component cmp, Action action)
    {
        DelayFrameInternal(action);
    }
    private async static UniTask DelayFrameInternal(Action action)
    {
        await UniTask.DelayFrame(1);
        action?.Invoke();
    }
}
Main/Utility/UniTaskExtension.cs.meta
New file
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 66bafc3f6f9d7a44197ce6cc49cd26fc
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant: