Unity3D:如何检测按下和释放按钮的时间 [英] Unity3D: How to detect when a button is being held down and released
本文介绍了Unity3D:如何检测按下和释放按钮的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个UI按钮.我想在用户按下按钮时显示文本,而在用户释放按钮时隐藏文本.
I have a UI button. I want to show a text when the user is pressing the button and hide the text when the user releases the button.
我该怎么做?
推荐答案
您必须通过扩展Button类并重写方法OnPoiterDown和OnPointerUp来创建自己的自定义按钮.将MyButton组件而不是Button附加到游戏对象上
You have to create your own custom button by extending Button class and override method OnPoiterDown and OnPointerUp. Attach MyButton component instead of Button to your gameobject
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MyButton : Button
{
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
Debug.Log("Down");
//show text
}
public override void OnPointerUp(PointerEventData eventData)
{
base.OnPointerUp(eventData);
Debug.Log("Up");
//hide text
}
}
这篇关于Unity3D:如何检测按下和释放按钮的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文