UI框架
设计主菜单面板:
Image组件→SourceImage修改背景面板
Project中的图片资源必须设置Texture Type 为Sprite(2D and UI)才能被设置为UI背景图
按住shift+alt按键等比例的缩小放大
UI自适应问题:即UI画布在不同分辨率现实设备上的显示问题,锚点和Point的设计原理:4锚点个锚点到个Point点的距离时固定不变的,及子物体相对父物体的大小变化是有父物体锚点和子物体中心点的距离和位置关系决定的。
相关链接
https://blog.csdn.net/wkhabc/article/details/52294493
预制体设置:
预制体修改名称后,如想把修改后的游戏对象再次设为预制体应先将其拖入Project目录,若Prefab含有子对象prefab,修改后prefab单击apply保存设置,先修改子prefab再修改父prefab。
UIManager using UnityEngine; using System.Collections; using System.Collections.Generic; using System; public class UIManager { /// /// 单例模式的核心 /// 1,定义一个静态的对象 在外界访问 在内部构造 /// 2,构造方法私有化 private static UIManager _instance; public static UIManager Instance { get { if (_instance == null) { _instance = new UIManager(); } return _instance; } } private Transform canvasTransform; private Transform CanvasTransform { get { if (canvasTransform == null) { canvasTransform = GameObject.Find("Canvas").transform; } return canvasTransform; } } private Dictionary<UIPanelType, string> panelPathDict;//存储所有面板Prefab的路径 private Dictionary<UIPanelType, BasePanel> panelDict;//保存所有实例化面板的游戏物体身上的BasePanel组件 private Stack<BasePanel> panelStack; private UIManager() { ParseUIPanelTypeJson(); } /// <summary> /// 把某个页面入栈, 把某个页面显示在界面上 /// </summary> public void PushPanel(UIPanelType panelType) { if (panelStack == null) panelStack = new Stack<BasePanel>(); //判断一下栈里面是否有页面 if (panelStack.Count > 0) { BasePanel topPanel = panelStack.Peek(); topPanel.OnPause(); } BasePanel panel = GetPanel(panelType); panel.OnEnter(); panelStack.Push(panel); } /// <summary> /// 出栈 ,把页面从界面上移除 /// </summary> public void PopPanel() { if (panelStack == null) panelStack = new Stack<BasePanel>(); if (panelStack.Count <= 0) return; //关闭栈顶页面的显示 BasePanel topPanel = panelStack.Pop(); topPanel.OnExit(); if (panelStack.Count <= 0) return; BasePanel topPanel2 = panelStack.Peek(); topPanel2.OnResume(); } /// <summary> /// 根据面板类型 得到实例化的面板 /// </summary> /// <returns></returns> private BasePanel GetPanel(UIPanelType panelType) { if (panelDict == null) { panelDict = new Dictionary<UIPanelType, BasePanel>(); } //BasePanel panel; //panelDict.TryGetValue(panelType, out panel);//TODO BasePanel panel = panelDict.TryGet(panelType); if (panel == null) { //如果找不到,那么就找这个面板的prefab的路径,然后去根据prefab去实例化面板 //string path; //panelPathDict.TryGetValue(panelType, out path); string path = panelPathDict.TryGet(panelType); GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject; instPanel.transform.SetParent(CanvasTransform,false); panelDict.Add(panelType, instPanel.GetComponent<BasePanel>()); return instPanel.GetComponent<BasePanel>(); } else { return panel; } } [Serializable] class UIPanelTypeJson { public List<UIPanelInfo> infoList; } private void ParseUIPanelTypeJson() { panelPathDict = new Dictionary<UIPanelType, string>(); TextAsset ta = Resources.Load<TextAsset>("UIPanelType"); UIPanelTypeJson jsonObject = JsonUtility.FromJson<UIPanelTypeJson>(ta.text); foreach (UIPanelInfo info in jsonObject.infoList) { //Debug.Log(info.panelType); panelPathDict.Add(info.panelType, info.path); } } /// <summary> /// just for test /// </summary> public void Test() { string path ; panelPathDict.TryGetValue(UIPanelType.Knapsack,out path); Debug.Log(path); } }
GameRoot using UnityEngine; using System.Collections; public class GameRoot : MonoBehaviour { // Use this for initialization void Start () { UIManager.Instance.PushPanel(UIPanelType.MainMenu); } }
DictionaryExtension using UnityEngine; using System.Collections; using System.Collections.Generic; /// <summary> /// 对Dictory的扩展 /// </summary> public static class DictionaryExtension { /// <summary> /// 尝试根据key得到value,得到了的话直接返回value,没有得到直接返回null /// this Dictionary<Tkey,Tvalue> dict 这个字典表示我们要获取值的字典 /// </summary> public static Tvalue TryGet<Tkey, Tvalue>(this Dictionary<Tkey, Tvalue> dict, Tkey key) { Tvalue value; dict.TryGetValue(key, out value); return value; } }
BasePanel using UnityEngine; using System.Collections; public class BasePanel : MonoBehaviour { /// <summary> /// 界面被显示出来 /// </summary> public virtual void OnEnter() { } /// <summary> /// 界面暂停 /// </summary> public virtual void OnPause() { } /// <summary> /// 界面继续 /// </summary> public virtual void OnResume() { } /// <summary> /// 界面不显示,退出这个界面,界面被关系 /// </summary> public virtual void OnExit() { } }