Unity UGUi 怎么判断 点击按钮的抬起和按下
方法:
1、检测按钮的按下与抬起就需要对按钮的事件进行监听。
2、这里我们创建一个脚本,把这个脚本添加引用。
[csharp] view plain copy。
using UnityEngine.EventSystems 。
3、并在脚本继承的MonoBehaviour后面加入接口。
[csharp] view plain copy。
public class ceshi : MonoBehaviour,IPointerDownHandler, IPointerUpHandler。
4、添加了接口要把这个接口进行实现。
[csharp] view plain copy
public class ceshi : MonoBehaviour,IPointerDownHandler, IPointerUpHandler
{
public void OnPointerDown(PointerEventData eventData)
{
print("按下!!!!");
}
public void OnPointerUp(PointerEventData eventData)
{
print("抬起!!!!");
}
}
5、处理后。
2016-05-21 · 知道合伙人数码行家
知道合伙人数码行家
向TA提问 私信TA
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{
public delegate void VoidDelegate (GameObject go);
public VoidDelegate onClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
static public EventTriggerListener Get (GameObject go)
{
EventTriggerListener listener = go.GetComponent(); if (listener == null) listener = go.AddComponent(); return listener;
}
public override void OnPointerClick(PointerEventData eventData)
{
if(onClick != null) onClick(gameObject);
}
public override void OnPointerDown (PointerEventData eventData){
if(onDown != null) onDown(gameObject);
}
public override void OnPointerEnter (PointerEventData eventData){
if(onEnter != null) onEnter(gameObject);
}
public override void OnPointerExit (PointerEventData eventData){
if(onExit != null) onExit(gameObject);
}
public override void OnPointerUp (PointerEventData eventData){
if(onUp != null) onUp(gameObject);
}
public override void OnSelect (BaseEventData eventData){
if(onSelect != null) onSelect(gameObject);
}
public override void OnUpdateSelected (BaseEventData eventData){
if(onUpdateSelect != null) onUpdateSelect(gameObject);
}
}
然后在你的界面里面写入监听按钮的代码。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class UIMain : MonoBehaviour {
Button button;
Image image;
void Start ()
{
button = transform.Find("Button").GetComponent