UI框架 可以直接当一个小插件的使用

1.文件夹规范:

2.做UI的时候:2D模式,正交摄像机 

3.

选择这个 数值改成0.5适应宽高

4.。一个空的image来当爹,其他的物体是他儿子,这样儿子依附于他在右下角 就不会改变形状了

5.方便ui管理,点击什么,就出现什么,用枚举
枚举和结构体都是值类型:

通过文本加载,通过单利进行,
通过Json加载出来,根据名字加载出来
json不能解析枚举,字符串转换枚举:

不解析枚举的属性

读取jison要序列化

 存入json与解析json

用list存入:

list<i****nfo>

6.json文档的书写(创建一个text文本来书写),然后在校验:
{}对象
:引出对象的集合

[]对象集合
文档内容:

在json校验

 代码部分(不是完整版本):

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using System;
 5 [Serializable]
 6 public class UIPanelInfo :ISerializationCallbackReceiver {
 7     [NonSerialized]//不序列化(json解析不了枚举)
 8    public UIPanelType type;
 9    public string path;
10    public string UIPanelTypeString;
11 
12     public void OnAfterDeserialize()//在序列化之后进行转换
13     {
14       type=(UIPanelType)Enum.Parse(typeof(UIPanelType),UIPanelTypeString);
15     }
16 
17     public void OnBeforeSerialize()
18     {
19         throw new NotImplementedException();
20     }
21 }
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 [System.Serializable]
5 public class UIListFromJson  {
6 
7     public List<UIPanelInfo> ListInfo;
8 }
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public enum UIPanelType
 6 {
 7     Task,
 8     Bag,
 9     System,
10     Shop,
11     Skill,
12     ItemMessage,
13     UIMain    
14 }

 做什么管理类 都做成单列

 以文本文件的形式进行加载。

                                                                                                                                                           

通过返回父类 得到上边图片 到东西,采用虚方法:用虚方法不用全部实现,可以实列化,抽象方法的作用

名字要高度一致

   暂停的方式:

最后(完整)的代码:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class MainPanel : BasePanel {
 6     CanvasGroup ag;
 7     // Use this for initialization
 8     void Start () {
 9         ag = GetComponent<CanvasGroup>();
10     }
11    
12     public override void OnPause()
13     {
14         ag.blocksRaycasts = false;
15     }
16     public override void OnContinue()
17     {
18         ag.blocksRaycasts = true;
19     }
20     public void OnClick(string type)
21     {
22         UIPanelType uI = (UIPanelType)System.Enum.Parse(typeof(UIPanelType),type);
23         UImanger.Instance.PushStack(uI);
24 
25     }
26    
27 }

第二个脚本:

  1 齐培良老师 2018/10/15 15:45:43
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using UnityEngine;
  5 
  6 public class UImanger
  7 {
  8     private static UImanger _instance;
  9     public static UImanger Instance
 10     {
 11 
 12         get
 13         {
 14             if (_instance == null)
 15             {
 16                 _instance = new UImanger();
 17             }
 18             return _instance;
 19         }
 20 
 21     }
 22     private UImanger()
 23     {
 24 
 25         JsonFromJx();
 26 
 27     }
 28 
 29     public Dictionary<UIPanelType, string> UIPanelDic;//存取json信息
 30     //解析json信息保存到字典
 31     public void JsonFromJx()
 32     {
 33         //加载文本
 34         TextAsset text = Resources.Load<TextAsset>("UIPanelType");//加载json文件
 35         Debug.Log(text.text);
 36         //解析json文本返回UIListFromJson对象
 37         UIListFromJson listPanel = (UIListFromJson)JsonUtility.FromJson(text.text,typeof(UIListFromJson));
 38         Debug.Log(listPanel.ListInfo[0].Path);
 39         UIPanelDic = new Dictionary<UIPanelType, string>();
 40         foreach (var item in listPanel.ListInfo)
 41         {
 42            // Debug.Log(listPanel);
 43             UIPanelDic.Add(item.type, item.Path);
 44             Debug.Log(item.Path);
 45         }
 46     }
 47     public Dictionary<UIPanelType, BasePanel> Dic;
 48     public BasePanel GetPanel(UIPanelType panelType)
 49     {
 50 
 51         if (Dic == null)
 52         {
 53             Dic = new Dictionary<UIPanelType, BasePanel>();
 54         }
 55         BasePanel bp;
 56         Dic.TryGetValue(panelType, out bp);//尝试去获取Key(panelType)对应的Panel
 57 
 58         if (bp==null)
 59         {
 60             string path;
 61             UIPanelDic.TryGetValue(panelType, out path);         
 62             GameObject obj = GameObject.Instantiate(Resources.Load(path)) as GameObject;
 63             obj.transform.SetParent(GameObject.Find("Canvas").transform,false);
 64             Dic = new Dictionary<UIPanelType, BasePanel>();//加载出来的UI都在存在Dic里面    
 65             Dic.Add(panelType,obj.GetComponent<BasePanel>());
 66             return obj.GetComponent<BasePanel>();
 67         }
 68         else
 69         {
 70         return bp;
 71 
 72         }
 73        
 74         
 75     }
 76 
 77     public Stack<BasePanel> stack;
 78     public void PushStack(UIPanelType uIPanel)
 79     {
 80         if (stack==null)
 81         {
 82             stack = new Stack<BasePanel>();
 83         }
 84         if (stack.Count>0)
 85         {
 86            BasePanel panel= stack.Peek();//在入栈之前把栈里面的第一个暂停(停止使用)
 87             panel.OnPause();
 88         }
 89 
 90         BasePanel bp=  GetPanel(uIPanel);//打开的PanelUI    
 91         bp.OnEnter();
 92         stack.Push(bp);
 93     }
 94     public void PopStack()//出栈最顶层,然后下面一层继续可以操作
 95     {
 96 
 97         if (stack==null)
 98         {
 99             stack = new Stack<BasePanel>();
100         }
101         if (stack.Count<=0)
102         {
103             return;
104         }
105         else
106         {
107             BasePanel bp= stack.Peek();
108             bp.OnExit();
109             stack.Pop();
110             if (stack.Count<=0)
111             {
112                 return;
113             }
114             BasePanel panel= stack.Peek();
115             panel.OnContinue();
116         }
117 
118 
119 
120     }
121 
122 }

第三个

 

 1 齐培良老师 2018/10/15 15:45:18
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using UnityEngine;
 5 
 6 public class TaskPanel : BasePanel {
 7     CanvasGroup ag;
 8     // Use this for initialization
 9     void Start () {
10         if (ag==null)
11         {
12 
13         ag = GetComponent<CanvasGroup>();
14         }
15     }
16     public override void OnEnter()
17     {
18         if (ag==null)
19         {
20             ag = GetComponent<CanvasGroup>();
21         }
22         ag.alpha = 1;
23         ag.blocksRaycasts = true;
24     }
25     public override void OnExit()
26     {
27         ag.alpha = 0;
28         ag.blocksRaycasts = false;
29     }
30     public void OnClose() {
31 
32         UImanger.Instance.PopStack();
33 
34     }
35 }

     第四个:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class BasePanel : MonoBehaviour {
 6 
 7     public virtual void OnEnter() { }
 8     public virtual void OnPause() { }
 9     public virtual void OnContinue() { }
10     public virtual void OnExit() { }
11 }

每一个预制体都有一个对应 的脚本 没有操作 多久害死空脚本

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

猜你喜欢

转载自www.cnblogs.com/satanj/p/9791471.html
今日推荐