【Unityゲーム作成講座2】アドベンチャーゲームの作り方#12(サウンドシステム①)【ゆっくり解説】

ハッピーサンライズ
20 Dec 202122:31

Summary

TLDRThis video script offers a detailed tutorial on implementing a sound system in a game. It guides viewers through creating an AudioManager script to manage volume levels for music, sound effects, and system sounds. The tutorial covers attaching an AudioSource component for background music, setting up sliders for volume control within Unity's UI, and writing functions to adjust and play sounds. It also discusses obtaining sound sources from royalty-free platforms like DOVA-SYNDROME and SoundEffect-Lab for battle and normal BGMs, as well as sound effects for various in-game actions. The script concludes with tips on smoothly transitioning between different music tracks for an enhanced gaming experience.

Takeaways

  • 🎧 The video provides a tutorial on how to implement a sound system in a game.
  • 🛠️ It begins by instructing viewers to create an 'Audio Manager' script attached to a main object for managing audio settings.
  • 🔊 The script includes variables for master volume, sound effects volume, system sound volume, and music volume, all initialized to 1.
  • 📐 The master volume is used to adjust the overall volume across four channels: SFX, system sounds, and music.
  • 🔗 The tutorial explains how to protect these values from being changed externally by using properties.
  • 🎵 For managing background music, the script uses an AudioSource component attached to a game object that is created and set as a child of the main object.
  • 📹 The 'Awake' function, which runs once at the start of the game, is used to set up the AudioSource and attach it to a newly created game object.
  • 🔄 The video also covers creating functions to adjust the volume of sound effects and system sounds, as well as the master volume.
  • 📊 Unity's UI system is utilized to create sliders for adjusting volumes, which are scripted to interact with the 'Audio Manager'.
  • 🔗 A 'System UI' script is created to manage the UI components, including a system canvas and volume sliders.
  • 🎶 The tutorial mentions using royalty-free music from DOVA-SYNDROME for background music and sound effects from EffectLab for various in-game actions.

Q & A

  • What is the main topic of the video script?

    -The main topic of the video script is about explaining how to implement a sound system in a game, including creating an AudioManager script and managing audio volumes.

  • What is the purpose of creating an AudioManager script?

    -The AudioManager script is created to manage and control the audio settings within the game, such as adjusting the master volume, sound effects volume, system sounds volume, and background music volume.

  • How does the script handle the initial volume levels for different audio channels?

    -The script initializes the volume levels with public float variables for master volume, sound effects volume, system volume, and music volume, all set to 1, which represents the maximum volume.

  • What is the role of the AudioSource component in the script?

    -The AudioSource component is used to play music by attaching it to a game object. The script also includes a method to automatically generate a game object with an AudioSource component attached when the game starts.

  • How does the script ensure that the AudioManager instance is unique throughout the game?

    -The script uses a singleton pattern for the AudioManager class, ensuring that there is only one instance of the AudioManager throughout the game's lifecycle.

  • What is the purpose of the 'Start' function in the script?

    -The 'Start' function in the script is used to initialize the audio system, such as creating a game object with an AudioSource component and setting it up to play music.

  • How can the volume of the background music be adjusted?

    -The volume of the background music can be adjusted by creating a function that changes the 'musicVol' variable and then sets the AudioSource volume to the product of 'musicVolPercent' and 'MasterVolumePercent'.

  • What is the function of the 'SetVolumeMaster' method in the script?

    -The 'SetVolumeMaster' method is used to adjust the master volume by setting the 'MasterVolume' variable and then updating the AudioSource volume to reflect the new master volume setting.

  • How does the script handle the creation of sliders for volume control?

    -The script creates sliders using Unity's UI system, assigns them to control different audio channels, and links their values to the corresponding volume variables in the AudioManager script.

  • What is the purpose of the 'PlayClipAtPoint' function mentioned in the script?

    -The 'PlayClipAtPoint' function is used to play a sound effect at a specific location in the game world by utilizing the position of a transform and the volume settings.

  • How does the script manage the transition between different background music tracks?

    -The script plays a new music track by attaching it to the dedicated AudioSource for music. It suggests a future improvement to fade out the current music track and fade in the next one for a smooth transition.

Outlines

00:00

🎧 Audio System Implementation

The script introduces the process of implementing an audio system in a game. It begins by suggesting the creation of an 'Audio Manager' script attached to a main object. The script includes variables for master volume, sound effects volume, and music volume, all of which are adjustable. The tutorial also covers the automatic attachment of an AudioSource component to a game object and the use of the Awake function to initialize audio settings. Additionally, it explains how to create functions for adjusting the volume of sound effects and system sounds, and how to link these functions to sliders for user control.

05:02

🛠 Building the System UI for Volume Control

