SoundPlayer
The SoundPlayer
is a versatile audio component designed to handle the playback of various sound effects and audio clips in Unity projects. It offers an easy-to-use interface for managing sounds and provides functionalities like volume control, pitch adjustment, and spatial 3D sound positioning.
Key features and components of the SoundPlayer
class:
- Initialization: The
SoundyService.Initialize()
method is called in theAwake()
method to ensure that theSoundyService
is initialized before using it. - SoundId and Sound Library: The
SoundPlayer
has aSoundId
field, which represents the sound to be played. TheSoundId
combines the name of the sound library and the name of the sound within that library. The class provides methods to set a new sound by either providing theSoundId
or the library name and sound name directly. - PlayOnStart, PlayOnEnable, PlayOnDisable: These boolean properties determine whether the sound should automatically play when the GameObject is started, enabled, or disabled, respectively.
- StopOnDisable, StopOnDestroy: These boolean properties determine whether the sound should automatically stop when the GameObject is disabled or destroyed, respectively.
- Follow Target: The
SoundPlayer
allows you to set a transform as the follow target. While playing, the sound will follow the specified transform, which can be useful for creating 3D audio effects. - AudioPlayer, SoundLibrary, SoundObject: The
SoundPlayer
holds references to theAudioPlayer
,SoundLibrary
, andSoundObject
related to the loaded sound. TheAudioPlayer
is responsible for actual sound playback. - ResetPlayer(): This method clears the
SoundPlayer
, resets theSoundId
, and sets all references to null. After calling this method, theSoundPlayer
won’t be able to play any sound until a new sound is set. - Play(): This method plays the loaded sound object. If the sound is already playing, it restarts it. If the sound object is not loaded or cannot play, nothing happens. If an
AudioPlayer
is already available (from the object pool), it will be used; otherwise, a newAudioPlayer
is obtained from theSoundyService
. - Stop(): This method stops the loaded sound object if it is currently playing.
Getting Started
Follow these simple steps to set up and utilize the SoundPlayer
in your project:
- Attach the
SoundPlayer
component to a GameObject in your scene. - Customize the properties of the
SoundPlayer
using the inspector to configure its behavior according to your project’s requirements. - Use the provided methods to play, stop, pause, resume, and control the volume and pitch of your sound effects.
Sound Playback
The SoundPlayer
allows you to effortlessly play sound effects and audio clips through simple-to-use methods. You can trigger sounds from user interactions, events, or scripts, providing auditory feedback to enhance the overall user experience.
Code Example
// Create a new SoundPlayer component and set the sound to play
SoundPlayer soundPlayer =
SoundPlayer
.Get()
.SetSound("LibraryName", "SoundName");
// Play the sound
soundPlayer.Play();
Code language: C# (cs)
For more detailed information, please refer to the official API documentation available at https://api.doozyui.com.