using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace WS { [ObjectSystem] public class ViewCanvasAwake : AwakeSystem<ViewCanvas, Transform, ViewShowModeType> { public override void Awake(ViewCanvas self, Transform a, ViewShowModeType b) { self.Awake(a, b); } } ///<summary>视图画布</summary> public class ViewCanvas : WSComponent { //根节点 public Transform UIRoot; //画布的缩放 private CanvasScaler UICanvasScaler; //Groups public Dictionary<ViewGroup, Transform> GroupDic = new Dictionary<ViewGroup, Transform>(); //视图类型 public ViewShowModeType ViewShowMode; public void Awake(Transform a, ViewShowModeType viewshow) { UIRoot = a; ViewShowMode = viewshow; #if PLATFORM_PICO if (ViewShowMode != ViewShowModeType.Screen) { int count = UIRoot.childCount; for (int i = 0; i < count; i++) { UnityEngine.Object.Destroy(UIRoot.GetChild(i).gameObject); } return; } #endif UICanvasScaler = UIRoot.GetComponent<CanvasScaler>(); string[] groups = Enum.GetNames(typeof(ViewGroup)); for (int i = 0; i < groups.Length; i++) { Transform group = UIRoot.Find(groups[i]); if (group != null) { ViewGroup form = (ViewGroup)Enum.Parse(typeof(ViewGroup), groups[i]); GroupDic[form] = group; } } if (ViewShowMode == ViewShowModeType.Screen) { AdaptationCanvasScaler(); } } ///<summary>绘制</summary> public void ShowCanvas(View view) { if (view.ShowModeType != ViewShowModeType.Screen) { RectTransform rect = view.UIGameObject.GetComponent<RectTransform>(); BoxCollider collider = view.UIGameObject.AddComponent<BoxCollider>(); collider.size = new Vector3(rect.sizeDelta.x, rect.sizeDelta.y, 1); } #if PLATFORM_PICO if (view.ShowModeType != ViewShowModeType.Screen) { GameObject WorldCanvas = GamePoolManager.Instance.CreateObject(UIRoot.gameObject); view.UIGameObject.transform.SetParent(WorldCanvas.transform, false); view.UIGameObject.transform.localPosition = Vector3.zero; view.UIGameObject.transform.localScale = Vector3.one; view.UIGameObject.transform.SetAsLastSibling(); return; } #endif Transform group = GetGroup(view.Group); if (group != null) { view.UIGameObject.transform.SetParent(group, false); view.UIGameObject.transform.localPosition = Vector3.zero; view.UIGameObject.transform.localScale = Vector3.one; if (ViewShowMode == ViewShowModeType.Screen) { RectTransform rect = view.UIGameObject.GetComponent<RectTransform>(); rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0); rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0); rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; } view.UIGameObject.transform.SetAsLastSibling(); //移动到末尾 } } ///<summary>适配分辨率</summary> public void AdaptationCanvasScaler() { ViewManager manager = (ViewManager)Parent; if (manager.CurrStandardScreen >= manager.StandardScreen) { UICanvasScaler.matchWidthOrHeight = 1; } else { UICanvasScaler.matchWidthOrHeight = manager.StandardScreen - manager.CurrStandardScreen; } } ///<summary>获取Group</summary> private Transform GetGroup(ViewGroup form) { Transform group = null; if (GroupDic.ContainsKey(form)) { GroupDic.TryGetValue(form, out group); } else { Log.LogError($"ViewGroup:{form}物体不存在"); } return group; } public override void Dispose() { base.Dispose(); UIRoot = null; GroupDic.Clear(); UICanvasScaler = null; } } }