This paragraph details the creation of a user interface for system settings, specifically for volume control. It describes the process of making a button and a panel in Unity's UI system, naming them appropriately and setting up the panel for transparency and visibility. The tutorial then guides through adding sliders for master volume, music, sound effects, and system sounds within the panel. It explains how to set initial values for the sliders and how to link them to the previously created volume adjustment functions in the script. The paragraph also covers creating a script to manage the system UI, attaching it to the main object, and ensuring the UI updates correctly with the volume settings.

10:03

🔊 Implementing Singleton Pattern for AudioManager

The speaker discusses the implementation of the singleton pattern for the AudioManager class to ensure there is only one instance throughout the game. They explain the concept of a static variable that points to a single instance and provide a brief overview of how singletons work in game development. The tutorial includes steps to initialize the AudioManager instance correctly during scene changes and emphasizes the importance of writing code that is always in motion, rather than getting stuck. It also touches on the process of attaching audio sources to the AudioManager and adjusting the volume using sliders, which is crucial for the game's audio system.

15:05

🎵 Acquiring and Setting Up Audio Sources

The paragraph focuses on acquiring audio sources for the game, mentioning the use of royalty-free music from specific composers and sound effect labs. It details the process of downloading background music (BGM) for different game states, such as combat and normal gameplay, and sound effects for actions like attacking or button presses. The tutorial then explains how to import these audio files into Unity, organize them into an 'Audio' folder, and attach them to the AudioManager for use within the game.

20:07

📡 Playing Audio Clips and Managing BGM Transitions

This section covers the functionality of playing audio clips and managing background music within the game. It explains how to create functions to play system sounds and sound effects using the AudioSource component and how to adjust their volume based on the master volume and individual settings. The tutorial also addresses the playing of music by attaching different audio clips to a dedicated AudioSource and provides a method to avoid resetting the music clip upon entering different game states. The speaker plans to create a smooth transition between different BGM tracks in the next part of the tutorial.

Mindmap

Keywords

💡Audio System

The audio system refers to the collection of components and processes that enable the generation, manipulation, and playback of audio within a game environment. In the video's context, the audio system is central to the theme as it discusses how to integrate and manage audio within a game, including creating an AudioManager script to handle various audio channels and their volumes.

💡Audio Manager Script

The AudioManager script is a crucial part of the audio system in the video. It is a custom script attached to a main object in the game that manages audio playback, including volume levels for different audio types such as music, sound effects, and system sounds. The script is used to create variables for master volume, SFX volume, system volume, and music volume, which are essential for controlling the audio output.

💡Volume Control

Volume control in the video pertains to the ability to adjust the loudness of different audio channels within the game. The AudioManager script includes public float variables for master volume, SFX volume, system volume, and music volume, which are used to regulate the overall audio output. The concept is integral to the video's theme as it explains how to implement volume control through Unity's scripting and UI elements.

💡Audio Source Component

The AudioSource component in Unity is used to play back audio clips. In the video, the script creates an AudioSource component attached to a game object to manage background music (BGM). The component is essential for the audio system as it allows for the playing of audio clips at specific game object positions, which is demonstrated when creating a music source object.

💡Unity

Unity is a cross-platform game engine used for creating video games. The video's theme revolves around using Unity to develop an audio system for a game. The script examples and UI elements discussed are all implemented within the Unity engine, showcasing how to integrate audio functionalities into a game using Unity's tools and scripting capabilities.

💡BGM (Background Music)

Background music, or BGM, is the music that plays in the background of a game to set the mood and atmosphere. The video discusses managing BGM through an AudioSource component and an AudioManager script. It also mentions the process of selecting and attaching different BGM tracks for various in-game states, such as normal and battle themes.

💡Sound Effects

Sound effects are the short audio clips that are played in response to specific actions or events within a game. The video script explains how to create functions within the AudioManager script to play sound effects using Unity's PlayClipAtPoint function, which takes an audio clip, a position, and volume as parameters.

💡UI (User Interface)

The user interface, or UI, in the video refers to the in-game graphical elements that allow players to interact with the game, such as sliders for volume control. The script discusses creating a system canvas with sliders for adjusting master volume, music volume, SFX volume, and system volume, which are essential for giving players control over the game's audio settings.

💡Singleton Pattern

The singleton pattern is a software design pattern that restricts the instantiation of a class to one single instance and provides a global point of access to it. In the video, the AudioManager script uses the singleton pattern to ensure that there is only one instance of the AudioManager throughout the game, which is crucial for managing audio without conflicts.

💡Audio Clips

Audio clips are the individual audio files used in a game for various purposes, such as background music, sound effects, and system sounds. The video script mentions downloading and attaching audio clips to the AudioManager script, emphasizing the importance of selecting high-quality, royalty-free audio clips for game development.

Highlights

Introduction to the method of implementing a sound system in a game.

Explanation on creating an 'Audio Manager' script attached to the main object.

Setting up float variables for volume control, including 'Master Volume Percent'.

Creating a function to adjust the volume of BGM, SFX, and system sounds.

Automatic attachment of the AudioSource component to a game object.

Introduction of the Awake function for initializing game system constructions.

