วันศุกร์ที่ 21 มีนาคม พ.ศ. 2557

Unity: Multi-Touch 2D Collider Work ^o^

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TouchInput : MonoBehaviour {

    private List<GameObjecttouchList = new List<GameObject>();
    private GameObject[] touchesOld;
    private RaycastHit2D hit;

    void Update () {

#if UNITY_EDITOR
        if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
            
            touchesOld = new GameObject[touchList.Count];
            touchList.CopyTo(touchesOld);
            touchList.Clear();

            hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
        
            if(hit.collider != null) {

                GameObject recipient = hit.transform.gameObject;
                touchList.Add(recipient);
                
                if (Input.GetMouseButtonDown(0)) {
                    recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
                }
                if (Input.GetMouseButtonUp(0)) {
                    recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
                }
                if (Input.GetMouseButton(0)) {
                    recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
                }
                
            }
            
            foreach (GameObject g in touchesOld) {
                if (!touchList.Contains(g)) {
                    g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
                }
            }
        }
#endif

        if (Input.touchCount > 0) {

            touchesOld = new GameObject[touchList.Count];
            touchList.CopyTo(touchesOld);
            touchList.Clear();

            foreach (Touch touch in Input.touches) {
                
                hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero);

                if(hit.collider != null) {
                        
                    GameObject recipient = hit.transform.gameObject;
                    touchList.Add(recipient);
                    
                    if (touch.phase == TouchPhase.Began) {
                        recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
                    }
                    if (touch.phase == TouchPhase.Ended) {
                        recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
                    }
                    if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
                        recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
                    }
                    if (touch.phase == TouchPhase.Canceled) {
                        recipient.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
                    }

                }
            }

            foreach (GameObject g in touchesOld) {
                if (!touchList.Contains(g)) {
                    g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
                }
            }
        }
    }
}