You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
11 Hrs
31 Min
46 Seg
Curso de Herramientas de Unity

Curso de Herramientas de Unity

Hederson Carrasquero

Hederson Carrasquero

Controles: creando un bot贸n

5/20
Resources

How do the controls work in Unity?

Getting into the world of Unity development can be exciting and challenging at the same time. One of the most important aspects to master is the use of controls. These are fundamental for drawing and creating interactive interfaces. There are several types that can be used, such as buttons, sliders and text boxes, and the way you position them can vary. Learning how to use these controls correctly will allow you to create more effective and fluid user experiences.

What is a Rect in Unity?

Unity uses a system of rectangles, known as a Rect, to define the position and size of on-screen controls. Unlike traditional systems where the origin (0,0) is in the lower left corner, in Unity the origin is in the upper left corner.

  • X: Advances to the right.
  • Y: Increments downward.

This system measures distances in pixels, which may require some manual adjustments to correctly position your elements on screen. Fortunately, Unity offers several useful features for working with Rects, such as locating the center and detecting collisions with other rectangles, making the design much more intuitive.

What types of user interfaces are there?

When working in Unity, you will face two main types of interfaces when developing GUIs:

  1. Manual GUI: This type of interface requires you to manually define the position, size, height and width of each element. Although it offers more control over the design, it also requires more time and effort.

  2. GUI Layout: Unlike the manual GUI, the GUI layout is an automatic system that places the controls for you. It is a more efficient way to work, but at the cost of having less control over the exact layout.

Both systems have their pros and cons. While manual intervention gives you more precision, the automatic system speeds up the design process and allows for faster changes.

How to create a button in Unity?

Creating a button in Unity is a great way to start understanding the implementation of GUI controls. Here's how to do it:

using UnityEngine;
public class GUIExamples : MonoBehaviour{ void OnGUI() { // Define the position and size of the Rect for the button Rect buttonRect = new Rect(0, 0, 100, 50);
 // Create and display the button if (GUI.Button(buttonRect, "Press")) { Debug.Log("Button pressed"); } } }}
  • Rect: The Rect defines the area of the button with its coordinates (X, Y) and its size (width and height).
  • GUI.Button: This method creates the button and returns a boolean. When the button is pressed, it returns true, triggering any action within its if block.
  • Debug.Log: Useful to check if the button is working, it will print a message when pressed.

What is a GUIContent and how is it used?

The GUIContent in Unity allows you to define the text and image to be displayed in a control. You can, for example, add an image and text at the same time, maximizing the visual richness of your interface.

How do I give functionality to a button in Unity?

To give functionality to a button, you must use the boolean value returned by the GUI.Button method each time it is pressed:

// Inside the OnGUI() methodif (GUI.Button(new Rect(0, 0, 100, 50), "Hello Platzi")){ Debug.Log("Button pressed");}

This simple structure allows you to implement different actions that will be executed when the button is pressed, such as restarting a game, opening a new menu or updating information on the screen.

Don't forget that practice is essential to understand how to design and program effective interfaces in Unity. Experiment with different types of controls and systems to find the one that best suits your needs, keep learning and improving your game development skills!

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?