// Update is called once per frame void Update() { //Property reset for each frame Width = 0; Delta = 0; Ratio = 1; #if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS) //Only platforms you want to obtain with touch if (Input.touchCount == 2) { Touch touch = (Input.touches[1].fingerId == 1) ? Input.touches[1] : Input.touches[0]; if (!IsPinching && touch.phase == TouchPhase.Began) { Vector2 center = (Input.touches[0].position + Input.touches[1].position) / 2; if (validArea.xMin * Screen.width <= center.x && center.x <= validArea.xMax * Screen.width && validArea.yMin * Screen.height <= center.y && center.y <= validArea.yMax * Screen.height) { IsPinching = true; Width = startDistance = oldDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); if (isNormalized) { float unit = widthReference ? Screen.width : Screen.height; Width /= unit; center /= unit; } if (OnPinchStart != null) { OnPinchStart.Invoke(Width, center); } } } else if (IsPinching) { float endDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); Width = endDistance; Delta = endDistance - oldDistance; Ratio = endDistance / startDistance; oldDistance = endDistance; if (isNormalized) { float unit = widthReference ? Screen.width : Screen.height; Width /= unit; Delta /= unit; } if (OnPinch != null) { OnPinch.Invoke(Width, Delta, Ratio); } } } else #endif { IsPinching = false; } }
// Update is called once per frame void Update() { //プロパティはフレーム毎にリセット Width = 0; Delta = 0; Ratio = 1; #if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS) //タッチで取得したいプラットフォームのみ if (Input.touchCount == 2) //ピンチでの操作(2本指のみ) { //※fingerId と touches[] のインデクスは必ずしも一致しないらしいので fingerId=1 となっている方を取得(指1本→2本になったとき可能とするため) Touch touch = (Input.touches[1].fingerId == 1) ? Input.touches[1] : Input.touches[0]; if (!IsPinching && touch.phase == TouchPhase.Began) //新しく認識したときのみ { //認識する画面上の領域内か?(2本の指の中心の座標を基準にする) Vector2 center = (Input.touches[0].position + Input.touches[1].position) / 2; if (validArea.xMin * Screen.width <= center.x && center.x <= validArea.xMax * Screen.width && validArea.yMin * Screen.height <= center.y && center.y <= validArea.yMax * Screen.height) { IsPinching = true; //ピンチ開始 //fingerId=0~1 のみ(必ず最初と2本目の指)。指3本→2本(0-2 など)は不可とする。 Width = startDistance = oldDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); if (isNormalized) { float unit = widthReference ? Screen.width : Screen.height; Width /= unit; //画面幅で正規化すれば、解像度に依存しなくなる center /= unit; } if (OnPinchStart != null) { OnPinchStart.Invoke(Width, center); //開始時は必ず Delta=0, Ratio=1 となる } } } else if (IsPinching) //既に認識されているときのみ:3本→2本になったときは無効になる { float endDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); Width = endDistance; Delta = endDistance - oldDistance; //直前との差分 Ratio = endDistance / startDistance; //開始時のピンチ幅(px距離)を基準にした倍率になる oldDistance = endDistance; if (isNormalized) { float unit = widthReference ? Screen.width : Screen.height; Width /= unit; //画面幅で正規化すれば、解像度に依存しなくなる Delta /= unit; } if (OnPinch != null) { OnPinch.Invoke(Width, Delta, Ratio); } } } else //タッチが2つでないときは全て無効にする #endif { IsPinching = false; } }
// Update is called once per frame void Update() { //Property reset for each frame //プロパティはフレーム毎にリセット Width = 0; Delta = 0; Ratio = 1; #if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS) //Only platforms you want to obtain with touch //タッチで取得したいプラットフォームのみ if (Input.touchCount == 2) //Operation with pinch (2 fingers only) //ピンチでの操作(2本指のみ) { //(*) The index of fingerId and touches[] does not necessarily match, so get fingerId = 1 (to make it possible when 1 finger -> 2) //※fingerId と touches[] のインデクスは必ずしも一致しないらしいので fingerId=1 となっている方を取得(指1本→2本になったとき可能とするため) Touch touch = (Input.touches[1].fingerId == 1) ? Input.touches[1] : Input.touches[0]; if (!IsPinching && touch.phase == TouchPhase.Began) //Only when newly recognized //新しく認識したときのみ { //Is it within the area on the screen to recognize? (Based on the coordinates of the center of two fingers) //認識する画面上の領域内か?(2本の指の中心の座標を基準にする) Vector2 center = (Input.touches[0].position + Input.touches[1].position) / 2; if (validArea.xMin * Screen.width <= center.x && center.x <= validArea.xMax * Screen.width && validArea.yMin * Screen.height <= center.y && center.y <= validArea.yMax * Screen.height) { IsPinching = true; //Pinch start //ピンチ開始 //fingerId = 0 to 1 only (always the first and second fingers). 3 fingers -> 2 (0 - 2 etc.) are not allowed. //fingerId=0~1 のみ(必ず最初と2本目の指)。指3本→2本(0-2 など)は不可とする。 Width = startDistance = oldDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); if (isNormalized) { float unit = widthReference ? Screen.width : Screen.height; Width /= unit; //If it is normalized by screen width, it becomes independent of resolution. //画面幅で正規化すれば、解像度に依存しなくなる center /= unit; } if (OnPinchStart != null) { OnPinchStart.Invoke(Width, center); //Delta = 0 and Ratio = 1 at the start. //開始時は必ず Delta=0, Ratio=1 となる } } } else if (IsPinching) //Only when it is already recognized: When 3 -> 2, it becomes invalid. //既に認識されているときのみ:3本→2本になったときは無効になる { float endDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); Width = endDistance; Delta = endDistance - oldDistance; //Difference from immediately before //直前との差分 Ratio = endDistance / startDistance; //The magnification is based on the pinch width at the start (px distance). //開始時のピンチ幅(px距離)を基準にした倍率になる oldDistance = endDistance; if (isNormalized) { float unit = widthReference ? Screen.width : Screen.height; Width /= unit; //If it is normalized by screen width, it becomes independent of resolution. //画面幅で正規化すれば、解像度に依存しなくなる Delta /= unit; } if (OnPinch != null) { OnPinch.Invoke(Width, Delta, Ratio); } } } else //Invalid all when there are not two touch. //タッチが2つでないときは全て無効にする #endif { IsPinching = false; } }
private void Update() { //プロパティはフレーム毎に初期化 Width = 0; Delta = 0; Ratio = 1; if (Input.touchCount == 2) { //※fingerIdとtouch[]のインデックスは必ずしも一致しないのでfingerId=1となっている方を取得 //(指1本から2本になったときに可能とするため) Touch touch = (Input.touches[1].fingerId == 1) ? Input.touches[1] : Input.touches[0]; //新しく認識したときのみ if (!IsPinching && touch.phase == TouchPhase.Began) { //認識する画面上の領域内か?(2本の指の中心の座標を標準にする) Vector2 center = (Input.touches[0].position + Input.touches[1].position) / 2; if (validArea.xMin * Screen.width <= center.x && center.x <= validArea.xMax * Screen.width && validArea.yMin * Screen.height <= center.y && center.y <= validArea.yMax * Screen.height) { IsPinching = true; //ピンチ開始 //fingerId=0~1のみ(必ず最初の2本目の指)。指3本→2本(0-2など)は不可 Width = startDinstance = oldDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); if (isNormalized) { //画面幅で正規化して解像度に依存させない float unit = widthRederence ? Screen.width : Screen.height; Width /= unit; center /= unit; } if (OnPinchStart != null) { OnPinchStart.Invoke(Width, center); //開始時は必ずDelta=0,Ratio=1となる } } } else if (IsPinching) { //既に認識されているときのみ 3本から2本になった場合無効 float endDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); Width = endDistance; Delta = endDistance - oldDistance; //直前との差分 Ratio = endDistance / startDinstance; //開始時のピンチ幅(px距離)を基準にした倍率になる oldDistance = endDistance; if (isNormalized) { float unit = widthRederence ? Screen.width : Screen.height; Width /= unit; //画面幅で正規化すれば解像度に依存しなくなる Delta /= unit; } if (OnPinch != null) { OnPinch.Invoke(Width, Delta, Ratio); } } } else { IsPinching = false; } }