Search Unity

Android Touch Game Menu

Discussion in 'General Discussion' started by AM_entertainmet, Sep 17, 2014.

  1. AM_entertainmet

    AM_entertainmet

    Joined:
    Aug 18, 2014
    Posts:
    3
    Hey
    I've made a 2d game but it starts directly when I open it on android
    I want it to show a menu screen that shows
    Play and Exit Buttons with the logo over them
    I've searched alot but I found scripts that only works on PC and never worked on android touch screens


    Help pleaase :)
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Use Input.GetTouch. That'll tell you where on the screen the user has touched. If that is inside a GUITexture then the button has been pressed.
     
  3. AM_entertainmet

    AM_entertainmet

    Joined:
    Aug 18, 2014
    Posts:
    3
    how to use touch here ?

    if(GUI.Button(Rect(55, 100, 180, 40), "Start game")) {
    var script = GetComponent("MainMenuScript");
    script.enabled = false;

    }
     
  4. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I'm not an Android expert, but your code "just works" on an iOS device. What happens/doesn't happen on your Android?
     
  5. Kawaburd

    Kawaburd

    Joined:
    Jul 22, 2013
    Posts:
    16
    Tiny slice of how I do it:
    Code (CSharp):
    1. if (MOBILE == false) { hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero); }
    2.         else                { hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.GetTouch(0).position), Vector2.zero);}
    3.  
    4.         if (hit.collider) { // THIS is how you null-check a raycast's collider!
    5.  
    6. //Do stuff.
    7.  
    8. }
    How it works: I heard OnGUI calls are horrible for a mobile game's performance. The info may be inaccurate or outdated, but just in case, I made my buttons sprites with colliders instead.

    Raycasting to a worldpoint like that will check anything on the camera that gets clicked or touched, provided it has a collider on it. (If you're going mobile only, GetTouch(0)'s what you're gonna want.) I don't have an iOS device to test this on, but it works like a charm on Android.
     
  6. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    Well I'm pretty sure Input.GetMouseButtonDown(0) which checks for left mouse click, works with mobile as well, as the first touch.

    I don't recommend using it if you're going to use multitouch later on, because it does some weird things (it uses the average position between all touches I think). But for single touch it works well.

    But regarding Input.GetTouch functionality... you should also need to know about Input.touchCount, which checks the amount of touches currently pressed on the screen... it's zero when you're not touching the screen, and it's 1 or more, depending how many fingers you're pressing on the screen.

    So what you do is make a for loop to retrieve all touches.

    for (var i = 0; i < Input.touchCount; ++i) {
    var thisTouch : Touch = Input.GetTouch(i);
    }


    So in that for loop you can access what's going on with the screen touches, by retrieving the info within the Touch variable.
    If you want to know, for example, the position of the touch, you access it within the Touch variables.

    for (var i = 0; i < Input.touchCount; ++i) {
    var thisTouch : Touch = Input.GetTouch(i);
    var touchPosition : Vector2 = thisTouch.position;
    }


    The touch position was stored in the variable touchPosition, and you can use it to do a Camera.mainScreenToWorldPoint, as Kawaburd said.
     
    Kawaburd likes this.
  7. AM_entertainmet

    AM_entertainmet

    Joined:
    Aug 18, 2014
    Posts:
    3
    Thanks All for Help :D

    Am not actually that experienced person in those things so can someone give me a clear tutorial please??
    And the last Gui button I used worked with pc mouse but didn't even show on Android like it starts the game directly
     
  8. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    Those examples are as simple as they nearly can be. I suggest you start by visiting the top bar Learn section and continue with scripting tutorials. When you master them you can adapt the code in your own needs.