Skip to content

Instantly share code, notes, and snippets.

@kazuooooo
Created March 6, 2015 09:23
Show Gist options
  • Save kazuooooo/001f610759e70e0a2338 to your computer and use it in GitHub Desktop.
Save kazuooooo/001f610759e70e0a2338 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SwipeDetector : MonoBehaviour
{
public float minSwipeDistY;
public float minSwipeDistX;
private Vector2 startPos;
private GameObject text;
private GameObject text2;
public GameObject GUITEXT;
private bool FLG;
void Update ()
{
text = GameObject.Find ("Text") as GameObject;
text2 = GameObject.Find ("Text2") as GameObject;
// if (Input.GetKeyDown (KeyCode.A)) {
// text.GetComponent<Text> ().text = "TEST";
// }
//#if UNITY_ANDROID
Debug.Log ("ON2");
//タッチされたら
if (Input.touchCount > 0) {
//タッチを取得
Touch touch = Input.touches [0];
//タッチフェーズによって場合分け
switch (touch.phase) {
//タッチ開始時
case TouchPhase.Began:
//タッチのポジションを取得してstartPosに代入
startPos = touch.position;
break;
//タッチ終了時
case TouchPhase.Ended:
float swipeYValue;
float swipeXValue;
//タッチ終了のyポジションからタッチ開始のyポジションを引く
float swipeDistVertical = (new Vector3 (0, touch.position.y, 0) - new Vector3 (0, startPos.y, 0)).magnitude;
text.GetComponent<Text> ().text = "Y" + swipeDistVertical.ToString ();
//差分が最低スワイプ分を超えていた場合
if (swipeDistVertical > minSwipeDistY) {
//y座標の差分のサインを計算
//yの差分をとっているので絶対にサインの値は1(90度)か-1(270度)
swipeYValue = Mathf.Sign (touch.position.y - startPos.y);
if (swipeYValue > 0) {
//sin = 1
GUITEXT.GetComponent<GUIText> ().text = "UP" + "swipeY is" + swipeYValue.ToString ();
} else if (swipeYValue < 0) {
//sin = -1
GUITEXT.GetComponent<GUIText> ().text = "DOWN" + "swipeY is" + swipeYValue.ToString ();
}
}
//タッチ終了のyポジションからタッチ開始のxポジションを引く
float swipeDistHorizontal = (new Vector3 (touch.position.x, 0, 0) - new Vector3 (startPos.x, 0, 0)).magnitude;
text2.GetComponent<Text> ().text = "X" + swipeDistHorizontal.ToString ();
if (swipeDistHorizontal > minSwipeDistX) {
//x座標の差分のサインを計算
//xの差分をとっているので絶対にサインの値は1(90度)か-1(270度)
swipeXValue = Mathf.Sign (touch.position.x - startPos.x);
if (swipeXValue > 0) {
GUITEXT.GetComponent<GUIText> ().text = "RIGHT" + "swipeX is" + swipeXValue.ToString ();
} else if (swipeXValue < 0) {//left swipe
GUITEXT.GetComponent<GUIText> ().text = "LEFT" + "swipeX is" + swipeXValue.ToString ();
}
}
break;
}
}
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment