Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

Table of Contents (17 chapters)
Unity Game Development Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Background music


The first part of audio that we will cover will be the background music. Having music in your background can set the mood of a scene, keep the player entertained on a subconscious level, or even be gameplay mechanics that the player interacts with. We will create a dynamic system that will allow us to play songs randomly or in a playlist style.

Creating a random system

The first step in creating our background music system will be to create a new C# script and name it BG_Music_Manager. Before we start scripting, add the using statement to the top of the script using the other using statements:

Using System.Collections.Generic;

We will need the using statement so that we can use lists. Next, we will create a few variables and add these to our script:

public List<AudioClip> SongList = new List<AudioClip>();
public float bgVolume = 1.00f;
public int curSong = 0;
public int ranMin, ranMax;
public bool playRandomly = false;

Our first variable is a list of audio clips, which...