1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unityで効果音を鳴らす

1
Posted at

Unityで音を鳴らすには「AudioSource」を使います。基本は「AudioClipをAudioSourceに設定して再生」するだけです。効果音(SE)やBGMを鳴らすときに必須のコンポーネントです。

AudioSourceの基本構造

AudioClip

再生する音声ファイル(.wav, .mp3, .ogg, .aifなど対応)

AudioSource

音を鳴らす「プレイヤー」。ゲームオブジェクトにアタッチして使う

AudioListener

音を「聞く」役割。通常はMain Cameraに自動で付いている

AudioSourceの設定項目(代表的なもの)

Audio Clip : 再生する音声ファイル
Play On Awake : シーン開始時に自動再生するか
Loop : 繰り返し再生するか
Volume : 音量調整
Spatial Blend : 2D音か3D音か(3Dにすると距離で音量が変化)
Mute : ミュート設定

スクリプトでの使い方

using UnityEngine;

public class SoundTest : MonoBehaviour
{
    public AudioSource audioSource;
    public AudioClip clip;

    void Start()
    {
        // BGM再生
        audioSource.clip = clip;
        audioSource.Play();

        // 効果音再生(重ねて鳴らせる)
        audioSource.PlayOneShot(clip);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            audioSource.Stop();   // 停止
        }
    }
}

Play() : 登録したAudioClipを再生
PlayOneShot() : 効果音用。複数同時再生可能
Stop() : 再生停止(次回は頭から再生)
Pause()/UnPause() : 一時停止と再開

使い分けのポイント

BGM → Play()+Loopを有効にする
効果音(SE) → PlayOneShot()を使う
環境音や立体音響 → Spatial Blendを3Dにして距離減衰を設定

まとめ

AudioSourceは「音を鳴らす装置」
AudioClipを設定してPlay/PlayOneShotで再生
BGMはループ、効果音はワンショット
3D設定で距離や方向による音量変化も可能

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?