EventTrigger
我們知道Button可以添加點(diǎn)擊監(jiān)聽,Slider可以滑動行施,假如我們想給Image添加點(diǎn)擊監(jiān)聽,或者更多的監(jiān)聽事件呢魂那,比如選中蛾号、鼠標(biāo)移入、拖動涯雅。我們可以用到EventTrigger鲜结。EventTrigger里繼承了很多接口,正好可以用來封裝
public class EventTriggerTest : EventTrigger {
public override void OnPointerClick (PointerEventData eventData) {
Debug.Log ("OnPointerClick");
}
public override void OnPointerEnter (PointerEventData eventData) {
Debug.Log ("OnPointerEnter");
}
public override void OnPointerExit (PointerEventData eventData) {
Debug.Log ("OnPointerExit");
}
}
這里重寫了EventTrigger里的3個方法活逆,分別是點(diǎn)擊精刷、鼠標(biāo)移入、鼠標(biāo)移出蔗候,我們把它掛載在一個Image上怒允,運(yùn)行,可以看到锈遥,根據(jù)操作纫事,確實(shí)可以執(zhí)行對應(yīng)的方法。
EventListener
要是有很多控件都需要綁定所灸,那就需要在里面都掛上這個腳本儿礼,每個腳本都在對應(yīng)的方法下重寫內(nèi)容,這樣顯得過于麻煩庆寺,我們可以就寫一個腳本蚊夫,繼承自EventTrigger,通過委托的形式懦尝,觸發(fā)EventTrigger下的事件是就執(zhí)行委托的方法知纷,具體的方法內(nèi)容在各自的腳本里去實(shí)現(xiàn)就可以了。
public class UIEventListener : EventTrigger {
public delegate void VoidDelegate (GameObject go);
public delegate void BoolDelegate (GameObject go, bool isValue);
public delegate void FloatDelegate (GameObject go, float fValue);
public delegate void IntDelegate (GameObject go, int iIndex);
public delegate void StringDelegate (GameObject go, string strValue);
public VoidDelegate onSubmit;
public VoidDelegate onClick;
public BoolDelegate onHover;
public BoolDelegate onToggleChanged;
public FloatDelegate onSliderChanged;
public FloatDelegate onScrollbarChanged;
public IntDelegate onDrapDownChanged;
public StringDelegate onInputFieldChanged;
public override void OnSubmit (BaseEventData eventData) { //提交
if (onSubmit != null)
onSubmit (gameObject);
}
public override void OnPointerEnter (PointerEventData eventData) { //鼠標(biāo)移入
if (onHover != null)
onHover (gameObject, true);
}
public override void OnPointerClick (PointerEventData eventData) { //點(diǎn)擊
if (onClick != null)
onClick (gameObject);
if (onToggleChanged != null)
onToggleChanged (gameObject, gameObject.GetComponent<Toggle> ().isOn);
}
public override void OnPointerExit (PointerEventData eventData) { //鼠標(biāo)移出
if (onHover != null)
onHover (gameObject, false);
}
public override void OnDrag (PointerEventData eventData) { //拖動
if (onSliderChanged != null)
onSliderChanged (gameObject, gameObject.GetComponent<Slider> ().value);
if (onScrollbarChanged != null)
onScrollbarChanged (gameObject, gameObject.GetComponent<Scrollbar> ().value);
}
public override void OnSelect (BaseEventData eventData) { //選中
if (onDrapDownChanged != null)
onDrapDownChanged (gameObject, gameObject.GetComponent<Dropdown> ().value);
}
public override void OnUpdateSelected (BaseEventData eventData) { //選中陵霉、每幀更新
if (onInputFieldChanged != null)
onInputFieldChanged (gameObject, gameObject.GetComponent<InputField> ().text);
}
public override void OnDeselect (BaseEventData eventData) { //不選中
if (onInputFieldChanged != null)
onInputFieldChanged (gameObject, gameObject.GetComponent<InputField> ().text);
}
public static UIEventListener Get (GameObject go) {
UIEventListener listener = go.GetComponent<UIEventListener> ();
if (listener == null) listener = go.AddComponent<UIEventListener> ();
return listener;
}
}
試一下把琅轧,我們還是實(shí)現(xiàn)上面的功能,現(xiàn)在不用手動去掛載專門的腳本踊挠,只要在Image的執(zhí)行腳本里添加就可以了乍桂,運(yùn)行后程序會自動添加上UIEventListener.cs冲杀。
public class Test1 : MonoBehaviour {
// Use this for initialization
void Start () {
UIEventListener.Get (gameObject).onClick = OnClick;
UIEventListener.Get (gameObject).onHover = OnHover;
}
// Update is called once per frame
void Update () { }
void OnClick (GameObject go) {
Debug.Log ("OnPointerClick");
}
void OnHover (GameObject go, bool _flag) {
if (_flag) {
Debug.Log ("OnPointerEnter");
} else {
Debug.Log ("OnPointerExit");
}
}
}
不足之處,多謝指正睹酌。