素材巴巴 > 程序开发 >

unity笔记--按钮绑定事件

程序开发 2023-09-16 22:55:28

原文地址: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 方法。
    可视化创建及事件绑定

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

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

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 initializationvoid Start () {Button btn = this.GetComponent

通过 EventTrigger 实现按钮点击事件

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..");}}

通过 MonoBehaviour 实现事件类接口

5.采用观察者模式监听

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

  1. TestEvent 脚本绑定在 Button 上即可。
//UIEventListener.csusing 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

观察者模式

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://Normalactionbreak;case Selection.Highlighted://Highlightedactionbreak;case Selection.Pressed://Pressedactionbreak;case Selection.Selected://Selectedactionbreak;case SelectionState.Disabled://Disabledactionbreak;}}
 }
 

标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。