当前位置:   article > 正文

Unity实现一个简单易用的UI框架_unity openpanel

unity openpanel

1、面板管理类

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PanelMgr:MonoBehaviour {
  5. //单例
  6. public static PanelMgr instance;
  7. //画板
  8. private GameObject canvas;
  9. //面板队列,记录已经打开的界面
  10. public List<PanelBase> dict;
  11. //开始
  12. public void Awake () {
  13. instance = this;
  14. InitLayer ();
  15. dict = new List<PanelBase> ();
  16. }
  17. //初始化层
  18. private void InitLayer ()
  19. {
  20. //画布
  21. canvas = GameObject.Find ("Canvas");
  22. if (canvas == null)
  23. Debug.LogError ("panelMgr.InitLayer fail, canvas is null");
  24. }
  25. //打开面板
  26. public void OpenPanel<T> (params object[] args) where T : PanelBase {
  27. /*为canvas添加脚本*/
  28. PanelBase panel = canvas.AddComponent<T> ();
  29. panel.Init (args);
  30. if (panel.skinPath != null)
  31. dict.Add (panel);
  32. Debug.Log ("打开面板"+panel.skinPath);
  33. /*加载UI预制体*/
  34. GameObject skin = Resources.Load<GameObject> (panel.skinPath);
  35. if (skin == null)
  36. Debug.LogError ("panelMgr.OpenPanel fail, skin is null,skinPath = " + panel.skinPath);
  37. /*实例化UI*/
  38. panel.skin = Instantiate (skin);
  39. if (panel.layer == PanelLayer.Panel)
  40. panel.skin.transform.SetParent (canvas.transform.GetChild (0), false);
  41. else
  42. panel.skin.transform.SetParent (canvas.transform.GetChild (1), false);
  43. //panel的生命周期
  44. panel.OnShowing ();
  45. //预留的面板动画
  46. panel.OnShowed ();
  47. }
  48. //关闭面板
  49. public void ClosePanel (PanelBase panel)
  50. {
  51. panel.OnClosing ();
  52. dict.Remove (panel);
  53. Debug.Log ("关闭面板"+panel.skinPath);
  54. Destroy (panel.skin);
  55. Destroy (panel);
  56. panel.OnClosed ();
  57. }
  58. //关闭所有面板
  59. public void CloseAllPanel ()
  60. {
  61. for(int i=0;i<dict.Count;)
  62. {
  63. ClosePanel (dict[0]);
  64. Debug.Log ("关闭页面");
  65. }
  66. }
  67. }
  68. //分层类型
  69. public enum PanelLayer{
  70. //面板
  71. Panel,
  72. //提示
  73. Tips
  74. }

2、面板基类

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PanelBase : MonoBehaviour
  5. {
  6. //皮肤路径
  7. public string skinPath;
  8. //面板参数
  9. public object[] args;
  10. //皮肤
  11. public GameObject skin;
  12. //层级
  13. public PanelLayer layer;
  14. //初始化
  15. public virtual void Init(params object[] args)
  16. {
  17. this.args = args;
  18. }
  19. //开始面板前
  20. public virtual void OnShowing(){}
  21. //显示面板后
  22. public virtual void OnShowed(){}
  23. //帧更新
  24. public virtual void Update(){}
  25. //关闭前
  26. public virtual void OnClosing(){}
  27. //关闭后
  28. public virtual void OnClosed(){}
  29. }

