123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using UnityEngine;
- using UnityEngine.UI;
- using Rokid.MRC;
- public delegate void ButtonFunc(GameObject obj, object userData);
- public delegate void ToggleFunc(GameObject obj, bool isOn);
- public delegate void DropdownFunc(GameObject obj, int index);
- public delegate void InputFieldFunc(GameObject obj, string val);
- public delegate void InputFieldEndFunc(GameObject obj, string val);
- public delegate void ScrollRectFunc(GameObject obj, Vector2 pos);
- public delegate void SliderFunc(GameObject obj, float val);
- // UI绑定事件接口
- public static class UguiEvent
- {
- //主要有Button、Toggle、Dropdown(下拉框)、InputField(输入框)、ScrollRect、Slider
- public static void ButtonClick(GameObject go, ButtonFunc func, object userData = null)
- {
- Button button = go.GetComponent<Button>();
- if(button == null)
- button = go.AddComponent<Button>();
- button.onClick.RemoveAllListeners();
- button.onClick.AddListener(delegate ()
- {
- if(func != null)
- {
- func(go, userData);
- }
- });
- }
- public static void ButtonClick(Transform go, ButtonFunc func, object userData = null)
- {
- Button button = go.GetComponent<Button>();
- if (button == null)
- button = go.gameObject.AddComponent<Button>();
- button.onClick.RemoveAllListeners();
- button.onClick.AddListener(delegate ()
- {
- if (func != null)
- {
- func(go.gameObject, userData);
- }
- });
- }
- public static void ToggleClick(GameObject go, ToggleFunc func)
- {
- Toggle toggle = go.GetComponent<Toggle>();
- toggle.onValueChanged.RemoveAllListeners();
- toggle.onValueChanged.AddListener(delegate (bool isOn)
- {
- if(func != null)
- {
- func(go, isOn);
- }
- });
- }
- public static void DropdownClick(GameObject go, DropdownFunc func)
- {
- Dropdown dropdown = go.GetComponent<Dropdown>();
- dropdown.onValueChanged.RemoveAllListeners();
- dropdown.onValueChanged.AddListener(delegate (int index)
- {
- if(func != null)
- {
- func(go, index);
- }
- });
- }
- public static void InputFieldChange(GameObject go, InputFieldFunc func)
- {
- InputField inputField = go.GetComponent<InputField>();
- inputField.onValueChanged.RemoveAllListeners();
- inputField.onValueChanged.AddListener(delegate (string val)
- {
- if(func != null)
- {
- func(go, val);
- }
- });
- }
- public static void InputFieldChange(InputField inputField, InputFieldFunc func)
- {
- if (inputField == null) return;
- inputField.onValueChanged.RemoveAllListeners();
- inputField.onValueChanged.AddListener(delegate (string val)
- {
- if (func != null)
- {
- func(inputField.gameObject, val);
- }
- });
- }
- public static void InputFieldEndEdit(GameObject go, InputFieldEndFunc func)
- {
- InputField inputField = go.GetComponent<InputField>();
- inputField.onEndEdit.RemoveAllListeners();
- inputField.onEndEdit.AddListener(delegate(string val)
- {
- if(func != null)
- {
- func(go, val);
- }
- });
- }
- public static void EventTriggerOnClick(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if (listener == null) listener = go.AddComponent<EventTriggerListener>();
- if (func != null)
- {
- listener.onClick += func;
- }
- }
- public static void EventTriggerOnEnter(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if (listener == null) listener = go.AddComponent<EventTriggerListener>();
- if (func != null)
- {
- listener.onEnter += func;
- }
- }
- public static void EventTriggerOnDown(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if(listener == null)
- listener = go.AddComponent<EventTriggerListener>();
- if(func != null)
- {
- listener.onDown += func;
- }
- }
- public static void EventTriggerOnUp(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if(listener == null)
- listener = go.AddComponent<EventTriggerListener>();
- if(func != null)
- {
- listener.onUp += func;
- }
- }
- public static void EventTriggerOnExit(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if(listener == null)
- listener = go.AddComponent<EventTriggerListener>();
- if(func != null)
- {
- listener.onExit += func;
- }
- }
- public static void EventTriggerOnSelect(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if(listener == null)
- listener = go.AddComponent<EventTriggerListener>();
- if(func != null)
- {
- listener.onSelect += func;
- }
- }
- public static void EventTriggerOnDragBegin(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if(listener == null)
- listener = go.AddComponent<EventTriggerListener>();
- if(func != null)
- {
- listener.onDragBegin += func;
- }
- }
- public static void EventTriggerOnDragEnd(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if(listener == null)
- listener = go.AddComponent<EventTriggerListener>();
- if(func != null)
- {
- listener.onDragEnd += func;
- }
- }
- public static void EventTriggerOnDrag(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if(listener == null)
- listener = go.AddComponent<EventTriggerListener>();
- if(func != null)
- {
- listener.onDrag += func;
- }
- }
- public static void EventTriggerOnScroll(GameObject go, EventTriggerListener.VoidDelegate func)
- {
- EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
- if(listener == null)
- listener = go.AddComponent<EventTriggerListener>();
- if(func != null)
- {
- listener.onScroll += func;
- }
- }
- public static void ScrollRectValueChange(GameObject go, ScrollRectFunc func)
- {
- ScrollRect scrollRect = go.GetComponent<ScrollRect>();
- scrollRect.onValueChanged.RemoveAllListeners();
- scrollRect.onValueChanged.AddListener(delegate (Vector2 pos)
- {
- if(func != null)
- {
- func(go, pos);
- }
- });
- }
- public static void SliderValueChange(GameObject go, SliderFunc func)
- {
- Slider slider = go.GetComponent<Slider>();
- slider.onValueChanged.RemoveAllListeners();
- slider.onValueChanged.AddListener(delegate (float val)
- {
- if(func != null)
- {
- func(go, val);
- }
- });
- }
- public static void SliderValueChange(Transform trans, SliderFunc func)
- {
- Slider slider = trans.GetComponent<Slider>();
- slider.onValueChanged.RemoveAllListeners();
- slider.onValueChanged.AddListener(delegate (float val)
- {
- if (func != null)
- {
- func(trans.gameObject, val);
- }
- });
- }
- /// <summary>
- /// 强制重建layout
- /// </summary>
- public static void ForceRebuildLayout(RectTransform trans)
- {
- LayoutRebuilder.ForceRebuildLayoutImmediate(trans);
- }
- }
|