134 lines
4.2 KiB
C#
134 lines
4.2 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|