Input.GetTouch 获取触摸

static function GetTouch (index : int) : Touch

Description描述

Returns object representing status of a specific touch (Does not allocate temporary variables).

返回一个存放触摸信息的对象(不允许分配临时变量)。

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);
		}
	}
}
// Moves object according to finger movement on the screen
//根据手指在屏幕上移动来移动物体

var speed : float = 0.1;
function Update () {
	if (Input.touchCount > 0 &&
	Input.GetTouch(0).phase == TouchPhase.Moved) {

		// Get movement of the finger since last frame
		//获取手指自最后一帧的移动
		var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;

		// Move object across XY plane
		//移动物体在XY平面
		transform.Translate (-touchDeltaPosition.x * speed,
		-touchDeltaPosition.y * speed, 0);
	}
}

另一个例子

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public GameObject projectile;
	void Update() {
		int i = 0;
		while (i < Input.touchCount) {
			if (Input.GetTouch(i).phase == TouchPhase.Began)
				clone = Instantiate(projectile, transform.position, transform.rotation);

			++i;
		}
	}
}
// Instantiates a projectile whenever the user taps on the screen
//实例一个炮弹,当用户在屏幕上点击时

var projectile : GameObject;
function Update () {
	for (var i = 0; i < Input.touchCount; ++i) {
		if (Input.GetTouch(i).phase == TouchPhase.Began) {
			clone = Instantiate (projectile, transform.position, transform.rotation);
		}
	}
}
using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public GameObject particle;
	void Update() {
		int i = 0;
		while (i < Input.touchCount) {
			if (Input.GetTouch(i).phase == TouchPhase.Began) {
				Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
				if (Physics.Raycast(ray))
					Instantiate(particle, transform.position, transform.rotation);

			}
			++i;
		}
	}
}
// Shoot a ray whenever the user taps on the screen
//发射一条光线,当用户在屏幕上点击时
var particle : GameObject;
function Update () {
	for (var i = 0; i < Input.touchCount; ++i) {
		if (Input.GetTouch(i).phase == TouchPhase.Began) {
			// Construct a ray from the current touch coordinates
			//构造一条光线,从当前的触摸坐标
			var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
			if (Physics.Raycast (ray)) {
				// Create a particle if hit
				//如果击中,创建一个粒子
				Instantiate (particle, transform.position, transform.rotation);
			}
		}
	}
}
最后修改:2011年3月12日 Saturday 21:52

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。