Creating a game object and attaching an AudioSource component for music.

Adjusting the volume of BGM using a parameter in the script.

Building functions to adjust the volume of SFX and system sounds.

Creating sliders in Unity for volume control.

Scripting to manage the opening and closing of the system UI panel.

Explanation of the singleton pattern for the AudioManager class.

Using delegates for slider value change events.

Acquiring music and sound effects from various sources for the game.

Creating functions to play system sounds and sound effects.

Implementation of music playback with consideration of volume levels.

Adjusting the game's audio system to avoid abrupt music changes.

Preview of next episode's content focusing on smooth music transitions.

Transcripts

play00:00

みなさんこんにちは happy

play00:01

sunrise のゆっくり霊夢です今回

play00:03

はサウンドシステムの導入の仕方について

play00:06

解説していきます今回の作業内容の

play00:08

プロジェクトファイル及び参考にさせて

play00:10

いただいた動画の url は概要欄に

play00:13

貼ってありますというわけで早速レッツ

play00:15

ゴー

play00:20

早速始めて以降今回はオーディオシステム

play00:22

をつくってゲーム内におとう鳴らす仕組み

play00:24

を解説するよ有無まず何すりゃいいんだ

play00:27

まずはスクリプトを作ろう

play00:29

オーディオマネージャーというスクリプト

play00:30

作ってオーディオマネージャー

play00:32

ほいそのスクリプトをメインオブジェクト

play00:34

にアタッチして

play00:37

ほいじゃあそのスクリプト開いて入らない

play00:40

中身決してまずは音量を調節する仕組み

play00:42

から作るよ音量を表すフロート変数を用意

play00:45

しよう public float 変数

play00:47

名はマスターボリュームパーセント

play00:48

マスターボリュームパーセント初期値は

play00:50

とりあえず1とかにしておこう=1 f

play00:53

この値が1の場合は音量 max 例の

play00:55

場合はミュートになる付6ふむ次は効果音

play00:58

の音量変数名は sfx ボリューム

play01:00

パーセント sfx ってなんだサウンド

play01:03

エフェクトの約なるほど public

play01:05

float sfx ボリューム%=1次

play01:08

はシステム音木鶏 public

play01:10

float システムボリューム%=1

play01:12

最後に bgm public float

play01:14

music vol %=1音量調節は

play01:18

以上の4チャンネルに分ける下の3つの

play01:20

音量にマスター音量を掛け合わせることで

play01:22

全体の音量を調節できるようにするよう

play01:25

ふむふむ一応この値を外から勝手に変え

play01:27

られないようにプロパティで保護しよう

play01:31

次は bgm を管理するための

play01:33

コンポーネントを用意するオーディオ

play01:35

ソースというコンポーネントに音声

play01:36

クリップを当てはめれば音楽を流すことが

play01:38

できるじゃあユニティーに戻って

play01:40

オーディオソースをゲームオブジェクトに

play01:42

アタッチし俺止まった今回はその作業も

play01:44

スクリプト内で自動的に行うそんなこと

play01:46

できるのかオーディオソース型の変数作っ

play01:49

て変数名はミュージックそうすぱぶりっく

play01:51

は要らないオーディオソースミュージック

play01:53

ソースではゲーム開始と同時にオーディオ

play01:56

ソースコンポーネントがータッチされた

play01:58

ゲームオブジェクトを生成しそれをメイン

play02:00

オブジェクトの子にする処理を書こうその

play02:02

仕組みはウェイク関数に各アウェイク関数

play02:04

過去のシリーズの中では初登場かなあ

play02:07

ウェイク関数はゲーム開始と同時に一度

play02:09

だけ実行される関数だ4つたと関数よりも

play02:12

前に実行されるんだったかそうだから

play02:14

ゲーム内のキャラのパラメーターの初期化

play02:16

とかとは違ってゲームのシステムの構築の

play02:19

ための処理などを書くまあ明確なルールが

play02:21

あるわけじゃないからどっちでもいいんだ

play02:23

けどねまず何を書けばいいまずは新規の

play02:25

ゲームオブジェクトを生成する処理ゲーム

play02:27

オブジェクト

play02:28

ニューミュージックソース=ニューゲーム

play02:30

オブジェクトmusic 総数これで

play02:32

ミュージックソースという名前のゲーム

play02:34

オブジェクトがヒエラルキー常に作られる

play02:36

ふむふむミュージック総数 at

play02:38

コンポーネントオーディオ総数これでこの

play02:40

オブジェクトにオーディオソースがー

play02:41

タッチされたふむそれと同時に

play02:43

ミュージックソースにコンポーネントを

play02:45

代入しよう music そう水=

play02:47

なるほど最後にこのゲームオブジェクトを

play02:50

メインオブジェクトの子に設定するニュー

play02:52

ミュージックソーストランスフォーム

play02:53

ペアレント=トランスフォーム

play02:56

