Hi everyone,
I just started playing around with the new GUI features in 4.6 beta and i am having a hard time right now trying to figure out how to check if the user touches a GUI element (in my case an image).
I have an InputManager class that some GameObject use to check if a certain action is triggered. Inside the InputManager class i determine whether it’s a mobile device and and check for touches instead of keys if it is.
The class structure looks like this:
public class InputManager : MonoBehaviour {
public Image jumpImage;
private bool isTouchEnabled = false;
void Start () {
isTouchEnabled = Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer;
if (!isTouchEnabled)
jumpImage.enabled = false;
}
public bool GetJumpButtonDown() {
if (isTouchEnabled) {
if (Input.touchCount < 1)
return false;
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began && /* touch position is inside jumpImage rect */)
return true;
}
} else
return Input.GetButtonDown("Jump");
return false;
}
}
I tried several things like
jumpImage.rectTransform.rect.Contains(Camera.main.ScreenToWorldPoint(touch.position))
but they all failed, because they don’t seem to return the world coordinates (or screen coordinates) of the image.
When it comes to Unity, i am pretty much a noob and any tip or advice would be appreciated. Thanks!
Hi, have you tried download the UI example for 4.6, it contains lots of examples to learn this new system: http://forum.unity3d.com/threads/ui-example-project.263418/#post-1787210
– _dns