赞
踩
1、面板管理类
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PanelMgr:MonoBehaviour {
- //单例
- public static PanelMgr instance;
- //画板
- private GameObject canvas;
- //面板队列,记录已经打开的界面
- public List<PanelBase> dict;
-
- //开始
- public void Awake () {
- instance = this;
- InitLayer ();
- dict = new List<PanelBase> ();
- }
-
- //初始化层
- private void InitLayer ()
- {
- //画布
- canvas = GameObject.Find ("Canvas");
- if (canvas == null)
- Debug.LogError ("panelMgr.InitLayer fail, canvas is null");
- }
- //打开面板
- public void OpenPanel<T> (params object[] args) where T : PanelBase {
- /*为canvas添加脚本*/
- PanelBase panel = canvas.AddComponent<T> ();
- panel.Init (args);
- if (panel.skinPath != null)
- dict.Add (panel);
- Debug.Log ("打开面板"+panel.skinPath);
- /*加载UI预制体*/
- GameObject skin = Resources.Load<GameObject> (panel.skinPath);
- if (skin == null)
- Debug.LogError ("panelMgr.OpenPanel fail, skin is null,skinPath = " + panel.skinPath);
- /*实例化UI*/
- panel.skin = Instantiate (skin);
- if (panel.layer == PanelLayer.Panel)
- panel.skin.transform.SetParent (canvas.transform.GetChild (0), false);
- else
- panel.skin.transform.SetParent (canvas.transform.GetChild (1), false);
- //panel的生命周期
- panel.OnShowing ();
- //预留的面板动画
- panel.OnShowed ();
- }
- //关闭面板
- public void ClosePanel (PanelBase panel)
- {
- panel.OnClosing ();
- dict.Remove (panel);
- Debug.Log ("关闭面板"+panel.skinPath);
- Destroy (panel.skin);
- Destroy (panel);
- panel.OnClosed ();
- }
- //关闭所有面板
- public void CloseAllPanel ()
- {
- for(int i=0;i<dict.Count;)
- {
- ClosePanel (dict[0]);
- Debug.Log ("关闭页面");
- }
- }
-
- }
-
- //分层类型
- public enum PanelLayer{
- //面板
- Panel,
- //提示
- Tips
- }
2、面板基类
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PanelBase : MonoBehaviour
- {
- //皮肤路径
- public string skinPath;
- //面板参数
- public object[] args;
- //皮肤
- public GameObject skin;
- //层级
- public PanelLayer layer;
-
- //初始化
- public virtual void Init(params object[] args)
- {
- this.args = args;
- }
- //开始面板前
- public virtual void OnShowing(){}
- //显示面板后
- public virtual void OnShowed(){}
- //帧更新
- public virtual void Update(){}
- //关闭前
- public virtual void OnClosing(){}
- //关闭后
- public virtual void OnClosed(){}
- }
3、登录面板
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- /*登录面板*/
- using BoxWorldServer;
-
-
- public class LoginPanel : PanelBase
- {
- /*UI组件*/
- private InputField idInput;
- private InputField pwInput;
-
- private Button loginBtn;
- private Button regBtn;
- private Button closeBtn;
- //初始化
- public override void Init (params object[] args)
- {
- base.Init (args);
- skinPath = "UI/LoginPanel";
- layer = PanelLayer.Panel;
- }
-
- public override void OnShowing ()
- {
- /*初始化组件*/
- idInput = skin.transform.Find ("IDInput").GetComponent<InputField> ();
- pwInput = skin.transform.Find ("PWInput").GetComponent<InputField> ();
- loginBtn = skin.transform.Find ("LoginBtn").GetComponent<Button> ();
- regBtn = skin.transform.Find ("RegBtn").GetComponent<Button> ();
- closeBtn = skin.transform.Find ("CloseBtn").GetComponent<Button> ();
- /*添加UI事件*/
- loginBtn.onClick.AddListener (OnLoginClick);
- regBtn.onClick.AddListener (OnRegClick);
- closeBtn.onClick.AddListener (OnCloseClick);
- }
- /*关闭按钮事件*/
- public void OnCloseClick ()
- {
- Application.Quit ();
- }
- /*注册按钮事件*/
- public void OnRegClick ()
- {
- PanelMgr.instance.OpenPanel<RegPanel> ();
- }
- /*登录按钮事件*/
- public void OnLoginClick ()
- {
- if (idInput.text == "" || pwInput.text == "")
- {
- /*打开提示框*/
- PanelMgr.instance.OpenPanel<TipPanel> ("用户名密码不能为空!");
- return;
- }
- //如果服务器没有连接,连接服务器
- if (NetMgr.instance.status != NetMgr.Status.Connected)
- {
- NetMgr.instance.proto = new ProtocolStr ();
- /*连接*/
- if (!NetMgr.instance.Connect ())
- PanelMgr.instance.OpenPanel<TipPanel> ("连接服务器失败!");
- }
-
- //发送登录协议
- ProtocolStr protocol = new ProtocolStr ();
- protocol.AddString ("login");
- protocol.AddString (idInput.text);
- protocol.AddString (pwInput.text);
- /*一次性监听*/
- NetMgr.instance.Send (protocol, OnLoginBack);
- }
-
- /*登录协议回执*/
- public void OnLoginBack (ProtocolBase protocol)
- {
- /*构建协议*/
- ProtocolStr proto = (ProtocolStr)protocol;
- /*校验登录结果*/
- int result = proto.GetInt (1);
- if (result == 0)
- {
- GameMgr.instance.id = idInput.text; //保存账号ID
- PanelMgr.instance.OpenPanel<TipPanel> ("登录成功!");
- PanelMgr.instance.OpenPanel<LobbyPanel> (); /*进入游戏大厅*/
- PanelMgr.instance.ClosePanel (this);
- }
- else
- {
- NetMgr.instance.Close (); //断开连接
- PanelMgr.instance.OpenPanel<TipPanel> ("登录失败,请检查用户名密码!");
- }
- }
-
- }
4、提示面板
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
-
- /*提示面板*/
- public class TipPanel : PanelBase
- {
- /*UI组件*/
- private Text text;
- private Button btn;
- string str = "";
-
- public override void Init (params object[] args)
- {
- base.Init (args);
- skinPath = "UI/TipPanel";
- layer = PanelLayer.Tips;
- //参数 args[0]代表提示的内容
- if (args.Length == 1) {
- str = (string)args [0];
- }
- }
- //显示之前
- public override void OnShowing ()
- {
- base.OnShowing ();
- Transform skinTrans = skin.transform;
- //文字
- text = skinTrans.Find ("Text").GetComponent<Text> ();
- text.text = str;
- //关闭按钮
- btn = skinTrans.Find ("Btn").GetComponent<Button> ();
- btn.onClick.AddListener (OnBtnClick);
- }
- //按下“知道了”关闭提示
- public void OnBtnClick ()
- {
- PanelMgr.instance.ClosePanel (this);
- }
- }
普通面板只需要模仿登录面板一样就可以了,如果需要多种类型面板也只需要在结构体加上相应的类型,然后在层级面板上的Canvas子物体下面添加相应空物体就可以了。比如提示面板是应该显示在最上面,那么提示面板就应该放在Canvas的最下面的那个子问题下面,这样提示面板就会显示在最上面了。这套代码界面分离的框架还是特别好用的呀,可以自己更加需求进行修改。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。