- Home /
Problem with android on screen GUI
Hello to everyone. I'm trying to create an onscreen control gui with a dpad and two buttons with the following code:
public class GUIButtons : MonoBehaviour {
private Rect DPadRect;
private Rect RedButtonRect;
private Rect BlueButtonRect;
private Triangle ArrowUp;
private Triangle ArrowDown;
private Triangle ArrowLeft;
private Triangle ArrowRight;
private bool inputEnabled = true;
public Texture DPad;
public Texture RedButton;
public Texture BlueButton;
void Start () {
// Initializes size of gui items
DPadRect = new Rect(Screen.width/10,2*Screen.height/3 - Screen.height/8,Screen.width/5,Screen.width/5);
RedButtonRect = new Rect(2 * Screen.width / 3, 2 * Screen.height / 3 - Screen.height/10 , Screen.width / 8, Screen.width / 8);
BlueButtonRect = new Rect(2 * Screen.width / 3 + Screen.width / 9, 2 * Screen.height / 3 - Screen.height / 10 + Screen.height / 9, Screen.width / 10, Screen.width / 10);
ArrowUp = new Triangle(DPadRect.center, new Vector2(DPadRect.xMin, DPadRect.yMin), new Vector2(DPadRect.xMax, DPadRect.yMin));
ArrowDown = new Triangle(DPadRect.center, new Vector2(DPadRect.xMin, DPadRect.yMax), new Vector2(DPadRect.xMax, DPadRect.yMax));
ArrowRight = new Triangle(DPadRect.center, new Vector2(DPadRect.xMax, DPadRect.yMin), new Vector2(DPadRect.xMax, DPadRect.yMax));
ArrowLeft = new Triangle(DPadRect.center, new Vector2(DPadRect.xMin, DPadRect.yMin),new Vector2(DPadRect.xMin, DPadRect.yMax));
}
void OnGUI()
{
if (inputEnabled)
{
GUI.DrawTexture(DPadRect, DPad);
GUI.DrawTexture(RedButtonRect, RedButton);
GUI.DrawTexture(BlueButtonRect, BlueButton);
}
}
void Update()
{
if (inputEnabled)
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began && RedButtonRect.Contains(touch.position))
{
Debug.Log("Red Button Pressed");
}
else if (touch.phase == TouchPhase.Began && BlueButtonRect.Contains(touch.position))
{
Debug.Log("Blue button pressed");
}
else if ( (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Stationary) && DPadRect.Contains(touch.position))
{
Debug.Log("DPad touched");
if (ArrowUp.Contains(touch.position))
{
Debug.Log("Up arrow clicked");
}
else if (ArrowDown.Contains(touch.position))
{
Debug.Log("Down arrow clicked");
}
else if (ArrowRight.Contains(touch.position))
{
Debug.Log("Right arrow clicked");
}
else if (ArrowLeft.Contains(touch.position))
{
Debug.Log("Left arrow clicked");
}
}
}
}
}
public void EnableInput()
{
inputEnabled = true;
}
public void DisableInput()
{
inputEnabled = false;
}
}
The problem is that, using android studio's adb logcat, pressing the buttons does not generate any of the log messages. Moreover, it loops on the following lines:
03-17 17:20:30.665 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.691 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.712 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.727 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.744 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.763 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.804 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.805 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.829 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.847 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.864 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.884 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.903 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.946 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.948 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.970 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:30.992 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.005 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.023 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.043 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.083 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.084 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.110 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.132 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.145 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
03-17 17:20:31.178 17478-17499/com.company.productname D/Unity﹕ Unknown event structure (0)
Any help would be appreciated. Thanks in advance.
Comment
Answer by ukk · Mar 18, 2015 at 05:37 PM
The problem was that the GUI has a different coordinates system than the normal content, I had to add the following lines:
Vector2 touchPosition = touch.position;
touchPosition.y = Screen.height - touchPosition.y;
and use touchPosition instead of touch.position. Everything works fine now.