InputDefine 类unity 按钮长按事件unity 按钮长按事件3D植物:(公共变量、常量、枚举、事件)定义
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputDefine
{
/// 两次点击的时间间隔
public const float DOUBLE_CLICK_RATE = 0.3f;
/// 连续按住0.5s后判定为长按
public const float LONG_PRESS_SCALE = 0.5f;
/// 判断是否点击在UI上面
public static bool IsTouchUI()
{
if (UnityEngine.EventSystems.EventSystem.current == null) return false;
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount < 1) return false;
if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return true;
}
else
{
if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) return true;
}
return false;
}
}
/// 输入事件类型
public enum EnumInputEventType
{
/// 没有事件
None,
/// 单击
Click,
/// 双击
DoubleClick,
/// 滑动
//Slide,
/// 长按
LongPress,
}
///
/// 输入事件
///
/// 是否触摸到UI
/// (PC端)0表示左键,1表示右键,2表示中键
/// 输入事件类型
public delegate void DelInputEvent(bool isTouchUI, int keyCode, EnumInputEventType enumInputEventType);
///
/// 鼠标点击
///
/// 是否触摸到UI
/// (PC端)0表示左键,1表示右键,2表示中键
/// 连续点次数
public delegate void DelClickEvent(bool isTouchUI, int keyCode, Vector2 clickPs, int clickCount);
MouseEvent 类:鼠标事件(PC端)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// 鼠标事件
public class MouseEvent : MonoBehaviour
{
public DelInputEvent onInputEventChange;
public DelClickEvent onClickEvent;
private MouseParam[] mouseParams = new MouseParam[] { new MouseParam(), new MouseParam(), new MouseParam() };
private void Update()
{
addMouseInputEventTypeJudge();
}
/// 添加
private void addMouseInputEventTypeJudge()
{
for(int i=0;i< mouseParams.Length;i++)
{
mouseInputEventTypeJudge(i);
}
}
/// 鼠标左键输入事件判定
private void mouseInputEventTypeJudge(int mouseKeyCode)
{
//鼠标按下
if (Input.GetMouseButtonDown(mouseKeyCode))
{
mouseParams[mouseKeyCode].startTime = Time.realtimeSinceStartup;
}
//鼠标按住
if (Input.GetMouseButton(mouseKeyCode))
{
mouseParams[mouseKeyCode].holdTime = Time.realtimeSinceStartup - mouseParams[mouseKeyCode].startTime;
if (mouseParams[mouseKeyCode].holdTime >= InputDefine.LONG_PRESS_SCALE)
{
if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(),mouseKeyCode,EnumInputEventType.LongPress);
}
}
//鼠标抬起
if (Input.GetMouseButtonUp(mouseKeyCode))
{
if (mouseParams[mouseKeyCode].holdTime >= InputDefine.LONG_PRESS_SCALE) return;
if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(), mouseKeyCode,EnumInputEventType.Click);
mouseParams[mouseKeyCode].t2 = Time.realtimeSinceStartup;
if (mouseParams[mouseKeyCode].t2 - mouseParams[mouseKeyCode].t1 < InputDefine.DOUBLE_CLICK_RATE)
{
//Debug.Log(mouseKeyCode + ":双击!");
mouseParams[mouseKeyCode].clickCount++;
if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(), mouseKeyCode,EnumInputEventType.DoubleClick);
}
else
{
mouseParams[mouseKeyCode].clickCount = 1;
}
mouseParams[mouseKeyCode].t1 = mouseParams[mouseKeyCode].t2;
if (onClickEvent != null) onClickEvent(InputDefine.IsTouchUI(), mouseKeyCode,Input.mousePosition, mouseParams[mouseKeyCode].clickCount);
}
}
public struct MouseParam
{
public float startTime;
public float holdTime;
public float t1;
public float t2;
public int clickCount;
}
}
TouchEvent 类材质材料:触摸事件(移动端)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// 触摸事件
public class TouchEvent : MonoBehaviour
{
public DelClickEvent onClickEvent;
private void Update()
{
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began)
{
if (onClickEvent != null) onClickEvent(InputDefine.IsTouchUI(), i, touch.position, touch.tapCount);
//Debug.Log(i + "---" + "touch.tapCount:" + touch.tapCount+ "---touch.position:"+ touch.position);
}
}
}
}
}