返回介绍

Input.GetTouch 获取触摸

发布于 2019-12-18 15:37:53 字数 3804 浏览 1246 评论 0 收藏 0

JavaScript => public static function GetTouch(index: int): Touch;
C# => public static Touch GetTouch(int index);

Description 描述

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

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

JavaScript:

#pragma strict
public var speed = 0.1F;
function Update() {
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
		// Get movement of the finger since last frame
		var touchDeltaPosition = Input.GetTouch(0).deltaPosition;
		// Move object across XY plane
		transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public float speed = 0.1F;
    void Update() {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
            // Get movement of the finger since last frame
            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
 
            // Move object across XY plane
            transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
        }
    }
}

JavaScript:

#pragma strict
public var projectile;
public var clone;
function Update() {
	for (var i = 0; i < Input.touchCount; ++i) {
		if (Input.GetTouch(i).phase == TouchPhase.Began)
			clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
	}
}

C#:

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

JavaScript:

#pragma strict
public var particle;
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);
			// Create a particle if hit
			if (Physics.Raycast(ray))
				Instantiate(particle, transform.position, transform.rotation) as GameObject;
		}
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public GameObject particle;
    void Update() {
       for (var i = 0; i < Input.touchCount; ++i) {
            if (Input.GetTouch(i).phase == TouchPhase.Began) {
 
                // Construct a ray from the current touch coordinates
                Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                // Create a particle if hit
                if (Physics.Raycast(ray))
                    Instantiate(particle, transform.position, transform.rotation) as GameObject;                
            }
        }
    }
}

Input

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文