これで1回動かしてどうなるか見てみよう

play03:03

をメインオブジェクトの下にミュージック

play03:05

ソースって名前のオブジェクトが生成され

play03:07

たそしてそのオブジェクトにオーディオ

play03:09

ソースコンポーネントがアタッチされて

play03:11

いるな成功だねこのオーディオクリップと

play03:13

いう枠の中に音声データをアタッチして

play03:15

再生すればこのゲームオブジェクトのある

play03:17

位置からおとう鳴らすことができる今回の

play03:19

ゲームは3 d じゃないから意味ない

play03:21

けどな今回のゲームで使うのはこの

play03:23

ボリュームというパラメーターだけだね

play03:25

スクリプトでこの値を動かして bgm の

play03:27

音量を調節するというわけで次は音量調節

play03:30

関数を作ろうちょっと待て今 bgm 用

play03:33

のオーディオソースは用意したが効果音と

play03:35

システム音にも同様にオーディオソースが

play03:37

必要なんじゃないか効果音とシステム音は

play03:39

長久手市に秒の単発の音だからわざわざ

play03:42

オーディオソース用椅子必要はない単発の

play03:44

おとう鳴らすだけならオーディオソース.

play03:46

プレイクリップ at ポイントという

play03:48

便利関数を実行すればいいだけはいえーい

play03:50

まずは効果音のボリュームを調整する関数

play03:53

から関数名はセットボリュームアンダー

play03:55

バー sfx にしよう public

play03:57

ぽいどセットボリュームアンダーバー

play03:59

sfx 引数はフロートを1個変数名は

play04:02

パーセンとフロートパーセント中身は超

play04:05

単純 sfx ボリューム%=パーセント

play04:08

音量はスライダーで調節できるようにする

play04:11

スライダーの値をこの関数の引数に

play04:13

当てはめれば自在に音量調節ができ

play04:15

るってわけですふむ6次はシステム音の

play04:18

ボリューム変更処理 public

play04:19

void セットボリューム

play04:21

システムフローとパーセントシステム

play04:23

ボリューム%=パーセント次 bgm の

play04:27

ボリューム public void

play04:28

セットボリュームミュージックフロート

play04:31

パーセント music vol %=

play04:33

パーセントここではオーディオソースの

play04:35

ボリュームの値もどう時に変更する必要が

play04:38

ある music ソースポリューム=

play04:41

music vol パーセントかける

play04:43

マスターボリューム%ふむ最後にマスター

play04:46

音量 public void セット

play04:48

ボリュームマスター

play04:50

マスターボリューム=パーセントここでも

play04:53

オーディオソースのボリュームを変更する

play04:54

ミュージックソースボリューム=

play04:57

music vol パーセントかける

play04:59

マスターボリュームパーセントよし今度は

play05:01

スライダーを作るよユニティーに戻って

play05:03

ホイ画面の右下にボタン作ってボタンの

play05:06

名前はシステムキャンバスを右クリック

play05:08

ui ボタンその後ボタンを画面右下に

play05:11

移動うそのボタンの中の文字をシステムに

play05:13

書き換える

play05:15

システムこのボタンを押したらシステム

play05:17

画面を開けるように使用画面のど真ん中に

play05:20

パネルを用意してキャンバスを右クリック

play05:21

ui パネル

play05:24

そしてパネルをちょっと縮める

play05:30

こんなもんかパネルが半透明だとちょっと

play05:33

見づらいから色を濃くしてパネルの

play05:35

イメージコンポーネントのカラーを

play05:36

クリックアルファの値を100にするこれ

play05:38

でよしパネルの名前はパネルアンダーバー

play05:41

システムにしようパネルアンダーバー

play05:42

システムこのパネルでは音量調節だけで

play05:45

なく今後西武とかロードもできるようにし

play05:47

ていく予定ですふむついでにさっき作った

play05:49

ボタンはボタンアンダーバーシステムに

play05:51

変えておこうボタンアンダーバーシステム

play05:53

ではパネルの中にスライダー ui を

play05:55

設置し用パネルを右クリック

play05:57

ui スライダーそのスライダーを大体

play05:59

ここらへんに移動させる

play06:03

続いてこのスライダーの中にテキストを

play06:05

追加スライダーを右クリック

play06:08

ui テキストその敵須藤スライダーの左

play06:11

端に寄せる

play06:15

4

play06:17

こんなもんかそのテキストの文字を

play06:19

マスター音量と書き換えよう

play06:22

文字が見切れたこういう時は

play06:24

アレンジメントを中央揃えに指定ベスト

play06:26

フィットにチェックを入れるこれでよし

play06:28

ok そのスライダーの名前をスライダー

play06:30

アンダーバーマスターにしておいて

play06:32

スライダーアンダーバーマスターその

play06:34

スライダーを3個複製してコントロール d

play06:37

コントロール d コントロール d

play06:38

それぞれ名前をスライダー sfx

play06:40

スライダーミュージックスライダー

