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:

1 Días
10 Hrs
18 Min
2 Seg

Implementación de la pantalla de configuración

28/29
Resources

How to add a new state for the options screen in Unity?

To get closer to implementing an options screen in our game, we need to start with the creation of a new state in our GameManager. This new state will be crucial to manage the proper transition to the options screen.

  1. Adding a new state:
    • In the GameManager file, we include a new GameState called Options.
    • We incorporate a public function to allow switching to this new state with public void Options(), setting the game state to GameState.Options.
    • We call the OnGameStateUpdated() function to update the game state.
public enum GameState { Idle, Options, // other states}
public void Options() { gameState = GameState.Options; OnGameStateUpdated();}
  1. Switch on the home screen button:
    • We activate all user interfaces.
    • We modify the UIStartScreen script to add a new optionsButtonClick function, which calls gameManager.instance.Options().
public void OptionsButtonClick() { gameManager.instance.Options();}
  1. Setting the button's OnClick:
    • In Unity, we set the Options button so that its OnClick event is bound to optionsButtonClick.

How to implement audio controls in the options screen?

Once the options state is integrated, it is time to configure the audio controls on the screen. This will allow users to set the volume of music and sound effects.

Configuring the elements in Unity

  1. Add UIScreen component:

    • In the UIOptions screen, a UIScreen component is added to control visibility in the Options state.
  2. Adjust screen layout:

    • Group elements correctly and adjust the size and spacing of volume controls.

Code to control volume

  1. Create UIOptions script:
    • This script will handle the values of the audio controls, using integer variables for volume and sound effects (SFX).
public int volume;public int sfx;public TextMeshProUGUI volumeLabel;public TextMeshProUGUI sfxLabel;
public void AddVolume() {  volume++; UpdateVolumeLabel();}
public void MinusVolume() {  volume--; UpdateVolumeLabel();}
  1. Initialize and update labels:

    • We start with default values (e.g., volume of 10) and update the labels with the Add and Minus functions.
  2. Limit numeric values:

    • We use Mathf.Clamp to limit values between 0 and 10 in all relevant functions when adjusting volume and sound effects.
volume = Mathf.Clamp(volume, 0, 10);
  1. Set OnClick events:
    • Bind each specific button to increase or decrease Volume and SFX to the corresponding functions in UIOptions.

How to connect the UI to the actual audio logic?

Although we have created a screen that allows you to see volume adjustments, you still need to connect these controls to the internal audio logic. Here are some considerations:

  • Link to AudioManager: Look for ways to integrate UIOptions with the AudioManager class to realistically effect volume changes.
  • Explore audio APIs: Use Unity's tools to manipulate audio levels in your projects.

Community Invitation

We want to hear from you! Can you think of a way to connect UIOptions to the AudioManager class to actually manage volume? Share your solution in the comments and let's help others learn too.

Contributions 1

Questions 0

Sort by:

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

Para añadir el funcionamiento a los botones de audio hice lo siguiente * Primero añadi una referencia al AudioSource del objeto MusicSource desde el script de AudioManager y luego desde el inspector, ya que solo teniamos la referencia al sfxSource `public AudioSource MusicSource` * Luego en el awake del AudioManager estableci el volumen por defecto de los dos AudioSource a 0.5f (para que al iniciar el juego no se escuche ni muy duro ni muy pasito) `MusicSource.volume = 0.5f; ` `SfxSource.volume = 0.5f;` Y tambien cambie a 5 el valor inicial de volume y sfx en el script de UIOptions para que la UI tambien iniciara en 5 * Y luego en las 4 funciones del script UIOptions agregue una linea que por medio de la instancia del AudioManager accede a los AudioSource de los objetos hijos y establece el volumen del audio igual a la variable que indica que numero escribir (volume o sfx) dividida 10f para que asi nos de un valor entre 0 y 1, aqui como quedaria el script. `public void Addvolume()` * `{` * ` volume++;` * ` volume = Mathf.Clamp(volume, 0, 10);` * ` volumeLabel.text = volume.ToString();` * ` AudioManager.Instance.MusicSource.volume = volume/10f;` * `}` * * `public void Minusvolume()` * `{` * ` volume--;` * ` volume = Mathf.Clamp(volume, 0, 10);` * ` volumeLabel.text = volume.ToString();` * ` AudioManager.Instance.MusicSource.volume = volume /10f;` * `}` * * `public void Addvsfx()` * `{` * ` sfx++;` * ` sfx = Mathf.Clamp(sfx, 0, 10);` * ` sfxLabel.text = sfx.ToString();` * ` AudioManager.Instance.SfxSource.volume = sfx/10f;` * `}` * * `public void Minussfx()` * `{` * ` sfx--;` * ` sfx = Mathf.Clamp(sfx, 0, 10);` * ` sfxLabel.text = sfx.ToString();` * ` AudioManager.Instance.SfxSource.volume = sfx/10f;` * `}` Nota: recordar asignar en el inspector la referencia al MusicSource en el objeto AudioManager