FingerManager

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(update deprecated code)
(fix formatting)
 
Line 1: Line 1:
 
[[Category:Code Examples]]
 
[[Category:Code Examples]]
 
[[Category:Input]]
 
[[Category:Input]]
 
== Description ==
 
  
 
I created this script to handle multiple touches, moves, ends and cancels.
 
I created this script to handle multiple touches, moves, ends and cancels.
Line 118: Line 116:
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
void FingerBegin (Touch event) {}
+
void FingerBegin (Touch touch) {}
  
void FingerMove (Touch event) {}
+
void FingerMove (Touch touch) {}
  
void FingerEnd (Touch event) {}
+
void FingerEnd (Touch touch) {}
  
void FingerCancel (Touch event) {}
+
void FingerCancel (Touch touch) {}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 00:30, 19 October 2020


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

[edit] 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;
                    }
                }
            }
        }
    }
}

[edit] 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 touch) {}
 
void FingerMove (Touch touch) {}
 
void FingerEnd (Touch touch) {}
 
void FingerCancel (Touch touch) {}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox