新建一个Cube物体,创建一个脚本TouchTest04,将该脚本挂到Cube物体上,代码如下:
- using UnityEngine;
- using System.Collections;
- public class TouchTest04 : MonoBehaviour
- {
- void Update()
- {
- int touchNum = Input.touchCount;
- if (touchNum > 0)
- {
- Touch touch = Input.GetTouch(0);
- if (touch.phase == TouchPhase.Moved)
- {
- Vector3 dir = new Vector3(touch.deltaPosition.x, touch.deltaPosition.y, 0f) * 0.1f;
- transform.Translate(dir);
- }
- }
- }
- }
OK,完成!