【Unity】一个UI框架例子

使用框架前置条件:调整脚本运行顺序, Canvas挂载UIManager, Panel挂载对应的UIController、UI控件挂载UIControl。

UIManager:UI管理器,用于处理和管理各个UIController和UIControl的业务逻辑,挂载在Canvas上;

UIController:界面层Controller,用于管理该界面下的所有控件信息;

UIControl:控件层Control,用于管理对应的组件信息。

UIManager:

public class UIManager3 : MonoBehaviour

{

private static UIManager3 instance;

public static UIManager3 Instance { get => instance; }

private Dictionary<string, UIController3> uiControllerList = new();

public virtual void Awake()

{

instance = this;

}

public virtual void OnDestroy()

{

uiControllerList.Clear();

Destroy(this);

}

public void AddToList(string uiControllerName, UIController3 uiController3)

{

if (!uiControllerList.ContainsKey(uiControllerName))

uiControllerList.Add(uiControllerName, uiController3);

}

public void LogoutUIController(string name)

{

if (uiControllerList.ContainsKey(name))

uiControllerList.Remove(name);

}

public void SetControllerActive(string uiControllerName, bool active)

{

foreach(Transform trans in transform)

{

if (trans.name == uiControllerName) trans.gameObject.SetActive(active);

}

}

public UIController3 GetUIController(string name)

{

if (uiControllerList.TryGetValue(name, out UIController3 uiController3))

return uiController3;

return null;

}

public UIControl3 GetUIControl(string controllerName, string controlName)

{

if (uiControllerList.TryGetValue(controllerName, out UIController3 uiController3))

return uiController3.GetUIControl(controlName);

return null;

}

}

UIController:

public abstract class UIController3 : MonoBehaviour

{

private Dictionary<string, UIControl3> uiControlList = new();

public virtual void Start()

{

UIManager3.Instance.AddToList(this.name, this);

foreach(Transform trans in transform)

{

if (trans.gameObject.GetComponent<UIControl>() != null) Destroy(trans.gameObject.GetComponent<UIControl>());

trans.gameObject.AddComponent<UIControl3>();

}

}

public virtual void OnDestroy()

{

UIManager3.Instance.LogoutUIController(this.name);

Destroy(this);

}

public UIControl3 GetUIControl(string name)

{

if (uiControlList.TryGetValue(name, out UIControl3 uiControl3))

return uiControl3;

return null;

}

public void LogoutUIControl(string name)

{

if (uiControlList.ContainsKey(name))

uiControlList.Remove(name);

}

}

UIControl:

public class UIControl3 : MonoBehaviour

{

private UIController3 parentController;

public virtual void Start()

{

if (transform.parent.GetComponentInParent<UIController3>() != null) parentController = transform.parent.GetComponentInParent<UIController3>();

}

public virtual void OnDestroy()

{

parentController.LogoutUIControl(this.name);

Destroy(this);

}

public void SetText(string txt)

{

if (GetComponent<Text>() != null)

GetComponent<Text>().text = txt;

}

public void SetButtonOnClick(UnityAction unityAction)

{

if (GetComponent<Button>() != null)

GetComponent<Button>().onClick.AddListener(unityAction);

}

public void SetSliderEvent(UnityAction<float> unityAction)

{

if (GetComponent<Slider>() != null)

GetComponent<Slider>().onValueChanged.AddListener(unityAction);//unityAction(float), float为Slider改变后的value值

}

public void SetImage(Sprite sprite)

{

if (GetComponent<Image>() != null)

GetComponent<Image>().sprite = sprite;

}

public void SetInpueFieldEvent(UnityAction<string> unityAction)

{

if (GetComponent<InputField>() != null)

GetComponent<InputField>().onValueChanged.AddListener(unityAction);//unityAction(string), string为InputField的整个文本

}

}

LoginPanel(GameObject名称为“Login_Panel”):

public class LoginPanel3 : UIController3

{

public Button UI_LoginBtn;

public override void Start()

{

base.Start();

UI_LoginBtn.GetComponent<Button>().onClick.AddListener(UILoginBtnOnClick);

}

private void UILoginBtnOnClick()

{

UIManager3.Instance.SetControllerActive(this.name, false);

UIManager3.Instance.SetControllerActive("Main_Panel", true);

}

}

MainPanel(GameObject名称为“Main_Panel”):

public class MainPanel3 : UIController3

{

public Button UI_ToMenuBtn;

public Button UI_LogoutBtn;

public override void Start()

{

base.Start();

UI_ToMenuBtn.GetComponent<UIControl3>().SetButtonOnClick(UITOMenuBtnOnClick);

UI_LogoutBtn.GetComponent<UIControl3>().SetButtonOnClick(UILogoutBtnOnClick);

}

private void UITOMenuBtnOnClick()

{

UIManager3.Instance.SetControllerActive(this.name, false);

UIManager3.Instance.SetControllerActive("Menu_Panel", true);

}

private void UILogoutBtnOnClick()

{

UIManager3.Instance.SetControllerActive(this.name, false);

UIManager3.Instance.SetControllerActive("Login_Panel", true);

}

}

MenuPanel(GameObject名称为“Menu_Panel”):

public class MenuPanel3 : UIController3

{

public Button UI_TOMainBtn;

public override void Start()

{

base.Start();

UI_TOMainBtn.GetComponent<UIControl3>().SetButtonOnClick(UITOMainBtnOnClick);

}

private void UITOMainBtnOnClick()

{

UIManager3.Instance.SetControllerActive(this.name, false);

UIManager3.Instance.SetControllerActive("Main_Panel", true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值