134 lines
4.2 KiB
C#
Raw Normal View History

2025-01-02 12:15:45 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace CG.Framework
{
public enum UIEventType
{
Normal,
OnButtonClick,
OnToggleValueChanged,
OnInputFieldValueChanged,
OnInputFieldEndEdit,
OnInputFieldSubmitEdit,
OnSliderValueChanged,
OnDropdownValueChanged
}
public class UIBehaviours : MonoBehaviour
{
private string panelName;
/// <summary>
/// 自定义Awake
/// 如果_N对应物体刚是出于隐藏状态那麽在加入脚本后脚本上的Awke会执行
/// 此处需要优化这里的自定义Awake方法和单例当中的Awake方法原则不统一重新规划
/// </summary>
public void AwakeCustom()
{
panelName = GetComponentInParent<UIBase>().name;
UI_Manage.Instance.RegisterUIWedge(panelName, this.name, gameObject);
}
public void SetText(string msg)
{
Text t = GetComponent<Text>();
if (t != null)
{
t.text = msg;
}
}
#region
public void AddEventListener(UIEventType type, UnityAction callback)
{
switch (type)
{
case UIEventType.OnButtonClick:
Button button = GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener(callback);
}
break;
}
}
public void AddEventListener(UIEventType type, UnityAction<bool> callback)
{
switch (type)
{
case UIEventType.OnToggleValueChanged:
Toggle toggle = GetComponent<Toggle>();
if (toggle != null)
{
toggle.onValueChanged.AddListener(callback);
}
break;
}
}
public void AddEventListener(UIEventType type, UnityAction<string> callback)
{
InputField inputField;
switch (type)
{
case UIEventType.OnInputFieldValueChanged:
inputField = GetComponent<InputField>();
if (inputField != null)
{
inputField.onValueChanged.AddListener(callback);
}
break;
case UIEventType.OnInputFieldEndEdit:
inputField = GetComponent<InputField>();
if (inputField != null)
{
inputField.onEndEdit.AddListener(callback);
}
break;
case UIEventType.OnInputFieldSubmitEdit:
inputField = GetComponent<InputField>();
if (inputField != null)
{
inputField.onSubmit.AddListener(callback);
}
break;
}
}
public void AddEventListener(UIEventType type, UnityAction<int> callback)
{
switch (type)
{
case UIEventType.OnDropdownValueChanged:
Dropdown t = GetComponent<Dropdown>();
if (t != null)
{
t.onValueChanged.AddListener(callback);
}
break;
}
}
public void AddEventListener(UIEventType type, UnityAction<float> callback)
{
switch (type)
{
case UIEventType.OnSliderValueChanged:
Slider t = GetComponent<Slider>();
if (t != null)
{
t.onValueChanged.AddListener(callback);
}
break;
}
}
#endregion
public void OnDestroy()
{
if (!string.IsNullOrEmpty(panelName))
UI_Manage.Instance.UnRegisterUIWedge(panelName, name, gameObject);
}
}
}