Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by holman8er · Nov 16, 2011 at 07:15 PM · audiotagvolumeoptions

Audio Tagging

We are in the process of setting up audio options from our start menu. We would like to be able to set the volume for Sfx, Music, and Dialogue. I thought the best way to do this would just be to tag each of the audio clips as sfx, mus, or dia, and access them through script using there tag. However, it seems that audio clips cannot be tagged in the editor. Is there another way to achieve this? What's the best way of manipulating the volume of different types of audio?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FLASHDENMARK · Nov 16, 2011 at 09:27 PM 0
Share

That is a really good question, I have also tried(and failed) creating a distinguish-able audio system(I never left the drawing board).

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Nov 17, 2011 at 02:15 AM

You could define the global Sfx, Music and Dialog volumes in static variables in the volume control script, and have a special script attached to every object that has an AudioSource, which would define the type and the volume of the sound - something like this:

1- In the volume control script (let's call it VolumeControl.js):

static var sfxVolume: float = 0.7; static var musicVolume: float = 0.3; static var dialogVolume: float = 0.8;

function OnGUI(){ // adjust the volume of each type }

2- Attach the script below to objects that have an AudioSource:

enum SoundType {Sfx, Music, Dialog} var sType: SoundType = SoundType.Sfx; // define the sound type in the Inspector

private var iniVol: float; private var gVol: float;

function Start(){ iniVol = audio.volume; // save the local volume }

function Update(){ switch (sType){ // get the correct type volume case SoundType.Sfx: gVol = VolumeControl.sfxVolume; break; case SoundType.Music: gVol = VolumeControl.musicVolume; break; case SoundType.Dialog: gVol = VolumeControl.dialogVolume; break; } audio.volume = iniVol * gVol; }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FLASHDENMARK · Nov 26, 2011 at 07:04 PM 0
Share

Would this not be quiet resource expensive when(perhaps) there are thousands of audioSources in the scene all updating every frame?

avatar image aldonaletto · Nov 27, 2011 at 04:04 AM 0
Share

It would take some time, for sure, but not so much. Anyway, this could be optimized if the volume control script had a static boolean variable - say, volumeChanged - that could be set for one or two frames to tell all AudioSources to update their volumes.

avatar image
2

Answer by rabbitfang · Nov 26, 2011 at 08:49 PM

I currently have a project that deals with many AudioSources. While I currently only have one volume level, the system I use could easily be extended to fit your purposes.

The volume control script (called VolumeControl.js):

 // Volumes stored into array as Sfx, Music, and Dialog indexed respectively
 static var volumes : float[] = new float[3];
 // audioSources stores a reference to each audio source in an ArrayList
 // based on what volume level is controlling it (parallel array with volumes[])
 static var audioSources : ArrayList[3] = new ArrayList[3];

 public static function SetVolume(volumeGroup : int, volume : float) {
     if (volume != volumes[volumeGroup]) {
         volumes[volumeGroup] = volume;
         updateVolume(volumeGroup);
     }
 }

 //Send the new volume level to the audio sources
 static function updateVolume(volumeGroup : int) {
     for (var i = 0; i < audioSources[volumeGroup].Count; i++) {
         var obj : GameObject = keys[i];
         obj.BroadcastMessage("UpdateVolume", volumes[volumeGroup], SendMessageOptions.DontRequireReceiver);
     }
 }

 public static function AddSource(audioSource : GameObject, volumeGroup : int) {
     if (audioSources[volumeGroup] == null)
         audioSources[volumeGroup] = new ArrayList();
     audioSources[volumeGroup].Add(audioSource);
 }

 public static function GetVolume(volumeGroup : int) : float {
     return volumes[volumeGroup];
 }

That script might need some modifications to better suite your needs but it should work out. You will need to add a function that removes the audio sources from the ArrayList if you do delete the AudioSources.

Then this script goes into the audio sources themselves. (Borrowing an 2 ideas from aldonaletto's answer.)

 enum SoundGroup {Sfx, Music, Dialog}
 var sGroup : SoundGroup = SoundGroup.Sfx;
 
 private var startVol : float;
 private var groupVol : float;
 
 function Start() {
     startVol = audio.volume;
     switch (sGroup) {
         case SoundGroup.Sfx:
             groupVolume = VolumeControl.GetVolume(0);
             VolumeControl.AddSource(gameObject, 0);
             break;
         case SoundGroup.Music:
             groupVolume = VolumeControl.GetVolume(1);
             VolumeControl.AddSource(gameObject, 1);
             break;
         case SoundGroup.Dialog:
             groupVolume = VolumeControl.GetVolume(2);
             VolumeControl.AddSource(gameObject, 2);
             break;
     }
     audio.volume = groupVolume * startVolume;
 }
 
 function UpdateVolume(volume : float) {
     groupVolume = volume;
     audio.volume = groupVolume * startVolume;
 }

These scripts will only cause slow downs when the volume changes and there are large amounts of AudioSources in the group being updated. All you need to do is call VolumeControl.SetVolume when the user touches the volume control. The system automatically checks to see if the volume level has changed and only updates if it has, so it is safe to call VolumeControl.SetVolume every frame, within the OnGui() function for instance.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Nov 27, 2011 at 04:09 AM 0
Share

This is basically the way I would do it (with some variations) but the basic principle of registering audio sources with a central manager that then controls their volumes in a sensible fashion is a good, extendible one.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change volume on many audio objects with specific tag ? 1 Answer

Setting audio volume with horizontal slider (to many AudioSources with different volume) 2 Answers

Audio on object plays low in unity 5 1 Answer

How to Create Separate Volumes 1 Answer

Sound effects too quiet 8 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges