- Home /
Question by
$$anonymous$$ · Oct 11, 2014 at 07:40 AM ·
touchunity 4.6recthittest
Unity 4.6 UI Touch Detection
Hey, is there a way in Unity 4.6 how to detect whether Touch position is inside UI Rect like this?:
if (GUITexture.HitTest(Touch.Position)) return true;
I know there's a RectTransform Component but due to Anchors, it's coordinates arent in pixels from bottom-left corner. So I Tried something like this:
RectTransform SRTrans = this.GetComponent<RectTransform>();
this.TouchAreaRect = new Rect(
this.transform.position.x - SRTrans.rect.width / 2,
this.transform.position.y - SRTrans.rect.height / 2,
this.transform.position.x + SRTrans.rect.width / 2,
this.transform.position.y + SRTrans.rect.height / 2
);
This code is working for one of my UI Images, but it's not working properly for other ones so is there a better solution like GUI Texture had a HitTest Method?
Comment
Best Answer
Answer by $$anonymous$$ · Oct 11, 2014 at 08:18 AM
Well, I found out that I made mistake in defining new Rect of course which should be like this:
this.TouchAreaRect = new Rect(
this.transform.position.x - SRTrans.rect.width / 2,
this.transform.position.y - SRTrans.rect.height / 2,
SRTrans.rect.width,
SRTrans.rect.height
);
I had problems with width & height, I dont know why I was trying to add position to it as well...