FingerManager

From Unify Community Wiki
Revision as of 00:29, 19 October 2020 by Isaiah Kelly (Talk | contribs)

Jump to: navigation, search


Description

I created this script to handle multiple touches, moves, ends and cancels.

FingerManager.cs

// Author: alex@rozgo.com
 
using System.Collections;
using UnityEngine;
 
public class FingerData
{
    public Touch Touch;
    public bool Moved = false;
    public ArrayList Colliders = new ArrayList();
}
 
public class FingerManager : MonoBehaviour
{
    public ArrayList Fingers = new ArrayList();
 
    void FixedUpdate()
    {
        RaycastHit[] hits;
 
        foreach (Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                FingerData finger = new FingerData();
                finger.Touch = touch;
                Ray ray = Camera.main.ScreenPointToRay(touch.position);
                hits = Physics.RaycastAll(ray);
 
                foreach (RaycastHit hit in hits)
                {
                    finger.Colliders.Add(hit.collider);
                    GameObject to = hit.collider.gameObject;
                    to.SendMessage("FingerBegin", touch, SendMessageOptions.DontRequireReceiver);
                }
 
                Fingers.Add(finger);
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                for (int i = 0; i < Fingers.Count; ++i)
                {
                    FingerData finger = (FingerData)Fingers[i];
                    if (finger.Touch.fingerId == touch.fingerId)
                    {
                        finger.Moved = true;
                        foreach (Collider collider in finger.Colliders)
                        {
                            if (collider == null)
                                continue;
 
                            GameObject to = collider.gameObject;
                            to.SendMessage("FingerMove", touch, SendMessageOptions.DontRequireReceiver);
                        }
                    }
                }
            }
            else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
            {
                Ray ray = Camera.main.ScreenPointToRay(touch.position);
                hits = Physics.RaycastAll(ray);
                for (int i = 0; i < Fingers.Count;)
                {
                    FingerData finger = (FingerData)Fingers[i];
                    if (finger.Touch.fingerId == touch.fingerId)
                    {
                        foreach (Collider collider in finger.Colliders)
                        {
                            if (collider == null)
                                continue;
 
                            bool canceled = true;
 
                            foreach (RaycastHit hit in hits)
                            {
                                if (hit.collider == collider)
                                {
                                    canceled = false;
                                    GameObject to = collider.gameObject;
                                    to.SendMessage("FingerEnd", touch, SendMessageOptions.DontRequireReceiver);
                                }
                            }
                            if (canceled)
                            {
                                GameObject to = collider.gameObject;
                                to.SendMessage("FingerCancel", touch, SendMessageOptions.DontRequireReceiver);
                            }
                        }
                        Fingers[i] = Fingers[Fingers.Count - 1];
                        Fingers.RemoveAt(Fingers.Count - 1);
                    }
                    else
                    {
                        ++i;
                    }
                }
            }
        }
    }
}

Usage

Just drop the script on only one master object and listen to FingerBegin, FingerMove, FingerEnd and FingerCancel messages on all objects you want to be interactive. The target objects need a collider.

Write message handlers like this, on each of your interactive objects.

	void FingerBegin (Touch event) {}
 
	void FingerMove (Touch event) {}
 
	void FingerEnd (Touch event) {}
 
	void FingerCancel (Touch event) {}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox