Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnGUI buttons + Touchscreens

Discussion in 'Immediate Mode GUI (IMGUI)' started by Pyronide, Oct 7, 2014.

  1. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    I am trying to create a game for a mobile device that has a touch screen. I am still new to Unity 3D and am currently running version 4.3 if it matters. I am wondering if I can use OnGUI () to make a button and have that button be touchable from the screen. If this is an obvious thing I apologize, but I am still learning things as mentioned. Also, I am coding in Javascript for now until I get become more fluent in C# so please answer in JS. Thanks.
     
  2. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    139
    Yeah, I believe you just need to use something like this:

    function OnGUI(){
    if(GUILayout.Button("Do something"))
    {
    Debug.Log("Doing Something");
    }
    }

    If you're brand new it might be worth downloading version 4.6 and start using the new GUI system as the OnGUI system is not very easy, quick or flexible. There's plenty of videos that Unity have made to teach you the new system too.

    Hope that helps
     
  3. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    I looked through the manual (as Internet is usually not available when I am working in Unity) and I saw the code for GUI buttons. So right now it is something like:

    var btn1 : Texture;

    funcion OnGUI () {
    GUI.Button(Rect(xPos, yPos, xSize, ySize), btn1)) {
    Debug.Log ("A button");
    }
    }

    This is a rough example from what I can remembet. This will be a lot easier when I get internet at home and can copy paste some information for you. Sorry I am unable to do that right now. With this I do have a question, is GUI.Button a touchable button or just on mouse click? If it helps I am making a virtual pet type game.
     
  4. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    139
    If I remember correctly it can do both things. Touching acts as if you are clicking it
     
  5. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    So it should work on mouse clicks when on my computer but when I build and play it should work by touching my moble screen?
     
  6. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    139
    Exactly, make a build and see for yourself
     
  7. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Thank you very much, but I have a new question now... same topic...

    For this virtual pet game I am wanting a button on the screen that says menu and then upon clicking it, more buttons overlap the screen. I cannot seem to figure this out through the manual alone and I don't know if I mentioned it or not, but my Unity computer is not connected to the internet yet/still.

    Thanks in advanced.
     
  8. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    139
    I'll show you what I used to do with the GUI system. It's a bit messy but it worked...

    Code (JavaScript):
    1. enum GUIPanels = {GAME, PAUSE, MAINMENU, <Other screens you want>};
    2.  
    3. var currentPanel : GUIPanels;
    4.  
    5. function OnGUI()
    6. {
    7.       switch(currentPanel)
    8.       {
    9.             case GUIPanels.GAME:
    10.                if(GUILayout.Button("Pause")){
    11.                       currentPanel = GUIPanels.PAUSE; //Swaps to Pause
    12.                }
    13.                break;
    14.                case GUIPanels.PAUSE:
    15.                if(GUILayout.Button("Resume")){
    16.                        currentPanel = GUIPanels.GAME; //Swaps back to displaying Game buttons, etc
    17.                }
    18.                if(GUILayout.Button("Exit to Menu")){
    19.                        currentPanel = GUIPanels.MAINMENU; //Swaps back to displaying Game buttons, etc
    20.                }
    21.                break;
    22.                <Other Panel Cases...>
    23.       }
    24. }
    Give that a try... I think it's all correct JS, but it's been a while.
     
  9. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Thank you very much this will help a lot!!

    I am using textures right now, I hope that will not make a big difference. Just input the texture somewhere?
     
  10. The_BenEvans

    The_BenEvans

    Joined:
    Jul 31, 2012
    Posts:
    139
    That's an easy change, just do this:

    Add your textures as variables:
    Code (JavaScript):
    1. var texture1 : Texture;
    2. var texture2: Texture;
    3. var backButton : Texture;
    Then instead of using a string like "Resume" in GUILayout.Button() just reference a texture, for example:
    Code (CSharp):
    1. if(GUILayout.Button(texture1)){
    2.       DoSomething();
    3. }
     
  11. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    I tried to use your method, but it was a little confusing at first. I looked it up in the manual and learned more about it - which was a great help, thank you! - but I like the way it was setup a little better, and I understand it more. It may not be as nice or clean, but I like how it is looking so far. I wish I would remember to copy some code and bring it with me to show you, but alas I do not have any still.

    As mentioned, I did try with your code and still it would not create a new GUI if applied as a button. If I take it out of the if statement it is there and positioned perfectly, but to get it to instantiate when I click the menu button seems to be giving me some trouble still... Now with that said, I started running my menu stuff through the debug.log and it shows up in the log like I would like, but still not on screen...

    Thanks for the help so far.