Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Administrador de Sonido | Interfaz y Sonidos de Unity
Unity para Principiantes
course content

Contenido del Curso

Unity para Principiantes

Unity para Principiantes

1. Introducción a Unity
2. Escribe tu Primer Script
3. Física de Unity
4. Interfaz y Sonidos de Unity
5. Pulido y Exportación de tu Juego

book
Administrador de Sonido

Understanding Singleton in Unity

In Unity, a Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to it. This is particularly useful for managing game components that need to persist across different scenes, like a SoundManager.

How Singleton Works in Unity

  1. Single Instance: the SoundManager class declares a public static variable instance of its own type. This variable holds the single instance of the SoundManager class;

  2. Initialization in Start() Method: when the GameObject with this script becomes active, Unity automatically calls the Start() method. Here, the Singleton instance is initialized:

    • If instance is null, it means no SoundManager exists yet, so the current instance (this) is assigned to instance, making it the Singleton;
    • If instance is not null, another SoundManager already exists, which should not happen in a Singleton pattern. In this case, the current GameObject is destroyed using Destroy(gameObject) to maintain the Singleton pattern.
  3. Persistence Across Scenes: the method DontDestroyOnLoad(gameObject) is used to ensure that the GameObject (and thus, the SoundManager instance) persists between scene changes. This is crucial for maintaining consistent audio behavior across different scenes.

Accessing the Singleton Instance

Once set up, other scripts can easily access the SoundManager's functionality throughout the game using SoundManager.instance. For example, to play an audio effect, another script can call SoundManager.instance.PlayEffect(index). This allows any script in the game to interact with the SoundManager without creating multiple instances or worrying about initialization.

Advantages of Using Singleton

  • Centralized Management: provides a central point for managing sound-related functionality, making it easier to control and maintain audio playback across the game;

  • Global Access: the Singleton instance can be accessed globally from any script, allowing different parts of the game to interact with the sound system seamlessly;

  • Persistence: ensures consistent audio playback throughout the game without interruption during scene changes.

Inicialización de Singleton en Unity:

En Unity, a menudo necesitamos que ciertos gestores o controladores persistan a través de las escenas, como un gestor de sonido. La clase SoundManager está diseñada como un Singleton para asegurar que solo haya una instancia de ella durante toda la vida del juego. Así es como se utiliza dentro de Unity:

Instancia Única: La línea SoundManager pública estática; declara una variable estática instancia del tipo SoundManager. Esta variable contiene la única instancia de la clase SoundManager.

Inicialización en el Método Start(): En el método Start(), que es llamado automáticamente por Unity cuando el GameObject al que este script está adjunto se activa, se inicializa la instancia del Singleton.

Si la instancia es nula, lo que significa que aún no existe una instancia de SoundManager, la instancia actual (this) se asigna a la instancia, convirtiendo efectivamente este objeto en la instancia singleton.

1. What is the purpose of the instance variable in the SoundManager class?

2. What is the purpose of the instance variable in the SoundManager class?

What is the purpose of the instance variable in the `SoundManager` class?

What is the purpose of the instance variable in the SoundManager class?

Selecciona la respuesta correcta

What is the purpose of the instance variable in the `SoundManager` class?

What is the purpose of the instance variable in the SoundManager class?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt