using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using UnityEngine.UI;
///
/// 脚本位置:UGUI按钮组件身上
/// 脚本功能:实现按钮长按状态的判断
/// 创建时间:2015年11月17日
///
// 继承:按下,抬起和离开的三个接口
public class OnButtonPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
// 延迟时间
private float delay = 1f;
// 按钮是否是按下状态
private bool isDown = false;
// 按钮最后一次是被按住状态时候的时间
private float lastIsDownTime;
public GameObject pasteBtn;
void Start()
{
Debug.Log("绑定方法++++++++++");
pasteBtn.GetComponent<Button>().onClick.AddListener(OnClickPastedBtn);
}
private void OnEnable()
{
pasteBtn.SetActive(false);
}
//点击粘贴板
void OnClickPastedBtn()
{
Debug.Log("粘贴文件++++++++++++=");
pasteBtn.SetActive(false);
string str = GUIUtility.systemCopyBuffer;
GetComponent<InputField>().text = str;
Debug.Log(str);
}
void Update()
{
// 如果按钮是被按下状态
if (isDown)
{
// 当前时间 - 按钮最后一次被按下的时间 > 延迟时间0.2秒
if (Time.time - lastIsDownTime > delay)
{
// 触发长按方法
Debug.Log("长按");
pasteBtn.SetActive(true);
// 记录按钮最后一次被按下的时间
lastIsDownTime = Time.time;
}
}
}
// 当按钮被按下后系统自动调用此方法
public void OnPointerDown(PointerEventData eventData)
{
isDown = true;
pasteBtn.SetActive(false);
lastIsDownTime = Time.time;
}
// 当按钮抬起的时候自动调用此方法
public void OnPointerUp(PointerEventData eventData)
{
isDown = false;
}
// 当鼠标从按钮上离开的时候自动调用此方法
public void OnPointerExit(PointerEventData eventData)
{
isDown = false;
}
}
按钮长按粘贴功能
最新推荐文章于 2024-04-11 16:21:09 发布