play06:43

システムに変えて

play06:46

そいつらをパネル内にきれいに並べてホイ

play06:48

ホイ

play06:50

ほい

play06:51

そしてスライダーの中のテキストも

play06:53

書き換える

play06:55

なくもっと

play06:57

000

play07:01

なんか文字サイズが不揃いで不格好だなぁ

play07:03

こういう時はテキストの max サイズ

play07:05

を小さくしてあげればいい4つのテキスト

play07:07

を同時選択してコントロールをしながら

play07:09

テキストをクリック

play07:11

そして max サイズの値を14に

play07:13

しようこれで綺麗になったふむこの

play07:15

スライダーコンポーネントのバリューと

play07:17

いう値これはスライダーのハンドルが左端

play07:20

に行くと0右端に行くと1になるこの値を

play07:22

さっき作ったボリューム変更関数と連動さ

play07:25

せるわけだなそういうことです角

play07:27

スライダーのボリュームの値は全部市にし

play07:29

ておこう

play07:31

できたぜそしてこのパネルを表示にする

play07:38

ではシステムパネルを管理するための

play07:40

スクリプトを作ろうシステム ui と

play07:42

いう名前のスクリプト作って

play07:46

システム ui そいつをメイン

play07:48

オブジェクトにアタッチして

play07:50

スクリプト開いてさっき作ったシステム

play07:53

関連のパーツ等をこのスクリプトにア

play07:55

タッチしていくよチャーそのための枠を

play07:57

作るぜ頼むぜーまずはネームスペースいう

play08:00

神具ユニディエンジン ui システム

play08:02

ボタン用の枠を作る public ボタン

play08:05

ぽたんアンダーバーシステム次にパネル

play08:07

public a 6オブジェクトパネル

play08:09

アンダーバーシステム阻止て各種音量調整

play08:11

スライダー public slider

play08:13

slider アンダーバーマスター

play08:17

スライダーアンダーバー music

play08:20

スライダーアンダーバー sfx っっ

play08:24

スライダーアンダーバーシステム

play08:27

できた税化したぜではシステムボタンを

play08:29

押したらパネルが開く仕組みを作ろうか

play08:31

at リスナーを使ってボタンの押下を

play08:33

感知する仕組みをつくってどうやるんだっ

play08:36

けまずボタンが押されたかどうかを判定

play08:38

するブール値を作るそうだったそうだった

play08:40

ぶーるープッシュボタンシステム=

play08:43

false そしてあっとリスナーを使っ

play08:44

てボタンを押したと同時にこのプールを

play08:46

ブルーに変えるだったなそうそうぽたん

play08:48

アンダーバーシステム音 click at

play08:51

リスナーラムダ演算子 pb システム=

play08:53

true そしてフレームの最後に忘れず

play08:56

に false に戻すそうしないと

play08:57

ボタン1回押したらそれから毎フレーム

play08:59

ボタン連打することになるからねできた

play09:02

よしではアップデート関数内にボタンが

play09:04

押されたらパネルを表示しパネルが押され

play09:06

た状態で再度ボタンを押したらパネルを

play09:08

表示にする処理書いて任せろもしボタンが

play09:11

押されたら

play09:12

パネルが表示されている場合はパネルを

play09:15

表示にするパネルが表示ならパネルを表示

play09:17

するできたぜではユニティーに戻って各

play09:20

パーツをアタッチし抵抗

play09:22

おい

play09:24

ほい

play09:26

ホイホイ

play09:29

ホイホイってきたアーチャーちゃんと動く

play09:32

かテストしてみよう

play09:36

000

play09:38

動いてるぜではこのスライダーと

play09:40

ボリュームを連動させる処理を各幼稚

play09:41

ステム ui スクリプトに戻ってホイ

play09:44

まずはマスター音量のスライダーからこれ

play09:46

も at リスナーだろうそうだけど

play09:47

スライダーの場合ボタンとちょっと書き方

play09:49

が異なるスライダー master on

play09:51

バリューチェンジどうあっどリスナー本場

play09:53

流チェンジどつまり値が変わったときに

play09:56

実行される処理仮想つまりハンドルが動い

play09:58

た時にこの格好の中に書かれた処理が実行

play10:01

されるつまりセットボリュームマスター

play10:03

関数だなそうということはここで

play10:05

オーディオマネージャーを参照しなきゃ

play10:06

いけないなというわけでオーディオ

play10:08

マネージャーをシングルトンかしてここ

play10:10

から参照できるようにしようシングルトン

play10:12

って何って方はステート工藤 ai の

play10:14

作り方動画の40分30秒あたりで解説し

play10:17

てますとりあえずオーディオマネージャー

play10:19

クラス開いたぜシングルトンの書き方覚え

play10:21

てる忘れたぜそんじゃあ改めて説明する

play10:24

まずはオーディオマネージャー方の

play10:25

static 変数を用意する

play10:27

public static オーディオ

play10:28

