using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using GameCore; namespace GameLogic { /// /// UI深度适应 /// [RequireComponent(typeof(Canvas))] [RequireComponent(typeof(GraphicRaycaster))] public class UIDepthAdapter : MonoBehaviour { /// /// 目标画布 /// [SerializeField] Canvas targetCanvas; /// /// 自身画布相对于目标画布的偏移值 /// [SerializeField] int offset; /// /// 自身画布 /// Canvas canvas; /// /// 射线发射器 /// GraphicRaycaster graphicRaycaster; private void Awake() { canvas = this.gameObject.AddMissingComponent(); graphicRaycaster = this.gameObject.AddMissingComponent(); } private void Start() { if (targetCanvas == null && this.transform.parent != null) targetCanvas = this.transform.parent.gameObject.GetComponentInParent(); } private void Update() { if (canvas && targetCanvas) { int targetDepth = targetCanvas.sortingOrder + offset; canvas.overrideSorting = true; if (canvas.sortingOrder != targetDepth) { canvas.sortingOrder = targetDepth; } } } } }