3、登录面板

 

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /*登录面板*/
  6. using BoxWorldServer;
  7. public class LoginPanel : PanelBase
  8. {
  9. /*UI组件*/
  10. private InputField idInput;
  11. private InputField pwInput;
  12. private Button loginBtn;
  13. private Button regBtn;
  14. private Button closeBtn;
  15. //初始化
  16. public override void Init (params object[] args)
  17. {
  18. base.Init (args);
  19. skinPath = "UI/LoginPanel";
  20. layer = PanelLayer.Panel;
  21. }
  22. public override void OnShowing ()
  23. {
  24. /*初始化组件*/
  25. idInput = skin.transform.Find ("IDInput").GetComponent<InputField> ();
  26. pwInput = skin.transform.Find ("PWInput").GetComponent<InputField> ();
  27. loginBtn = skin.transform.Find ("LoginBtn").GetComponent<Button> ();
  28. regBtn = skin.transform.Find ("RegBtn").GetComponent<Button> ();
  29. closeBtn = skin.transform.Find ("CloseBtn").GetComponent<Button> ();
  30. /*添加UI事件*/
  31. loginBtn.onClick.AddListener (OnLoginClick);
  32. regBtn.onClick.AddListener (OnRegClick);
  33. closeBtn.onClick.AddListener (OnCloseClick);
  34. }
  35. /*关闭按钮事件*/
  36. public void OnCloseClick ()
  37. {
  38. Application.Quit ();
  39. }
  40. /*注册按钮事件*/
  41. public void OnRegClick ()
  42. {
  43. PanelMgr.instance.OpenPanel<RegPanel> ();
  44. }
  45. /*登录按钮事件*/
  46. public void OnLoginClick ()
  47. {
  48. if (idInput.text == "" || pwInput.text == "")
  49. {
  50. /*打开提示框*/
  51. PanelMgr.instance.OpenPanel<TipPanel> ("用户名密码不能为空!");
  52. return;
  53. }
  54. //如果服务器没有连接,连接服务器
  55. if (NetMgr.instance.status != NetMgr.Status.Connected)
  56. {
  57. NetMgr.instance.proto = new ProtocolStr ();
  58. /*连接*/
  59. if (!NetMgr.instance.Connect ())
  60. PanelMgr.instance.OpenPanel<TipPanel> ("连接服务器失败!");
  61. }
  62. //发送登录协议
  63. ProtocolStr protocol = new ProtocolStr ();
  64. protocol.AddString ("login");
  65. protocol.AddString (idInput.text);
  66. protocol.AddString (pwInput.text);
  67. /*一次性监听*/
  68. NetMgr.instance.Send (protocol, OnLoginBack);
  69. }
  70. /*登录协议回执*/
  71. public void OnLoginBack (ProtocolBase protocol)
  72. {
  73. /*构建协议*/
  74. ProtocolStr proto = (ProtocolStr)protocol;
  75. /*校验登录结果*/
  76. int result = proto.GetInt (1);
  77. if (result == 0)
  78. {
  79. GameMgr.instance.id = idInput.text; //保存账号ID
  80. PanelMgr.instance.OpenPanel<TipPanel> ("登录成功!");
  81. PanelMgr.instance.OpenPanel<LobbyPanel> (); /*进入游戏大厅*/
  82. PanelMgr.instance.ClosePanel (this);
  83. }
  84. else
  85. {
  86. NetMgr.instance.Close (); //断开连接
  87. PanelMgr.instance.OpenPanel<TipPanel> ("登录失败,请检查用户名密码!");
  88. }
  89. }
  90. }

4、提示面板

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. /*提示面板*/
  5. public class TipPanel : PanelBase
  6. {
  7. /*UI组件*/
  8. private Text text;
  9. private Button btn;
  10. string str = "";
  11. public override void Init (params object[] args)
  12. {
  13. base.Init (args);
  14. skinPath = "UI/TipPanel";
  15. layer = PanelLayer.Tips;
  16. //参数 args[0]代表提示的内容
  17. if (args.Length == 1) {
  18. str = (string)args [0];
  19. }
  20. }
  21. //显示之前
  22. public override void OnShowing ()
  23. {
  24. base.OnShowing ();
  25. Transform skinTrans = skin.transform;
  26. //文字
  27. text = skinTrans.Find ("Text").GetComponent<Text> ();
  28. text.text = str;
  29. //关闭按钮
  30. btn = skinTrans.Find ("Btn").GetComponent<Button> ();
  31. btn.onClick.AddListener (OnBtnClick);
  32. }
  33. //按下“知道了”关闭提示
  34. public void OnBtnClick ()
  35. {
  36. PanelMgr.instance.ClosePanel (this);
  37. }
  38. }

普通面板只需要模仿登录面板一样就可以了,如果需要多种类型面板也只需要在结构体加上相应的类型,然后在层级面板上的Canvas子物体下面添加相应空物体就可以了。比如提示面板是应该显示在最上面,那么提示面板就应该放在Canvas的最下面的那个子问题下面,这样提示面板就会显示在最上面了。这套代码界面分离的框架还是特别好用的呀,可以自己更加需求进行修改。

 

 

 

 

 

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号