unity基础开发----常用代码鼠标滑动,触摸事件

判断鼠标向左还是向右滑动

private var first = Vector2.zero;
private var second = Vector2.zero;
function Update () {
}
function OnGUI () {
if(Event.current.type == EventType.MouseDown){//记录鼠标按下的位置
first = Event.current.mousePosition ;
}
if(Event.current.type == EventType.MouseDrag){//记录鼠标拖动的位置
second = Event.current.mousePosition ;
if(second.x<first.x){//拖动的位置的x坐标比按下的位置的x坐标小时,响应向左事件
print("left");
}
if(second.x>first.x){//拖动的位置的x坐标比按下的位置的x坐标大时,响应向右事件
print("right");
}
first = second;
}
}

屏幕触摸事件, 移动物体代码
using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public float speed = 0.1F;
    void Update() {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
            transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值