0
1

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】ゲームジャム向けSE再生クラス

0
Last updated at Posted at 2026-08-23

効果音さえ再生できればいい人向けのSePlayerを用意しました。
効果音再生ライブラリを導入するほどでもない、小規模な用途向けのシンプルな実装です。
SePlayer はプロジェクトに入れるだけで使えます。
audioClip はインスペクタから参照するか Resources/Addressablesで読み込んでください。
実装に問題があれば連絡していただけると助かります。

利用例.cs
audioClip.PlayOneShot(); // 再生
audioClips.PlayOneShot(0.5f); // ランダムに1つ再生。音量は0.5

コード

SePlayer.cs
using UnityEngine;

public sealed class SePlayer : MonoBehaviour
{
    private static AudioSource _audioSource;

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    private static void Initialize()
    {
        if (_audioSource != null) return;
        var go = new GameObject(nameof(SePlayer));
        _audioSource = go.AddComponent<AudioSource>();
        DontDestroyOnLoad(go);
    }

    public static void PlayOneShot(AudioClip clip, float volume = 1f)
    {
        if (clip == null) return;
        _audioSource.PlayOneShot(clip, volume);
    }
    public static void PlayOneShot(float volume, params AudioClip[] clips)
    {
        if (clips == null || clips.Length == 0) return;
        PlayOneShot(clips[Random.Range(0, clips.Length)], volume);
    }

    public static void Pause() => _audioSource.Pause();
    public static void UnPause() => _audioSource.UnPause();
    public static void Stop() => _audioSource.Stop();
}

public static class SePlayerExtensions
{
    public static void PlayOneShot(this AudioClip clip, float volume = 1f)
        => SePlayer.PlayOneShot(clip, volume);
    public static void PlayOneShot(this AudioClip[] clips, float volume = 1f)
        => SePlayer.PlayOneShot(volume, clips);
}
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?