using UnityEngine;
using System.Collections;
using System.IO;
public class Move : MonoBehaviour
{
private Touch oldTouch1; //上次觸摸點1(手指1)
private Touch oldTouch2; //上次觸摸點2(手指2)
public float timer = 2.0f;
void Start()
{
}
Vector3 screenPos;
Vector3 detalPos;
Vector3 velocity;
float damp=0.2f;
bool isDrag=false;
Vector3 startMousePosition;
void Update()
{
#if UNITY_EDITOR
if (Input.GetMouseButton(0)) {
float x=Input.GetAxis("Mouse X");
float y=Input.GetAxis("Mouse Y");
if (Input.GetMouseButtonDown(0)) {
startMousePosition=Input.mousePosition;
screenPos = Camera.main.WorldToScreenPoint (transform.position);
Vector3 worldPos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPos.z)); detalPos = transform.position -worldPos; timer = 0f; } else if (Input.mousePosition == startMousePosition){ if (timer >= 1f) isDrag = true; else { timer += Time.deltaTime; if(isDrag) isDrag=false; } } else if (Input.mousePosition != startMousePosition) { if (isDrag) { //screenPos = Camera.main.WorldToScreenPoint (transform.position); Vector3 targetPos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPos.z)); transform.position = Vector3.SmoothDamp (transform.position, targetPos +detalPos, ref velocity, damp); } else { //相對於世界座標中心向右旋轉物體 transform.Rotate(transform.TransformDirection(Vector3.down),x, Space.World); //相對於世界座標中心向上旋轉物體 transform.Rotate(Camera.main.transform.TransformDirection(Vector3.right),y, Space.World); } } } return; #endif // if (2 == Input.touchCount) { //多點觸摸, 放大縮小 Touch newTouch1 = Input.GetTouch (0); Touch newTouch2 = Input.GetTouch (1); //第2點剛開始接觸屏幕, 只記錄,不作處理 if (newTouch2.phase == TouchPhase.Began) { oldTouch2 = newTouch2; oldTouch1 = newTouch1; return; } //計算老的兩點距離和新的兩點間距離,變大要放大模型,變小要縮放模型 float oldDistance = Vector2.Distance (oldTouch1.position, oldTouch2.position); float newDistance = Vector2.Distance (newTouch1.position, newTouch2.position); //兩個距離之差,爲正表示放大手勢, 爲負表示縮小手勢 float offset = newDistance - oldDistance; //放大因子, 一個像素按 0.01倍來算(100可調整) float scaleFactor = offset / 100f; Vector3 localScale = transform.localScale; Vector3 scale = new Vector3 (localScale.x + scaleFactor, localScale.y + scaleFactor, localScale.z + scaleFactor); //最小縮放到 0.3 倍 // if (scale.x < 0.3f) { // scale = Vector3.one * 0.3f; // } else if (scale.x > 3f) { // scale = Vector3.one * 3f; // } scale =Vector3.one* Mathf.Clamp (scale.x,0.3f,3f); transform.localScale = scale; //記住最新的觸摸點,下次使用 oldTouch1 = newTouch1; oldTouch2 = newTouch2; } } }spa