TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Unity Sound

Unity Sound with Introduction, Installing, GameObject, First Unity Project, Unity 2D, Sprite Unity, Loops, If Statement, Data Types, Swith Statements, Unity UI, Unity Asset Store etc.

<< Back to UNITY

Unity Sound

Creating the visual elements of a game is only half of the game, adding sounds to your game are just as important as developing amazing shaders. Unity's sound system is flexible and powerful.

Unity can import most standard audio file formats and has features for playing sounds in 3D space, optionally with effects like echo and filtering applied. Even Unity can also record audio from any available microphone on a user's machine for use during gameplay or storage and transmission.

There are two components related to Audio in Unity; they are:

  • Audio Listener
  • Audio Source

Let's see these components one by one:

Audio Listener

Audio Listener is the component that is automatically attached to the main camera every time you create a scene. It does not have any properties since its only job is to act as the point of perception.

This component listens to all audio playing in the scene and transfers it to the system's speaker. It acts as the ears of the game. Only one AudioListener should be in a scene for it to function properly.

Unity Sound

Audio Source

The audio source is the primary component that you will attach to a GameObject to make it play sound. This is the component that is responsible for playing the sound.

To add the Audio Source component, select one GameObject, and go to the Inspector tab. Click on Add Component and search for Audio Source.

Unity Sound

Select Audio Source.

Audio source will playback an Audio Clip when triggered through the mixer, through code or by default, when it awakes.

An Audio Clip is a sound file that is loaded into an AudioSource. It can be any standard audio file such as .wav, .mp3, and so on. An Audio Clip is a component within itself.

Unity Sound

Playing a Sound

Let's add a button that plays a sound when it is clicked. For this, first of all, create a sprite and do one color. Here, I am creating a circle sprite and making it red.

Unity Sound

Now, attach an Audio Source component to this circle sprite.

Unity Sound

Now, you have to import one audio file. Here, I am downloading success sound. To download it, click here.

Drag this sound file into Assets.

Drag this sound clip from Assets to the Audio clip slot in our sprite's Audio source component.

Unity Sound
  • Uncheck the "Play on Awake" in the audio source properties. Not doing so will make the sound play the moment the game starts.
Unity Sound
  • Create a new script called "SuccessSound" and open it up.
  • In this script file, we have to set up the method to detect the object being clicked. MonoBehaviour gives us just the method we need for it, named onMouseDown. The method is called whenever the mouse clicks in the range of a collider of that gameObject.

For this add 'Circle Collider 2D' component.

Unity Sound
  • We will not need a Rigidbody for this one; neither do we need to access this collider by code. It just has to be there for the method to work.
  • Finally, copy the following code in your script file SuccessSound.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SuccessSound : MonoBehaviour
{
    AudioSource mySource;

   	void OnMouseDown() {
   		mySource.Play();
   		Debug.Log("Clicked!");
    }
   // Use this for initialization
   void Start () {
      mySource = GetComponent();
    }
}

Attach this script to your circle script. Now play the game. Clicking on the button (red circle sprite) should show a message to the console, and you should hear the success sound.

Unity Sound




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf