142 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			142 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C#
		
	
	
| 
 | |
| namespace Guru.UI
 | |
| {
 | |
|     using System;
 | |
|     using UnityEngine;
 | |
|     using UnityEngine.UI;
 | |
|     
 | |
|     public class UIRoot : ViewBase
 | |
|     {
 | |
|         const string PrefabPath = "guru/ui/root_canvas";
 | |
|         private static UIRoot _instance;
 | |
|         public static UIRoot Instance
 | |
|         {
 | |
|             get
 | |
|             {
 | |
|                 if (_instance == null) _instance = LoadInstance();
 | |
|                 return _instance;   
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         
 | |
|         
 | |
|         /// <summary>
 | |
|         /// CloneObject 对象
 | |
|         /// </summary>
 | |
|         /// <returns></returns>
 | |
|         private static UIRoot LoadInstance()
 | |
|         {
 | |
|             var ui = CloneResources<UIRoot>(PrefabPath);
 | |
|             ui.Init();
 | |
|             return ui;
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public static T Clone<T>(GameObject prefab, Transform container = null) where T :Component
 | |
|         {
 | |
|             var go = Instantiate(prefab, container);
 | |
|             if (go != null)
 | |
|             {
 | |
|                 go.name = nameof(T).ToLower();
 | |
|                 return go.GetComponent<T>();
 | |
|             }
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|         public static T CloneResources<T>(string path, Transform container = null) where T : Component
 | |
|         {
 | |
|             var prefab = Resources.Load<GameObject>(path);
 | |
|             if( null != prefab) return Clone<T>(prefab, container);
 | |
|             return null;
 | |
|         }
 | |
|         
 | |
|         
 | |
|         
 | |
|         private Canvas _canvas;
 | |
|         private CanvasScaler _canvasScaler;
 | |
|         private Transform _rootNode;
 | |
|         
 | |
|         
 | |
| 
 | |
|         private void Init()
 | |
|         {
 | |
|             DontDestroyOnLoad(gameObject);
 | |
|             gameObject.name = "guru_ui";
 | |
|             _rootNode = CreateEmptyNode("root");
 | |
| 
 | |
|             _canvas = gameObject.GetComponent<Canvas>();
 | |
|             _canvasScaler = gameObject.GetComponent<CanvasScaler>();
 | |
|         }
 | |
| 
 | |
| 
 | |
| 
 | |
|      
 | |
| 
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 引用分辨率
 | |
|         /// </summary>
 | |
|         public Vector2 ReferenceResolution
 | |
|         {
 | |
|             get => _canvasScaler.referenceResolution;
 | |
|             set => _canvasScaler.referenceResolution = value;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 宽高适配
 | |
|         /// </summary>
 | |
|         public float MatchWidthOrHeight
 | |
|         {
 | |
|             get => _canvasScaler.matchWidthOrHeight;
 | |
|             set => _canvasScaler.matchWidthOrHeight = value;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 屏幕渲染 Order 
 | |
|         /// </summary>
 | |
|         public int SortingOrder
 | |
|         {
 | |
|             get => _canvas.sortingOrder;
 | |
|             set => _canvas.sortingOrder = value;
 | |
|         }
 | |
| 
 | |
| 
 | |
| 
 | |
|         #region 节点操作
 | |
| 
 | |
|         
 | |
|         public RectTransform CreateEmptyNode(string nodeName = "node", Transform parent = null)
 | |
|         {
 | |
|             if (parent == null) parent = transform;
 | |
|             var go = new GameObject(nodeName);
 | |
|             go.transform.SetParent(parent, false);
 | |
|             go.transform.localScale = Vector3.one;
 | |
|             var c = go.AddComponent<RectTransform>();
 | |
|             c.anchorMax = Vector2.one;
 | |
|             c.anchorMin = Vector2.zero;
 | |
|             c.sizeDelta = Vector2.zero;
 | |
|             c.anchoredPosition = Vector2.zero;
 | |
|             c.pivot = Vector2.one * 0.5f;
 | |
|             
 | |
|             return c;
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public RectTransform CreateNodeFromPath(string path, Transform parent = null)
 | |
|         {
 | |
|             var names = path.Split('/');
 | |
|             if (parent == null) parent = _rootNode;
 | |
|             var node = parent as RectTransform;
 | |
|             foreach (var n in names)
 | |
|             {
 | |
|                 node = CreateEmptyNode(n, node);
 | |
|             }
 | |
|             return node;
 | |
|         }
 | |
| 
 | |
| 
 | |
|         #endregion
 | |
|         
 | |
| 
 | |
|     }
 | |
| } |