赞
踩
(1)导入之前的框架,并创建3个新的脚本:UIManager、UIControl、UIController。
UIManager
using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIManager : ManageBase<UIManager> { //控制器字典 public Dictionary<string, UIController> UIControllerDic = new Dictionary<string, UIController>(); //設置頁面激活狀態 public void SetActive(string controllerName,bool active) { transform.Find(controllerName).gameObject.SetActive(active); } //获取页面上的子控件 public UIControl GetUIControl(string controllerName,string controlName) { if (UIControllerDic.ContainsKey(controllerName)) { //寻找页面中的子控件 if (UIControllerDic[controllerName].UIControlDic.ContainsKey(controlName)) { return UIControllerDic[controllerName].UIControlDic[controlName]; } } return null; } }
UIControl
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public class UIControl : MonoBehaviour { public UIController controller; private void Awake() { //向上层检查,赋值父控制器 if (transform.parent != null) { controller = transform.GetComponentInParent<UIController>(); //将自身控件添加到父控制器中 if (controller != null) { controller.UIControlDic.Add(transform.name,this); } } } //更改文本 public void ChageText(string str) { if (GetComponent<Text>() != null) { GetComponent<Text>().text = str; } } //更改图片 public void ChangeImage(Sprite sprite) { if (GetComponent<Image>() != null) { GetComponent<Image>().sprite = sprite; } } //按钮 public void AddButtonClickEvent(UnityAction action) { Button control = GetComponent<Button>(); if (control != null) { control.onClick.AddListener(action); } } //滑动条 public void AddSliderEvent(UnityAction<float> action) { Slider control = GetComponent<Slider>(); if (control!= null){ control.onValueChanged.AddListener(action); } } //InputField public void InputFieldEvent(UnityAction<string> action) { InputField control = GetComponent<InputField>(); if (control != null) { control.onValueChanged.AddListener(action); } } }
UIController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIController : MonoBehaviour { public Dictionary<string, UIControl> UIControlDic = new Dictionary<string, UIControl>(); void Awake() { //将当前的页面控制器添加到管理器字典中 UIManager.Instance.UIControllerDic.Add(transform.name, this); //给子控件添加UIControl脚本 foreach(Transform tran in transform) { if (tran.gameObject.GetComponent<UIControl>() == null) { tran.gameObject.AddComponent<UIControl>(); } } } }
(2)再创建这些脚本挂载到对应的UI上
MainControl:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MainController : UIController { // Start is called before the first frame update void Start() { //给按钮添加事件 UIManager.Instance.GetUIControl(transform.name,"MenuButton").AddButtonClickEvent(ShowMenu); } void ShowMenu() { UIManager.Instance.SetActive("MenuController", true); gameObject.SetActive(false); } }
StateController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class StateController : UIController { // Start is called before the first frame update void Start() { UIManager.Instance.GetUIControl(transform.name, "CloseButton (1)").AddButtonClickEvent(Close); } void Close() { UIManager.Instance.SetActive("MenuController", true); gameObject.SetActive(false); } }
ItemController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ItemController : UIController { // Start is called before the first frame update void Start() { UIManager.Instance.GetUIControl(transform.name, "CloseButton (2)").AddButtonClickEvent(Close); } // Update is called once per frame void Close() { UIManager.Instance.SetActive("MenuController",true); gameObject.SetActive(false); } }
MenuController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MenuController :UIController { // Start is called before the first frame update void Start() { UIManager.Instance.GetUIControl(transform.name, "StateButton").AddButtonClickEvent(ShowStateController); UIManager.Instance.GetUIControl(transform.name, "ItemButton").AddButtonClickEvent(ShowItemController); UIManager.Instance.GetUIControl(transform.name, "CloseButton").AddButtonClickEvent(Close); UIManager.Instance.GetUIControl(transform.name, "SkillButton").AddButtonClickEvent(ShowSkillController); } // Update is called once per frame void ShowStateController() { UIManager.Instance.SetActive("StateController", true); gameObject.SetActive(false); } void ShowItemController() { UIManager.Instance.SetActive("ItemController", true); gameObject.SetActive(false); } void ShowSkillController() { UIManager.Instance.SetActive("SkillController", true); gameObject.SetActive(false); } void Close() { UIManager.Instance.SetActive("MainController", true); gameObject.SetActive(false); } }
SkillController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SkillController : UIController { // Start is called before the first frame update void Start() { UIManager.Instance.GetUIControl(transform.name, "Button (Legacy)").AddButtonClickEvent(Close); } // Update is called once per frame void Close() { UIManager.Instance.SetActive("MenuController",true); gameObject.SetActive(false); } }
(3)这样就是一个完整的UI框架了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。