The question is answered, right answer was accepted
Touchscreen Start Button
Hello,
I have a menu for my 2d android game now, and I have added a UI button to it. Now on this button, i have this simple script attached on it:
void OnMouseEnter()
{
Application.LoadLevel("Krolsch");
}
I can touch the button, but nothing happens, it doesn't load the new scene.
I have heard that you can use a OnMouseEnter or OnMouseDown, and that should work as a touch?
Bram
Answer by freemann098 · Oct 14, 2015 at 08:37 PM
Like said above just use the OnClick event in the inspector. Here's what I do for UI.
Make a new script for managing button clicks.
Attach this script to some Gameobject in your scene.
In the script, make a new public function for what you want your button to do...for example
public void playButton() { Application.LoadLevel("Krolsch"); }
.Now click on your button in the hierarchy and find the little area in the inspector that says "On Click () "
Click the little plus symbol
This will add somthing with 2 drop down menus and an input thing that says "None (Object)". Drag the game object that has your button script on it into that field that says "None (Object)".
This will let you reference this object. Click on the drop-down menu that says "No Function" and it will show some options. Click on the one that is the same name as the script you made earlier and then click on the function name. This will make the button run this function.
For example, my script is called ButtonManager and my function was called playButton
Thanks @freemann098 and @DiegoSLTS,
The public void is what I needed to know.
Answer by DiegoSLTS · Oct 14, 2015 at 07:01 PM
By "UI" you mean uGUI or the legacy GUI?
In uGUI, for a UI element to react to touch events you can do various things:
Use the Button "OnClick" event in the inspector, setup the function that should be called. Or...
Implement the IPointerDownHandler interface in a script added to the button. This forces you to implement the OnPointerDown function.
Use an EventTrigger component instead of the OnClick event of the button, and add and setup the PointerDown event in the inspector.
I'm not sure if OnMouseEnter should work for the UI, but if it should, I guess OnMouseDown would make a lot more sense.
Also, UI elements need an EventSystem game object in the scene to react to events. Make sure it's not missing in your scene.
I don't really understand the IPointerDownHandler, so I am trying the others. But what do I need to add on the "OnClick" event on the button and on the EventTrigger.
I have added my button as a object on it, then I need to add a function to that, what function does it need to be? Because I can't add my On$$anonymous$$ouseDown function to it.
And by the way, what I did:
at the Hierarchy, Create > UI > Button
I just started this so I don't really know the means of UI, uGUI or GUI
That's uGUI.
UI means User Interface. GUI means Graphic User Interface.
UI and GUI are generic names for this, they're not related to the technology used.
uGUI means "Uinty GUI", it's the UI system implemented in Unity since version 4.6, it works by adding UI objects in a Canvas on the scene. "legacy" GUI (or OnGUI) is the old system used by Unity. Is pretty ugly and hard to use, and you had to do anything in code (inside the OnGUI method). It's still used for editor scripts, but for GUI that's part of the game is a really bad idea to keep using it.
The function you wrote doesn't show in the list because it's private. This video explains everything you need to know to correctly setup a button in uGUI: http://unity3d.com/es/learn/tutorials/modules/beginner/ui/ui-button
This should be enough, but if you need something else, this other video explains the basics about EventTriggers: http://unity3d.com/es/learn/tutorials/modules/beginner/ui/ui-events-and-event-triggers?playlist=17111
The IPointerDownHandler may seem to complicated if you just started, ignore it unless you need it. This methods gives you more information of the event, like the exact screen coordinate of the pointer.
Follow this Question
Related Questions
Unity 2d android game How to "freeze" position on Y axis 1 Answer
Android UI push Multiple buttons Help 0 Answers
Making buttons for touch devices 0 Answers
High memory usage 2d game 1 Answer