How To Add Button Controls In Unity
Unity - The Push
In this chapter, we volition earn how to insert UI elements into our scene and go about working with them.
Let united states of america start off with a Button. To insert a push button, right click in the Scene Hierarchy and go to Create → UI → Push button. If you do not have an existing Canvas and an EventSystem, Unity will automatically create one for you, and place the button within the Canvas equally well.
Remember that in Overlay rendering mode, which is the default mode, the size of the Canvass is independent of the size of the camera. You tin can exam this past clicking on the Game tab.
If you lot play the scene, you will observe the push already has some standard functionality such equally detecting when the mouse is hovering over it, and changing color when pressed.
A Button requires functionality to be really useful in the UI. This functionality tin can be added through its properties.
Let us create a new script, and call information technology ButtonBehaviour.
public class ButtonBehaviour : MonoBehaviour { int northward; public void OnButtonPress(){ northward++; Debug.Log("Push button clicked " + northward + " times."); } }
We accept fabricated a simple method that logs how many times we have hitting the push button.
Note − This method has to be public; information technology will not exist noticed by the Push'due south functionality otherwise.
Let us create an empty GameObject and attach this script to information technology. We practise this because a button will non do anything on its own; information technology but calls the specified method in its scripting.
Now, go into the Button's properties, and discover the OnClick() property.
Hit the + icon on the lesser tab, and a new entry should show upwards in the list.
This entry defines what object the button press acts on, and what role of that object'due south script is chosen. Because of the event organization used in the push button press, you can trigger multiple functions simply by adding them to the list.
Elevate and drop the empty GameObject, which contains the ButtonManager script nosotros created, onto the None (Object) slot.
Navigate the No Function dropdown listing, and look for our OnButtonPress method. (Remember that information technology can be named anything yous desire, OnButtonPress is simply a standardized naming convention.) Y'all should find it in the ButtonBehaviour section.
If you play the game now, you tin test the push and surely enough, the panel prints out how many times y'all have pressed the button.
How To Add Button Controls In Unity,
Source: https://www.tutorialspoint.com/unity/unity_the_button.htm
Posted by: cardonaformiscrad.blogspot.com
0 Response to "How To Add Button Controls In Unity"
Post a Comment