public void OnePlay(string name)

{

Sound s = Array.Find(sounds, sound => sound.name == name);

s.source.PlayOneShot(name,0.3f);

if (s == null) return;

}


์†Œ๋ฆฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ ๋‹ด์— ๊ทธ ์ค‘์— ์ด๋ฆ„ ๊ฐ™์€๊ฑฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ˜•์‹์œผ๋กœ ์ฝ”๋“œ ์งฐ์Œ.

๊ทธ๋Ÿฐ๋ฐ ํ•œ๋ฒˆ๋งŒ ์žฌ์ƒํ•˜๋ฉด ๋˜๊ฒŒ๋” PlayOneShot()๋ฅผ ์ด์šฉํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ

๋งค๊ฐœ๋ณ€์ˆ˜๋ช…์œผ๋กœ ๊ทธ๋Œ€๋กœ ๋ฐ›์•„์˜ค๊ณ ์‹ถ์ง€๋งŒ audioclip๊ฐ€ ์•„๋‹ˆ๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ์ € ์•ˆ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•  ์ˆ˜ ์—†๋‚˜?




ํ’€ ์ฝ”๋“œ ์˜ค๋””์˜ค ๋งค๋‹ˆ์ €

using UnityEngine;

using System;

using UnityEngine.Audio;


public class AudioManager : MonoBehaviour

{

public Sound[] sounds;


public static AudioManager instance;

void Awake()

{

if (instance == null) instance = this; // ์˜ค๋””์˜ค๋งค๋‹ˆ์ €๊ฐ€ ์—†์œผ๋ฉด

else

{

Destroy(gameObject);

return;

}

DontDestroyOnLoad(gameObject); // ์ค‘๋ณตํ˜ธ์ถœ ๋ฐฉ์ง€


foreach(Sound s in sounds)

{

s.source = gameObject.AddComponent<AudioSource>();

s.source.clip = s.clip;

s.source.volume = s.volume;

s.source.pitch = s.pitch;

s.source.loop = s.loop;

s.source.outputAudioMixerGroup = s.mix; // ๋ฏน์Šค

}

}

void Start()

{

Play("ST1BG");

}

public void Play(string name)

{

Sound s = Array.Find(sounds, sound => sound.name == name);

// ์‚ฌ์šด๋“œ ๋ฐฐ์—ด ์•ˆ์—์„œ ์‚ฌ์šด๋“œ ์ด๋ฆ„๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ๊ฐ™์€๊ฑธ ์ฐพ๋Š”๋‹ค.

if (s == null) return;

s.source.Play();

}

}


ํ’€ ์ฝ”๋“œ ์‚ฌ์šด๋“œ

using UnityEngine;

using UnityEngine.Audio;


[System.Serializable]

public class Sound

{

public string name; // ์ด๋ฆ„

public AudioClip clip; // ํด๋ฆฝ


[Range(0f, 1f)]

public float volume; // ๋ณผ๋ฅจ


[Range(.1f, 3f)]

public float pitch; // ํ”ผ์น˜


public bool loop; // ๋ฐ˜๋ณต ์—ฌ๋ถ€

[HideInInspector]

public AudioSource source;

public AudioMixerGroup mix;

public AudioClip clipsource;

}


์ด ์ƒํ™ฉ์ด์•ผ