マネージャーインスタンス static

play10:30

変数はゲーム内に必ず1個しか実態が存在

play10:33

しないたとえオーディオマネージャーが2

play10:35

個以上あってもこのインスタンスという枠

play10:37

はかならず共通の1個のオーディオ

play10:39

マネージャーを指し示すこれ参照の概念が

play10:42

わからないとピンと来ないだろうなピンと

play10:44

こない方に必殺技を教えてあげようどんな

play10:46

技だよくわからんけど動きゃいいと強引に

play10:48

納得するお前の常套手段だな真面目な話

play10:52

マジでこれでなんとかなりますし理解わー

play10:54

とから追い付いてきますゲーム作りで一番

play10:56

やっちゃいけないことは手が止まって1

play10:58

カ所にとどまり続けてしまうことです

play11:00

とにかく書いて動かしてみるを徹底し

play11:02

ましょう本題に戻ろうウイ次はウェイク

play11:04

関数内に降格

play11:06

もしインスタンスが塗るじゃなければ自分

play11:08

を抹消するもシーンスタンスが塗るなら

play11:10

インスタンスに自分を代入するそして

play11:13

シーン切り替え時に自分を抹消しない今回

play11:15

のゲームではシーン切り替えを行わないの

play11:17

で本来この文は不要だけど一応これが

play11:20

シングルトンの書き方の一つですこれで

play11:22

ゲーム開始から終了までの間

play11:24

オーディオ manager .

play11:25

インスタンスは常に2個以上にならず確実

play11:28

に1個だけ存在する状態が保障される

play11:30

インスタンスが一個しかないということは

play11:32

どのインスタンスを参照するのか選ぶ必要

play11:34

もないつまりオーディオ manager

play11:36

.インスタンスを呼べばいつでもどこでも

play11:38

共通のオーディオマネージャーにアクセス

play11:40

できるってわけですすげー星一つ忘れちゃ

play11:42

いけないのがウェイク分の下に書いたこの

play11:45

オーディオソースの初期化処理これらは

play11:46

インスタンスが null の時だけ実行

play11:48

されるべき内容だからこうやって切り取っ

play11:51

てここに貼り付けるこれでシングルトン家

play11:54

官僚システム言う位に戻ってぽいてワーッ

play11:57

とリスナーの中でボリューム調整処理を

play11:59

書いていくわけですけどもが無駄演算子

play12:01

だろういやスライダーの場合それだと

play12:03

うまくいかないそうなのかなので今回は

play12:05

降格 delegate 中括弧

play12:08

のカッコの中に処理を書けばいい

play12:10

delegate ってなんだ分の中に

play12:12

関数を作りながら埋め込むような仕組みだ

play12:14

よそれラムダ式と何が違うんだほぼ同じだ

play12:17

よじゃあなんでラムダ式じゃダメなんだ魔

play12:19

理沙君はい動けばいいんだお前もわかって

play12:21

ないのかよってりゲートやラムダ式はまだ

play12:23

勉強不足なのでもっといい書き方はあるか

play12:26

もしれないですがとりあえず自分が知っ

play12:28

てるか気かたで進めさせていただきます

play12:30

チャーとりあえず処理を書く税頼む

play12:32

オーディオ manager .

play12:33

インスタンス

play12:34

セットボリュームマスターカッコこの格好

play12:36

の中にスライダーの値を入れればいいわけ

play12:39

だがその値はどうやってるんだー

play12:40

slider .ばいうで参照できる

play12:43

スライダー master . value

play12:46

同じ要領で他の3つのスライダーにも関数

play12:49

設定してっ

play12:52

play12:55

play12:56

voodoo

play12:59

できたよしこれで音量調節システムは

play13:01

出来上がったいよいよ応答鳴らすのかその

play13:03

前に音源を調達しなきゃね音源ってどう

play13:06

やって調達するんだてはちょっとお買い物

play13:08

行く母が妹の私が普段使わせていただい

play13:11

てる曲はすべてこのドーバーシンドローム

play13:13

さんで調達してる無料で商用利用可能な上

play13:16

にハイクオリティーな bgm が

play13:18

てんこ盛りだよそいつはすげぇ今回調達

play13:20

する bgm は2種類

play13:22

戦闘用の bgm と通常時の bgm

play13:24

ちなみに選曲はあらかじめしておいたよう

play13:27

ずらしく用意がいいなまず戦闘 bgm と

play13:29

して採用させていただいたのがこちら匠東

play13:32

さん作曲の大決戦を前が普段から愛用し

play13:35

てる曲だな非常に汎用性が高く

play13:38

めちゃくちゃちょうどいい感じがすごく

play13:39

ちょうどいいデビューが下手くそすぎて

play13:41

作曲者さんが可哀想だちなみに

play13:43

ダウンロードの仕方は超簡単仮面下の

play13:46

ダウンロードボタンを押してダウンロード

play13:47

ファイルボタンを押すだけ f 6

