Skip to content

Asset Programming

Innieeee edited this page Feb 26, 2023 · 15 revisions

Asset Programming Documentation

UI System

Introduction

I will advice you to go learn the basics of Unity UI before proceeding:

START MENU in Unity

A Beginner Guide to TEXTMESHPRO (Unity UI Tutorial)

Using the UI System

All the UI prefabs you will need can be found in this location

First time use

The UI System prefab should be dragged to the first scene in the game. All menu prefabs should be dragged into the "Menu Prefabs".

Code Examples

Simple way to open a menu: MenuCassName.Open(Action OnOpened = null);

MainMenu.Open();

or

MainMenu.Open("A void Function or lambda");

Simple way to close a menu : : MenuCassName.Close(Action OnClosed= null);

MainMenu.Close();

or

MainMenu.Close("A void Function or lambda");

Editing UI Menu

  • Drag the Menu Prefab into the scene
  • You can see all the properties in the inspector
  • Now you can edit the Image and location of UI components

Audio System

Introduction

I will advice you to go learn the basics of Unity Audio before proceeding

Introduction to AUDIO in Unity

Using the Audio System

Locate the Audio System

Drag the Audio Manager to the first scene

  • Music Bank - Audio Bank
  • Sfx Bank (Sound Effect) - Audio Bank
  • Music Player - Audio source
  • SoundEffect Player - Audio source

Make sure all required components are dragged in place to avoid error.

Audio Bank

  • A single data in the audio bank contains - > ID (Audio ID), Audio Clip and Info
  • The ID is very important and needs to be unique per data, this is used for getting the audio clip in code
  • The Audio Clip is a reference to the audio clip asset, this can be dragged in or selected by clicking on the white circle dot on the right the field.
  • The Similar Audi Clip is a reference to the audio clip array. Used for audio with random sounds
  • The Info text field is useful for providing little information about the audio if necessary

The audio bank will give error if there are more than one data with the same ID. The audio bank single data box will be red if it has no audio clip. Make sure you put music in music bank & sound effects in Sfx bank!!!

Get Audio from bank

// AudioManager.GetMusicClip(string audioID)

// AudioManager.GetSoundEffectClip(string audioID)

Code Examples

Play Music

// AudioManager.PlayMusic(string audioID, bool loop = true)

Pause, Resume, Stop Music

// AudioManager.PauseMusic()

// AudioManager.ResumeMusic()

// AudioManager.StopMusic()

Change Music with fade

// AudioManager.ChangeMusicWithFade(string audioID, bool loop = true, float speed = 0.05f)

Blend between two music

// AudioManager.BlendTwoMusic(string startAudioID, string nextAudioID, bool loop = true)

Play Sound Effect

// AudioManager.PlaySoundEffect(string audioID, bool randomPitch = false)

3D Audio

Watch this video to understand the basics of Unity 3D Audio

Audio Only Be Heard in a Certain Radius/Distance - Unity 3D Sound

  • Add the Audio3D script to your gameobjects, it will automatically add an audio source component
  • You need to set the audio source component -> Spatial Blend = 1;
  • You also need to edit the 3D Sound settings -> Volume Rolloff can be set to Linear Rolloff
  • You can then edit the min distance and max distance to your preference.

Useful functions in Audio3D script

// PlaySoundEffect(string audioID)

// PlayMusic(string audioID, bool loop)

// SetMinDistance(float minDistance) "Distance of sound"

// SetMaxDistance(float maxDistance) "Distance of sound"