兩個手指多點觸控 滑動 放大縮小 縮放
阿新 • • 發佈:2019-02-11
using UnityEngine;
using System.Collections;
public class FingersTwo : MonoBehaviour {
public GameObject m1, m2;
int Z_Far = 0, Z_Near;
float sulv = 0.001f;
Transform ziji;
Vector3 zhongZhuang;
Vector2 pianyi;
// Use this for initialization
void Start () {
///////////////
Z_Far = 10;
Z_Near = -10;
//////////////////
m1.transform.localPosition = new Vector3(0, 0, 0);
m2.transform.localPosition = new Vector3(0, 0, 0);
pianyi.x= Screen.width/2;
pianyi.y = Screen.height/2;
ziji = this.transform;
zhongZhuang = ziji.position;
}
// Update is called once per frame
void Update()
{
ziji = this.transform;
if (Input.touches.Length > 1)
{
if (Input.touches[0].fingerId == 0)
{
m1.transform.localPosition = Input.touches[0].position - pianyi;
}
if (Input.touches[1].fingerId == 1)
{
m2.transform.localPosition = Input.touches[1].position - pianyi;
}
//m1.transform.localPosition = Input.touches[0].position - pianyi;
//m2.transform.localPosition = Input.touches[1].position - pianyi;
}
else if (Input.touches.Length == 1)
{
if (Input.touches[0].fingerId == 0)
{
m1.transform.localPosition = Input.touches[0].position - pianyi;
}
m2.transform.localPosition = Input.touches[1].position - pianyi;
// m1.transform.localPosition = Input.touches[0].position - pianyi;
}
else
{
m1.transform.localPosition = new Vector3(0, 0, 0);
m2.transform.localPosition = new Vector3(0, 0, 0);
}
//if (Input.touches[0].phase == TouchPhase.Began )
//{
//}
if (Input.touches[1].phase == TouchPhase.Began)
{
start_dis = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
}
if (Input.touches.Length < 2 || (Input.touches[1].phase == TouchPhase.Stationary && Input.touches[0].phase == TouchPhase.Stationary))
{
start_dis = -1;
start_dis = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
}
/////////////////////////////// 第一個方法
//if (start_dis > 0)
//{
// dis_C = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
// dis_B = Vector3.Distance(m1.transform.localPosition, m2.transform.localPosition);
// //確定為兩個手指按下
// if (dis_B > start_dis)
// {
// if (ziji.localPosition.z < Z_Far)
// {
// this.transform.Translate(new Vector3(0, 0, 0.1f));
// }
// }
// else if (dis_B < start_dis)
// {
// if (ziji.localPosition.z > Z_Near)
// //this.transform.localPosition += new Vector3(0, 0, -0.1f);
// this.transform.Translate(new Vector3(0, 0, -0.1f));
// }
//}
/////////////////////////////// 第二個方法
if (start_dis > 0)
{
// dis_C = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
dis_B = Vector3.Distance(Input.touches[0].position, Input.touches[1].position) - start_dis;
// dis_B = Mathf.Clamp(dis_B, Z_Near, Z_Far);
//確定為兩個手指按下
if (ziji.localPosition.z <= Z_Far && ziji.localPosition.z >= Z_Near)
zhongZhuang += new Vector3(0, 0, dis_B * sulv);
if (zhongZhuang.z > Z_Far)
zhongZhuang = new Vector3(0, 0, Z_Far);
else if (zhongZhuang.z < Z_Near)
zhongZhuang = new Vector3(0, 0, Z_Near);
ziji.localPosition = zhongZhuang;
//if (dis_B > start_dis)
//{
// if (ziji.localPosition.z < Z_Far)
// {
// this.transform.Translate(new Vector3(0, 0, 0.1f));
// }
//}
//else if (dis_B < start_dis)
//{
// if (ziji.localPosition.z > Z_Near)
// //this.transform.localPosition += new Vector3(0, 0, -0.1f);
// this.transform.Translate(new Vector3(0, 0, -0.1f));
//}
}
}
float start_dis = 0, dis_B, dis_C;
void OnGUI()
{
if (GUI.Button(new Rect(100, 100, 200, 80), "1:" + m1.transform.localPosition)) { }
if (GUI.Button(new Rect(100, 200, 200, 80), "2:" + m2.transform.localPosition)) { }
if (GUI.Button(new Rect(100, 300, 200, 80), "3:" + Input.touches[0].position)) {
//真實位置
}
if (GUI.Button(new Rect(100, 400, 200, 80), "4:" + Input.touches[0].deltaPosition)) {
//運動方向
}
if (GUI.Button(new Rect(100, 500, 200, 80), "5:" + Input.touches[0].rawPosition)) {
//初始點位置
}
if (GUI.Button(new Rect(100, 600, 200, 80), "6:" + Input.touches[0].fingerId + " phase: " + Input.touches[1].phase))
{
}
if (GUI.Button(new Rect(400, 100, 200, 80), "7:" + Input.touches[1].fingerId + " now: " + ziji.localPosition.z + " \n start_dis: " + start_dis + " dis_B: " + dis_B))
{
////////////////////////////
//if (GUI.Button(new Rect(400, 200, 200, 80), "8:" + dis_B + " dis_C: " + dis_C))
//{
//}
}
}
}
using System.Collections;
public class FingersTwo : MonoBehaviour {
public GameObject m1, m2;
int Z_Far = 0, Z_Near;
float sulv = 0.001f;
Transform ziji;
Vector3 zhongZhuang;
Vector2 pianyi;
// Use this for initialization
void Start () {
///////////////
Z_Far = 10;
Z_Near = -10;
//////////////////
m1.transform.localPosition = new Vector3(0, 0, 0);
m2.transform.localPosition = new Vector3(0, 0, 0);
pianyi.x= Screen.width/2;
pianyi.y = Screen.height/2;
ziji = this.transform;
zhongZhuang = ziji.position;
}
// Update is called once per frame
void Update()
{
ziji = this.transform;
if (Input.touches.Length > 1)
{
if (Input.touches[0].fingerId == 0)
{
m1.transform.localPosition = Input.touches[0].position - pianyi;
}
if (Input.touches[1].fingerId == 1)
{
m2.transform.localPosition = Input.touches[1].position - pianyi;
}
//m1.transform.localPosition = Input.touches[0].position - pianyi;
//m2.transform.localPosition = Input.touches[1].position - pianyi;
}
else if (Input.touches.Length == 1)
{
if (Input.touches[0].fingerId == 0)
{
m1.transform.localPosition = Input.touches[0].position - pianyi;
}
m2.transform.localPosition = Input.touches[1].position - pianyi;
// m1.transform.localPosition = Input.touches[0].position - pianyi;
}
else
{
m1.transform.localPosition = new Vector3(0, 0, 0);
m2.transform.localPosition = new Vector3(0, 0, 0);
}
//if (Input.touches[0].phase == TouchPhase.Began )
//{
//}
if (Input.touches[1].phase == TouchPhase.Began)
{
start_dis = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
}
if (Input.touches.Length < 2 || (Input.touches[1].phase == TouchPhase.Stationary && Input.touches[0].phase == TouchPhase.Stationary))
{
start_dis = -1;
start_dis = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
}
/////////////////////////////// 第一個方法
//if (start_dis > 0)
//{
// dis_C = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
// dis_B = Vector3.Distance(m1.transform.localPosition, m2.transform.localPosition);
// //確定為兩個手指按下
// if (dis_B > start_dis)
// {
// if (ziji.localPosition.z < Z_Far)
// {
// this.transform.Translate(new Vector3(0, 0, 0.1f));
// }
// }
// else if (dis_B < start_dis)
// {
// if (ziji.localPosition.z > Z_Near)
// //this.transform.localPosition += new Vector3(0, 0, -0.1f);
// this.transform.Translate(new Vector3(0, 0, -0.1f));
// }
//}
/////////////////////////////// 第二個方法
if (start_dis > 0)
{
// dis_C = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
dis_B = Vector3.Distance(Input.touches[0].position, Input.touches[1].position) - start_dis;
// dis_B = Mathf.Clamp(dis_B, Z_Near, Z_Far);
//確定為兩個手指按下
if (ziji.localPosition.z <= Z_Far && ziji.localPosition.z >= Z_Near)
zhongZhuang += new Vector3(0, 0, dis_B * sulv);
if (zhongZhuang.z > Z_Far)
zhongZhuang = new Vector3(0, 0, Z_Far);
else if (zhongZhuang.z < Z_Near)
zhongZhuang = new Vector3(0, 0, Z_Near);
ziji.localPosition = zhongZhuang;
//if (dis_B > start_dis)
//{
// if (ziji.localPosition.z < Z_Far)
// {
// this.transform.Translate(new Vector3(0, 0, 0.1f));
// }
//}
//else if (dis_B < start_dis)
//{
// if (ziji.localPosition.z > Z_Near)
// //this.transform.localPosition += new Vector3(0, 0, -0.1f);
// this.transform.Translate(new Vector3(0, 0, -0.1f));
//}
}
}
float start_dis = 0, dis_B, dis_C;
void OnGUI()
{
if (GUI.Button(new Rect(100, 100, 200, 80), "1:" + m1.transform.localPosition)) { }
if (GUI.Button(new Rect(100, 200, 200, 80), "2:" + m2.transform.localPosition)) { }
if (GUI.Button(new Rect(100, 300, 200, 80), "3:" + Input.touches[0].position)) {
//真實位置
}
if (GUI.Button(new Rect(100, 400, 200, 80), "4:" + Input.touches[0].deltaPosition)) {
//運動方向
}
if (GUI.Button(new Rect(100, 500, 200, 80), "5:" + Input.touches[0].rawPosition)) {
//初始點位置
}
if (GUI.Button(new Rect(100, 600, 200, 80), "6:" + Input.touches[0].fingerId + " phase: " + Input.touches[1].phase))
{
}
if (GUI.Button(new Rect(400, 100, 200, 80), "7:" + Input.touches[1].fingerId + " now: " + ziji.localPosition.z + " \n start_dis: " + start_dis + " dis_B: " + dis_B))
{
////////////////////////////
//if (GUI.Button(new Rect(400, 200, 200, 80), "8:" + dis_B + " dis_C: " + dis_C))
//{
//}
}
}
}