UI按键自动绑定
This commit is contained in:
parent
4f7016d0d6
commit
278fce07a8
|
@ -1,4 +1,6 @@
|
|||
using UnityEngine;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class MainUIPanel : UIPanelBase
|
||||
|
@ -14,29 +16,19 @@ public class MainUIPanel : UIPanelBase
|
|||
_coDriverBtn = _content.Find("CoDriver").GetComponent<Button>();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_driverBtn.onClick.AddListener(OnDriverBtnClicked);
|
||||
_coDriverBtn.onClick.AddListener(OnCoDriverBtnClicked);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_driverBtn.onClick.RemoveListener(OnDriverBtnClicked);
|
||||
_coDriverBtn.onClick.RemoveListener(OnCoDriverBtnClicked);
|
||||
}
|
||||
|
||||
public override void Init(object[] pageData)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[UIButtonOnClick("Driver")]
|
||||
public void OnDriverBtnClicked()
|
||||
{
|
||||
GlobalData.Instance.Position = PositionType.Driver;
|
||||
UIManager.Instance.ShowPanel<ActionListUIPanel>(true);
|
||||
}
|
||||
|
||||
[UIButtonOnClick("CoDriver")]
|
||||
public void OnCoDriverBtnClicked()
|
||||
{
|
||||
GlobalData.Instance.Position = PositionType.CoDriver;
|
||||
|
@ -44,6 +36,8 @@ public class MainUIPanel : UIPanelBase
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public enum PositionType
|
||||
{
|
||||
None,
|
||||
|
|
|
@ -1,4 +1,66 @@
|
|||
public abstract class UIPanelBase : UIBase
|
||||
{
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public abstract class UIPanelBase : UIBase
|
||||
{
|
||||
private void OnEnable()
|
||||
{
|
||||
Bind();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
UnBind();
|
||||
}
|
||||
|
||||
protected void Bind()
|
||||
{
|
||||
Type type = GetType();
|
||||
var methods = type.GetMethods();
|
||||
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var attributes = method.GetCustomAttributes(typeof(UIButtonOnClickAttribute), true);
|
||||
if (attributes.Length == 0)
|
||||
continue;
|
||||
|
||||
var attribute = attributes[0] as UIButtonOnClickAttribute;
|
||||
|
||||
var btns = transform.GetComponentsInChildren<Button>().ToList();
|
||||
var button = btns.Find(btns => btns.name == attribute.ButtonName);
|
||||
if (button == null)
|
||||
continue;
|
||||
button.onClick.AddListener(() => { method.Invoke(this, null); });
|
||||
}
|
||||
}
|
||||
|
||||
protected void UnBind()
|
||||
{
|
||||
Type type = GetType();
|
||||
var methods = type.GetMethods();
|
||||
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var attributes = method.GetCustomAttributes(typeof(UIButtonOnClickAttribute), true);
|
||||
if (attributes.Length == 0)
|
||||
continue;
|
||||
|
||||
var attribute = attributes[0] as UIButtonOnClickAttribute;
|
||||
|
||||
var btns = transform.GetComponentsInChildren<Button>().ToList();
|
||||
var button = btns.Find(btns => btns.name == attribute.ButtonName);
|
||||
if (button == null)
|
||||
continue;
|
||||
button.onClick.RemoveListener(() => { method.Invoke(this, null); });
|
||||
}
|
||||
}
|
||||
}
|
||||
public class UIButtonOnClickAttribute : Attribute
|
||||
{
|
||||
public string ButtonName { get; private set; }
|
||||
public UIButtonOnClickAttribute(string button)
|
||||
{
|
||||
ButtonName = button;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue