unity - 屏幕滑动操作方案

该博客介绍了在Unity中优化屏幕滑动操作的方法,包括防止错误判断的有效距离设置、不在OnGUI上处理以提高控制便利性以及实现多样化判断。通过检测鼠标按下、按住和松开的状态来实现滑动判定,并提供了使用Input.GetMouseButtonDown(0)的代码示例,建议读者探索UGUI事件系统以获取更多交互事件处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目标:判断滑动屏幕方向,只调用一次滑动事件处理函数

因为功能简单易懂,代码也比较简单,不再详细说明,参考官方API:http://docs.unity3d.com/ScriptReference/EventType.html

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	enum slideVector { nullVector, left, right };

	private Vector2 lastPos;//上一个位置

	private Vector2 currentPos;//下一个位置

	private slideVector currentVector = slideVector.nullVector;//当前滑动方向

	private float timer;//时间计数器

	public float offsetTime = 0.01f;//判断的时间间隔

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	void OnGUI(){
		if (Event.current.type == EventType.MouseDown) {//滑动开始
			lastPos = Event.current.mousePosition;
			currentPos = Event.current.mousePosition;
			timer = 0;

			//TODO click event
			Debug.Log ("Click begin && Drag begin");
		}

		if (Event.current.type == EventType.MouseDrag) {//滑动过程
			currentPos = Event.current.mousePosition;
			time
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值