Created
March 23, 2015 02:11
-
-
Save kazuooooo/319b9216985a743df4ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| using UnityEngine.UI; | |
| public class PanelRoot : MonoBehaviour | |
| { | |
| private GameObject touchingObj1; | |
| private GameObject touchedObj1; | |
| private int touchNumber1; | |
| private int calcNumber1; | |
| private GameObject touchingObj2; | |
| private GameObject touchedObj2; | |
| private int touchNumber2; | |
| private int calcNumber2; | |
| private int untouchNumber1; | |
| private int untouchNumber2; | |
| public Text debugText; | |
| public Text debugText2; | |
| public Text debugText3; | |
| public Text debugText4; | |
| public Text debugText5; | |
| private int actionStateNum; | |
| private bool OneTimeFLG = false; | |
| private GameObject[] Panels; | |
| //private bool touch1EndFLG = false; | |
| // Use this for initialization | |
| void Start () | |
| { | |
| Panels = GameObject.FindGameObjectsWithTag ("Panel"); | |
| } | |
| // Update is called once per frame | |
| void Update () | |
| { | |
| if (Input.touchCount == 2) { | |
| // //Touch1を取得 | |
| Touch touch1 = Input.GetTouch (0); | |
| //タッチされているオブジェクトを取得 | |
| touchingObj1 = GetTouchObj (touch1); | |
| //タッチされているオブジェクトの番号を取得 | |
| if (touchingObj1 != null) { | |
| touchNumber1 = GetNumber (touchingObj1); | |
| } | |
| //タッチされた数字から計算 | |
| if (touch1.phase == TouchPhase.Began && touchingObj1 != null) { | |
| touchNumber1 = GetNumber (touchingObj1); | |
| debugText.text = touchNumber1.ToString (); | |
| calcNumber1 = calcNumber (touchNumber1); | |
| touchedObj1 = GetTouchObj (touch1); | |
| debugText2.text = calcNumber1.ToString (); | |
| } | |
| // debugText.text = touchNumber1.ToString (); | |
| // debugText2.text = calcNumber1.ToString (); | |
| //Touch2を取得 | |
| Touch touch2 = Input.GetTouch (1); | |
| //タッチされているオブジェクトを取得 | |
| touchingObj2 = GetTouchObj (touch2); | |
| //タッチされているオブジェクトの番号を取得 | |
| if (touchingObj2 != null) { | |
| touchNumber2 = GetNumber (touchingObj2); | |
| debugText3.text = touchNumber2.ToString (); | |
| } | |
| //タッチされた数字から計算 | |
| if (touch2.phase == TouchPhase.Began && touchingObj2 != null) { | |
| touchNumber2 = GetNumber (touchingObj2); | |
| calcNumber2 = calcNumber (touchNumber2); | |
| touchedObj2 = GetTouchObj (touch2); | |
| debugText4.text = calcNumber2.ToString (); | |
| //touchedObj2.GetComponent<Image> ().color = Color.red; | |
| } | |
| // debugText3.text = touchNumber2.ToString (); | |
| // debugText4.text = calcNumber2.ToString (); | |
| if (!OneTimeFLG) { | |
| actionStateNum = CheckAction (touchedObj1, touchedObj2); | |
| if (actionStateNum == 0) { | |
| OneTimeFLG = false; | |
| } | |
| debugText5.text = actionStateNum.ToString (); | |
| OneTimeFLG = true; | |
| } | |
| if ((touch1.phase == TouchPhase.Ended)) { | |
| //←→の計算 | |
| if (actionStateNum == 1) { | |
| if (touchedObj1 != null && touchingObj1 != null && touchingObj2 != null && touchedObj1 != touchingObj1 && touchedObj1 != touchingObj2 && touchingObj1 != touchingObj2) { | |
| //真ん中の数字から引く | |
| int beforeCalcNumberCenter = GetNumber (touchedObj1); | |
| int afterCalcNumberCenter = beforeCalcNumberCenter - calcNumber1 - calcNumber2; | |
| writeNumber (touchedObj1, afterCalcNumberCenter); | |
| // | |
| // debugText.text = touchedObj1.gameObject.name; | |
| // debugText2.text = touchedObj2.gameObject.name; | |
| // | |
| //両端の数字に足す | |
| int beforeCalcNumberEdge1 = GetNumber (touchingObj1); | |
| int afterCalcNumberEdge1 = beforeCalcNumberEdge1 + calcNumber1; | |
| //debugText2.text = "A" + afterCalcNumberEdge1.ToString (); | |
| writeNumber (touchingObj1, afterCalcNumberEdge1); | |
| int beforeCalcNumberEdge2 = GetNumber (touchingObj2); | |
| int afterCalcNumberEdge2 = beforeCalcNumberEdge2 + calcNumber2; | |
| //debugText4.text = "B" + afterCalcNumberEdge2.ToString (); | |
| writeNumber (touchingObj2, afterCalcNumberEdge2); | |
| //OneTimeFLG = false; | |
| } | |
| } | |
| //→←の計算 | |
| else if (actionStateNum == 2) { | |
| if (touchingObj1 != null && touchedObj1 != null && touchedObj2 != null && touchingObj1 != touchedObj1 && touchingObj1 != touchedObj2) { | |
| //両端の数字を真ん中に足す | |
| int beforeCalcNumberCenter = GetNumber (touchingObj1); | |
| int afterCalcNumberCenter = beforeCalcNumberCenter + calcNumber1 + calcNumber2; | |
| writeNumber (touchingObj1, afterCalcNumberCenter); | |
| // debugText.text = touchedObj1.gameObject.name; | |
| // debugText2.text = touchedObj2.gameObject.name; | |
| //両端の数字から引く | |
| int beforeCalcNumberEdge1 = GetNumber (touchedObj1); | |
| int afterCalcNumberEdge1 = beforeCalcNumberEdge1 - calcNumber1; | |
| // debugText2.text = "C" + afterCalcNumberEdge1.ToString (); | |
| writeNumber (touchedObj1, afterCalcNumberEdge1); | |
| int beforeCalcNumberEdge2 = GetNumber (touchedObj2); | |
| int afterCalcNumberEdge2 = beforeCalcNumberEdge2 - calcNumber2; | |
| // debugText5.text = "c2=" + calcNumber2.ToString (); | |
| // debugText4.text = "D" + afterCalcNumberEdge2.ToString (); | |
| writeNumber (touchedObj2, afterCalcNumberEdge2); | |
| //OneTimeFLG = false; | |
| } | |
| } | |
| foreach (GameObject panel in Panels) { | |
| panel.GetComponent<Image> ().color = Color.white; | |
| } | |
| touchNumber1 = 0; | |
| touchNumber2 = 0; | |
| calcNumber1 = 0; | |
| calcNumber2 = 0; | |
| OneTimeFLG = false; | |
| } | |
| if (touchingObj1 != null && touchingObj2 != null) { | |
| if (touchingObj1 == touchingObj2) { | |
| touchingObj1.GetComponent<Image> ().color = Color.red; | |
| } | |
| if (touchingObj1 != touchingObj2) { | |
| touchingObj1.GetComponent<Image> ().color = Color.green; | |
| touchingObj2.GetComponent<Image> ().color = Color.green; | |
| } | |
| } | |
| } | |
| } | |
| //タッチしてるところの数字を取得 | |
| public GameObject GetTouchObj (Touch touch) | |
| { | |
| //Touchのポジションを取得(Vector3) | |
| Vector3 wp = touch.position; | |
| //Vector2に変換 | |
| Vector2 touchPos = new Vector2 (wp.x, wp.y); | |
| //そのポジションにコライダー | |
| Collider2D collider = Physics2D.OverlapPoint (touchPos); | |
| //タッチしているポジションにコライダーがあれば | |
| if (collider != null) { | |
| return collider.gameObject; | |
| } | |
| return null; | |
| } | |
| public int GetNumber (GameObject obj) | |
| { | |
| Text text = obj.transform.FindChild ("NumberText").gameObject.GetComponent<Text> (); | |
| int number = int.Parse (text.text); | |
| return number; | |
| } | |
| public int calcNumber (int num) | |
| { | |
| int resultNum = num / 2; | |
| return resultNum; | |
| } | |
| public void writeNumber (GameObject target, int num) | |
| { | |
| Text text = target.transform.FindChild ("NumberText").gameObject.GetComponent<Text> (); | |
| if (num < 0) { | |
| num = 0; | |
| } | |
| text.text = num.ToString (); | |
| } | |
| public int CheckAction (GameObject touch1, GameObject touch2) | |
| { | |
| if (touch1 != null && touch2 != null) { | |
| if (touch1.name == touch2.name) { | |
| //←→ | |
| return 1; | |
| } else { | |
| //→← | |
| return 2; | |
| } | |
| } | |
| return 0; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment