- Home /
Use Rect.Contains to define an area on touchScreens
Hi, I have a very simple question. Can you use Rect.Contains
to reference an area on a touch screen? If so, how would you reference that area? Looking at the code below, what should be in the Input.mousePosition
section of the code?
function Update () {
var rect = Rect (0, 0, 150, 150);
if (rect.Contains(Input.mousePosition))
print("Inside");
}
Thanks for your insight and knowledge on this.
Answer by TrickyHandz · Oct 03, 2013 at 12:54 AM
This will work for you. Checking for the first touch, just check for the zero-index touch input like this:
function Update () {
var rect = Rect (0, 0, 150, 150);
if (rect.Contains(Input.GetTouch(0).position))
print("Inside");
}
You can check for multiple touches as well if you need. This post on the forums has links to several video tutorials on touch: Devin Curry, Touch Input
I'm using C# - and very new at this - so what type/class is "var rect"? I'm trying to use this code, but I'm struggling with defining that variable.
$$anonymous$$any thanks,
@timdan, rect is a Rect. Here's the docs link for you just in case you need it: Unity - Scripting API: Rect