play13:49

次に通常時の bgm として今回使用さ

play13:52

せていただくのがこちらかまぼこ幸子さん

play13:54

作曲

play13:55

不穏な雰囲気この曲もワイルドマンで使わ

play13:58

せてもらったな曲名通りすごい不穏な

play14:00

雰囲気があってなおかつシュールな感じも

play14:02

してお気に入りいては以上の2曲を

play14:04

ダウンロードしてダウンロードしたぜ次は

play14:06

効果音を手に入れようこちらの効果音ラボ

play14:09

さんはかゆいところに手が届く無料の音源

play14:11

を大量に取り扱ってらっしゃいますこの

play14:13

サイトには死ぬほどお世話になってるな

play14:15

冗談抜きでこのサイトなかったらこの

play14:17

チャンネル積んだ誰かもしれないぐらいお

play14:19

世話になっておりますてどんなおとう調達

play14:22

するんだ今回は4種類まず攻撃したり攻撃

play14:25

を食らったりした時の実施って音あとは

play14:27

ボタンを押したときのお父さん種類ほど

play14:29

オッケまず10知っておとう手に入れよう

play14:31

このように効果音ラボさんはさまざまな

play14:33

音源をカテゴリ別に並べてくれてる今回は

play14:36

戦闘カテゴリの中のパンチの中から感じの

play14:38

ものを選ぼう

play14:43

なんかちょっと重たすぎるかなし他にも

play14:45

まだまだあるよ

play14:47

しっ

play14:49

をこれがいいショーパンチちょうどいい

play14:51

コミカルさだじゃあこちらをダウンロード

play14:53

次はボタンを押したときのシステム音か

play14:55

ボタンで検索してみてそしたらズラって

play14:58

候補が出てくるからこの中からいいのを

play15:00

探そう目移りするナマズは一番

play15:02

オーソドックスなボタンをクリックした時

play15:04

のカチって音とりあえずボタン押すたびに

play15:06

鳴らすならあんまり視聴強くない方がいい

play15:09

play15:10

これだいい感じに地味な音じゃあこれ

play15:13

ダウンロード次は組み合わせが成功した時

play15:15

や敵との戦闘に勝利したときなどいいこと

play15:18

が起こったときに鳴らすプルリンってを

play15:20

とっ

play15:21

そして組み合わせが失敗したときなどの部

play15:23

って音以上をダウンロードしてユニティー

play15:26

に戻ろうただ今では手に入れた音源を

play15:28

ユニティー内に取り込むオーディオという

play15:30

フォルダを新しく作って

play15:33

オーディオそしてその中に音源をドラッグ

play15:35

ドロップ

play15:38

ほいそしてそれぞれの名前を分かりやすく

play15:41

支えてあげよう

play15:43

play15:45

持っていません

play15:47

play15:49

おっ

play15:52

できたぜよしこれらの音源オーディオ

play15:55

マネージャーにアタッチするよまずは2

play15:57

種類の bgm の枠を作るパブリック

play15:59

オーディオクリップ bgm アンダー

play16:01

バーノーマルパブリックオーディオ

play16:03

クリップ bgm アンダーバーバトル

play16:05

ふむふむこんな感じであとはシステム音と

play16:08

効果音の枠も作って了解

play16:14

できたでは音源を当たっしよう

play16:19

play16:23

アタッチしたぜオーディオマネージャーに

play16:24

戻ってではおとう鳴らす関数を今から作っ

play16:27

ていくよいいぞ最初に作るのはシステム音

play16:30

を鳴らす関数

play16:31

関数名はプレイシステムにしよう

play16:34

public void プレイシステム

play16:36

引数はオーディオクリップを1個音の

play16:38

鳴らし方はいたって簡単オーディオソース

play16:40

.プレイクリップ at ポイント引数は

play16:43

3つ

play16:44

右から音声クリップおとう鳴らす市の情報

play16:47

最後に音量まずは音声クリップ次に位置

play16:50

座標か今回のゲームでは市関係ないから

play16:52

普通に減点座標でいいトランスフォー

play16:54

home .ポジション最後に音量か音量

play16:56

はシステム音量とマスター音量を

play16:58

掛け合わせた数字システムボリューム

play17:00

パーセントかけるマスターボリューム

play17:02

パーセントでは早速おとうならしてみよう

play17:05

メインクラス開いてメインクラスまずは

play17:07

メインコマンドステートの最初に音を

play17:09

鳴らしてみようこの state エンター

play17:11

処理の最後にさっき作った関数を実行する

play17:13

オーディオマネーじゃあインスタンス

play17:15

プレイシステムオーディオマネージャー

play17:17

インスタンスシステムクリックこれで

play17:19

メインコマンドに移行するたびにカチって

play17:21

音が鳴るわけかこの時くれぐれも

play17:23

state エンター処理の格好の外に

play17:25

書かないでねじゃないと毎フレーム

play17:27

クリック音がなってしまうよ業界いっ

