Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unity. how can I get Touch by fingerID?

We have the index of the touches and method Input.GetTouch(int index) which returns Touch. Also we have fingerID property of Touch structure. How can we get a Touch by fingerID?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For getting a specific touch by ID you have to search for it in the existing touches.

private static bool TryGetTouch(int fingerID, out Touch touch)
{
    foreach(var t in Input.touches)
    {
        if(t.fingerID == fingerID)
        {
            touch = t;
            return true;
        }
    }

    // No touch with given ID exists
   touch = default;
   return false;
}

You can also use linq and do e.g.

using System.Linq;

...

private static bool TryGetTouch(int fingerID, out Touch touch)
{
    try
    {
        touch = Input.touches.First(t => t.fingerID == fingerID);
        return true;
    }
    catch
    {
       // No touch with given ID exists
       touch = default;
       return false;
    }
}

and use it like

if(TryGetTouch (someFingerID, out var touch))
{
    // use touch here
}
else
{
    // probably use a new touch and store its fingerID 
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading