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

More than 3 years have passed since last update.

Unity の SoundManager 作ってみた

Posted at

機能

  • サウンド再生機能一箇所に集約して、簡単に利用できる
  • エディタ内で簡単SEを入れることができる
  • 各サウンド個別音量設定できる
  • AudioSource不足の場合自動的に1個生成する
  • AudioSource最大数設定できる

コード

SoundManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// サウンドデータ定義
[System.Serializable]
public class SoundData
{
    // 名前
    public string name;
    // ソース
    public AudioClip clip;
    // 再生時間
    public float playedTime;
    // 音量
    [Range(0.0f, 1.0f)]
    public float volume = 1.0f;
}

public class SoundManager : UnitySingleton<SoundManager>
{

    // 最大同時再生数
    [Range(5,25)]
    public int MaxSoundTrack = 10;

    [SerializeField]
    private List<SoundData> soundDatas;

    private AudioSource[] audioSourceList;
    
    // 検索用辞書
    private Dictionary<string, SoundData> soundDictionary = new Dictionary<string, SoundData>();

    private void Awake()
    {

        audioSourceList = new AudioSource[MaxSoundTrack];

        for (var i = 0; i < audioSourceList.Length; ++i)
        {
            audioSourceList[i] = gameObject.AddComponent<AudioSource>();
        }

        foreach (var soundData in soundDatas)
        {
            soundDictionary.Add(soundData.name, soundData);
        }
    }
    
    // 使っていないAudioSourceを取得
    private AudioSource GetUnusedAudioSource()
    {
        for (var i = 0; i < audioSourceList.Length; ++i)
        {
            if (!audioSourceList[i].isPlaying) return audioSourceList[i];
        }

        return null;
    }
    
    // 再生(内部用)
    private void Play(AudioClip clip,float volume)
    {
        var audioSource = GetUnusedAudioSource();
        if (audioSource == null) return; 
        audioSource.clip = clip;
        audioSource.volume = volume;
        audioSource.Play();
    }
    
    // 再生(名前)
    public void Play(string name)
    {
        if (soundDictionary.TryGetValue(name, out var soundData))
        {
            if (Time.realtimeSinceStartup - soundData.playedTime < soundData.clip.length) return;
            soundData.playedTime = Time.realtimeSinceStartup;
            Play(soundData.clip,soundData.volume);
        }
    }

    // 再生(名前,間隔時間)
    public void Play(string name, float interval_time)
    {
        if (soundDictionary.TryGetValue(name, out var soundData))
        {
            if (Time.realtimeSinceStartup - soundData.playedTime < interval_time) return;
            soundData.playedTime = Time.realtimeSinceStartup;
            Play(soundData.clip,soundData.volume);
        }
    }
}

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