unity笔记--按钮绑定事件

原文地址:https://www.cnblogs.com/isayes/p/6370168.html

1.可视化创建及事件绑定

  1. 创建一个脚本 TestClick.cs, 定义了一个Click 的 public 方法
  2. 选中 Hierarchy 中的 Button, Add Component 脚本 TestClick.cs(TestClick : MonoBehaviour)。
  3. 在 Button(Script) 关联 TestClick 脚本里的 Click 方法。
    unity笔记--按钮绑定事件_第1张图片

2.通过直接绑定脚本来绑定事件

  1. 创建一个 ClickHandler.cs 脚本, 定义了一个私有方法 OnClick(), 并在 Start() 方法里为Button 添加点击事件的监听(Button.onClick.AddListener),作为参数传入 OnClick 方法。
  2. 将 ClickHandler 绑定在 Button 对象上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ClickHandler : MonoBehaviour {
     

	void Start () {
     
		Button btn = this.GetComponent<Button> ();
		btn.onClick.AddListener (OnClick);
	}

	private void OnClick(){
     
		Debug.Log ("Button Clicked. ClickHandler.");
	}

}

unity笔记--按钮绑定事件_第2张图片

3.通过 EventTrigger 实现按钮点击事件

  1. 为按钮添加组件:Event Trigger。
  2. 创建一个 EventTriggerHandler.cs 脚本, 利用 UnityEngine.EventSystems.EventTrigger 添加监听事件.
  3. 绑定 EventTriggerHandler.cs 脚本到 Button 上.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

// 需要 EventTrigger 脚本的支援
[RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))]
public class EventTriggerHandler : MonoBehaviour {
     

	// Use this for initialization
	void Start () {
     
		Button btn = this.GetComponent<Button> ();
		EventTrigger trigger = btn.gameObject.GetComponent<EventTrigger> ();
		EventTrigger.Entry entry = new EventTrigger.Entry ();
		// 鼠标点击事件
		entry.eventID = EventTriggerType.PointerClick;
		// 鼠标进入事件 entry.eventID = EventTriggerType.PointerEnter;
		// 鼠标滑出事件 entry.eventID = EventTriggerType.PointerExit;
		entry.callback = new EventTrigger.TriggerEvent ();
		entry.callback.AddListener (OnClick);
		// entry.callback.AddListener (OnMouseEnter);
		trigger.triggers.Add (entry);
	}

	private void OnClick(BaseEventData pointData){
     
		Debug.Log ("Button Clicked. EventTrigger..");
	}

	private void OnMouseEnter(BaseEventData pointData){
     
		Debug.Log ("Button Enter. EventTrigger..");
	}
}

unity笔记--按钮绑定事件_第3张图片

4. 通过 MonoBehaviour 实现事件类接口来实现事件的监听

  1. 创建一个 EventHandler.cs 脚本。
  2. 将脚本绑定在 Button 对象上.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class EventHandler : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IDragHandler {
     

	public void OnPointerClick(PointerEventData eventData){
     
		if(eventData.pointerId == -1){
     
			Debug.Log ("Left Mouse Clicked.");
		} else if(eventData.pointerId == -2){
     
			Debug.Log ("Right Mouse Clicked.");
		}
	}

	public void OnPointerEnter(PointerEventData eventData){
     
		Debug.Log ("Pointer Enter..");
	}

	public void OnPointerExit(PointerEventData eventData){
     
		Debug.Log ("Pointer Exit..");
	}

	public void OnPointerDown(PointerEventData eventData){
     
		Debug.Log ("Pointer Down..");
	}

	public void OnDrag(PointerEventData eventData){
     
		Debug.Log ("Dragged..");
	}

}

unity笔记--按钮绑定事件_第4张图片

5.采用观察者模式监听

  UGUI 如何判断 UI 元素被点击时是鼠标的哪个按键, 上面的代码中我们可以根据 eventData.pointerId 来监听是鼠标左键还是右键. 但是每个 UI 元素都创建一个 MonoBehaviour 来监听各个事件显然不好, 下面是通过利用 Delegate 和 Event 来做一个通用类 UIEventListener 来处理事件 (观察者模式).

  1. TestEvent 脚本绑定在 Button 上即可。
//UIEventListener.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler {
     

	// 定义事件代理
	public delegate void UIEventProxy(GameObject gb);

	// 鼠标点击事件
	public event UIEventProxy OnClick;

	// 鼠标进入事件
	public event UIEventProxy OnMouseEnter;

	// 鼠标滑出事件
	public event UIEventProxy OnMouseExit;

	public void OnPointerClick(PointerEventData eventData){
     
		if (OnClick != null)
			OnClick (this.gameObject);
	}

	public void OnPointerEnter(PointerEventData eventData){
     
		if (OnMouseEnter != null)
			OnMouseEnter (this.gameObject);
	}

	public void OnPointerExit(PointerEventData eventData){
     
		if (OnMouseExit != null)
			OnMouseExit (this.gameObject);
	}

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestEvent : MonoBehaviour {
     

	void Start () {
     
		Button btn = this.GetComponent<Button> ();
		UIEventListener btnListener = btn.gameObject.AddComponent<UIEventListener> ();

		btnListener.OnClick += delegate(GameObject gb) {
     
			Debug.Log(gb.name + " OnClick");
		};

		btnListener.OnMouseEnter += delegate(GameObject gb) {
     
			Debug.Log(gb.name + " OnMouseEnter");
		};

		btnListener.OnMouseExit += delegate(GameObject gb) {
     
			Debug.Log(gb.name + " OnMOuseExit");
		};
	}

}

unity笔记--按钮绑定事件_第5张图片

6.通过重写button类

  1. 删掉button自带的组件Button。
  2. 挂上自己写的脚本。
public class NewBtn : Button
{
     
    enum Selection
    {
     
        Normal,
        Highlighted,
        Pressed,
        Selected,
        Disabled
    }
    Selection _selection;

    protected override void DoStateTransition(SelectionState state, bool instant)
    {
     
        base.DoStateTransition(state, instant);

        switch (state)
        {
     
            case SelectionState.Normal:
                _selection = Selection.Normal;
                break;
            case SelectionState.Highlighted:
                _selection = Selection.Highlighted;
                break;
            case SelectionState.Pressed:
                _selection = Selection.Pressed;
                break;
            case SelectionState.Selected:
                _selection = Selection.Selected;
                break;
            case SelectionState.Disabled:
                _selection = Selection.Disabled;
                break;
        }
    }


    private void OnGUI()
    {
     
        GUI.skin.box.fontSize = 14;
        switch (_selection)
        {
     
        	case Selection.Normal:
              	//Normalaction
                break;
           	case Selection.Highlighted:
                //Highlightedaction
                break;
            case Selection.Pressed:
                //Pressedaction
                break;
          	case Selection.Selected:
                //Selectedaction
                break;
         	case SelectionState.Disabled:
                //Disabledaction
                break;
        }
    }
}

你可能感兴趣的:(笔记,unity)