35
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity5で脱オレオレAudioManager

Posted at

Unity5ではAudioMixerというAPIがある

俺はオレオレ仕様が大嫌いだ。いい設計とは無駄なマイノリティを発揮しないことだと思う。
ただUnity4.xまではAudioSourceから発された音源はそのままAudioListenerに届くため、ゲーム内のコンフィグ画面などでBGM,SEなどを調節するスライダーを設置する際に、大抵は自作のAudioManagerなるものを作り(空オブジェクトにC#貼り付けたようなアレ)、そこにBGMに使う系のAudioSource、SEに使う系のAudioSourceをインスペクターから登録できるような仕様のものを作ることになると思う。
ただUnity5ではAudioMixerがあるのでオレオレAudioManagerをやめてこの公式APIを活用した方がスマートだと思う。

今回は音量を調節することを例にして解説するが、歪み(ひずみ)などその他のパラメータの調節もこの方法で可能だ。

AudioMixerを使う

まずAudioMixer画面。
MainMenu.unity - Last Human - iPhone, iPod Touch and iPad 2015-04-27 15-22-52.jpg

まだ使ったことがない人は空っぽになっていると思うので左にあるGroupsの+をクリックしていくつかのGroupを作成する。とりあえずBGMとかSEとかがあればいいと思う。
図のように入れ子にすることができる。SE配下にVoice,Weapon,Playerを作成している。

作成したGroupのうち一つを選択した状態でInspectorは下のように表示される。

MainMenu.unity - Last Human - iPhone, iPod Touch and iPad 2015-04-27 15-27-42.jpg

右クリックするとこんなメニューが出る。
MainMenu.unity - Last Human - iPhone, iPod Touch and iPad 2015-04-27 15-31-02.jpg

Exposeを選ぶことで任意のパラメーターをスクリプトから操作できるようになる。
今回はVolumeをExposeする。

Exposeしたパラメーターは下の図のように右上の小さなプルダウンから確認できる。ここでパラメーターに名前を付け直すことが出来るのでここでは全てなんたらVolumeという名前をつけておいた。

MainMenu.unity - Last Human - iPhone, iPod Touch and iPad 2015-04-27 15-42-47.jpg

一旦ここでAudioSourceから出た音をAudioMixerに通す方法を解説する。

これは簡単である。
まずはAudioSourceコンポーネントをもつゲームオブジェクトを選択しInspectorにAudioSourceコンポーネントを表示させる。

MainMenu.unity - Last Human - iPhone, iPod Touch and iPad 2015-04-27 15-36-06.jpg

あとは図のようにOutput Audio Mixer Groupに先ほど作成したGroupのうちの一つを選んで登録する。

これでこのAudioSourceからの音はAudioMixerで音量、エフェクトともに操作できるようになった。

あとはAudioMixerクラスのSetFloatメソッドで値を操作できる。
http://docs.unity3d.com/ScriptReference/Audio.AudioMixer.SetFloat.html

audioMixer.SetFloat("パラメーター名", float値);
という感じで使えばいい。

例えばUnityのGUIのSliderで操作する仕様にしたければ

MainMenu.unity - Last Human - iPhone, iPod Touch and iPad 2015-04-27 15-55-08.jpg

というふうにMinとMaxと設定すればSliderの値をそのまま音量に使える。最大値を0以上にすると音割れすることがあるので上限は0が良い。

PlayMakerを使ってノースクリプトで作りたい人のためにヒントを載せておく。
MainMenu.unity - Last Human - iPhone, iPod Touch and iPad 2015-04-27 15-58-08.jpg

uGuiのSliderの値のGetやSetはEcoSystemで入手すると良い。
AudioMixerに関するPlayMakerのアクションは現状ではEcoSystem上で検索しても一つも出てこなかったので今からプレゼントする。

AudioMixerSetFloat.cs
using UnityEngine;
using HutongGames.PlayMaker;
using UnityEngine.Audio;

namespace HutongGames.PlayMaker.Actions {

	[ActionCategory(ActionCategory.Audio)]
	[Tooltip("Sets the value of the exposed parameter specified.")]
	public class AudioMixerSetFloat : FsmStateAction {

		[ObjectTypeAttribute(typeof(AudioMixer))]
		[Tooltip("Target AudioMixer.")]
		public FsmObject audioMixser;
		[Tooltip("Value to set.")]
		public FsmFloat value;
		[Tooltip("Exposed Parameter's name.")]
		public FsmString name;

		// Code that runs on entering the state.
		public override void OnEnter() {
			var mixer = audioMixser.Value as AudioMixer;
			mixer.SetFloat(name.Value, value.Value);
			Finish();
		}
	}
}

それではチャオ!

35
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?