play17:29

ちゃあ同じ処理を書くステートの最初に

play17:31

書いていって次は敵と遭遇ステートの最初

play17:34

おっ

play17:35

その次は戦闘コマンド state

play17:39

次は攻撃ステーとか攻撃 state で

play17:41

はクリック音ではなく殴った音を入れる

play17:43

から飛ばす

play17:45

同様に敵からの攻撃 state も

play17:47

飛ばす

play17:48

的に勝利した state ここでは成功

play17:50

したと機能等ならそうか

play17:54

システムアンダーバーサクセス次

play17:56

インベントリを開く state ここで

play17:58

もクリック音鳴らす

play18:01

さらにこの中では使うボタンが押された

play18:03

タイミングでもおとうならそう

play18:08

ここではステートの変化が無いからかそう

play18:11

そう次アイテム組み合わせ state

play18:13

このステートの最初にもクリック音を

play18:15

鳴らす

play18:17

そして組み合わせが成功したら成功音失敗

play18:20

したら失敗音を鳴らす

play18:25

play18:26

play18:27

続いて部屋の選択 state ここでも

play18:30

クリック音を鳴らす

play18:33

その次の部屋移動ステーとでも同様

play18:37

次の何も見つからなかった state で

play18:39

は失敗 on

play18:41

アイテムを見つけた state では

play18:43

成功音を鳴らす

play18:47

これで1回動かしてみるかよしきたああ

play18:50

あああ

play18:55

となってる

play19:04

なんかを通る生なじゃあ音量下げ8

play19:15

スライダーもちゃんと機能してるいい感じ

play19:18

だねでは同じ要領で効果音を鳴らす関数

play19:20

作るよ仕組みはシステム音と全く同じ業界

play19:23

public void プレイ

play19:25

sound effects

play19:28

オーディオソースプレイクリップ at

play19:30

ポイントクリップ transform

play19:32

ポジション効果音ボリュームかける

play19:34

マスターボリュームよしじゃあ攻撃

play19:35

state 内でこの関数実行しようまず

play19:38

は自分から敵への攻撃 state

play19:40

オーディオマネージャーインスタンス

play19:42

プレイ sound effect

play19:43

audio マネージャーインスタンス

play19:45

sound effects アタック敵

play19:47

からの攻撃する8でも同じものを書く

play19:53

じゃあ再度テスト

play19:56

4位

play20:00

4

play20:04

敵からの攻撃と自分の攻撃で音変えても

play20:07

いいかもなあそこらへんは各自お好みで

play20:09

調整してください一応成功だぜじゃあ最後

play20:12

に bgm ならそうよっしゃー

play20:13

public void プレイ

play20:15

music

play20:18

音楽に関しては専用のオーディオソースを

play20:20

用意したからそのオーディオソースの

play20:22

クリップっては国音源をアタッチして

play20:24

プレイという関数を実行する music

play20:27

ソースクリップ=クリップミュージック

play20:29

そうスプレーではメインクラス開いて

play20:32

メインコマンドステートに入った時点で

play20:34

通常 bgm

play20:35

敵との遭遇ステートに入った時点で戦闘 b

play20:38

cm を鳴らそう

play20:51

できたアーティスト

play20:55

を音がなってううるせえええええちょっと

play20:58

先に音量調節しよう

play21:09

ちゃんとなってるがうん

play21:14

おいメインステートに戻るたびに曲が

play21:16

リセットされてしまうぞメインステートに

play21:18

入るたびにクリップ入れ直してプレイ関数

play21:20

実行するからだねもっと工夫しよう

play21:23

もしアタッチしようとしてるクリップが

play21:25

現在アタッチされてるクリップと同じだっ

play21:28

たら何もせずに終了

play21:30

これでいちいち途切れなくなるということ

play21:33

でラスト

play21:34

play21:34

goo

play21:35

[音楽]

play21:37

play21:43

goo

play21:45

グー

play21:49

play21:53

いい感じ敵と戦ってみよう

play21:57

[音楽]

play21:58

ちゃんと切り替わったなぁ敵を倒してみて

play22:02

[音楽]

play22:05

なんだか bgm の切り替わりが急

play22:07

すぎるなそこなんですよ問題は今回はここ

play22:10

で区切って続きは次回にしよう次回は再生

play22:12

している bgm をゆっくりフェード

play22:14

アウトさせるのと同時に次の bgm を

play22:16

ゆっくりフェードインさせることで滑らか

play22:18

に曲を遷移させる仕組みをつくるよう

play22:20

楽しみだぜというわけで今回も最後までご

play22:22

視聴ありがとうございましたもし

play22:24

よろしければチャンネル登録お待ちして

play22:26

おります次回も必ず見てくださいまたねー

Rate This

5.0 / 5 (0 votes)

Related Tags
Game DevelopmentAudio SystemUnitySound TutorialVolume ControlAudio ManagerScriptingBGMSFXUI Sliders