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;
}
์ด ์ํฉ์ด์ผ
Resouces load ์ฐ๋ฉด ๋๊ธดํ๋๋ฐ..
๊ทธ๊ฒ ์๋๋ค ์ ๋ฐ์์ผ๋ก ํ ๊ฑฐ๋ฉด ๋น์ฐํ ์ค๋์คํด๋ฆฝ ๋ค์ด์๋ ๋ฐฐ์ด๋ ์์ด์ผ ๋๋๊ฑฐ ์๋? ๋์ฒด ๋จธ์ง?
ํน์ ๋ช ํํ๊ฒ ์ด๋ป๊ฒ ์? ์ด๊ฑฐ ์ ์๋ง ์ฝ๋ ํ๋ก ์ฌ๋ฆดํ ๋๊น ๋ด์ค์ ์์?
๊ทผ๋ฐ ์ฝ๋๊ฐ ์ข ์ด์ํด ์ null ์ฒดํฌ๋ฅผ ๋ง์ง๋ง์ ํด?
ํจ์ ์ฒซ๋ถ๋ถ์ ํ๋ ๋ง์ง๋ง ๋ถ๋ถ์ ํ๋ ์ฐจ์ด ์๋๊ฑธ๋ก ์๊ณ ์์ด์..
Sound ํด๋์ค์ ํด๋ฆฝ ์ด๋ฏธ ์์ผ๋๊น ๊ทธ๋๋ก ์ฐ๋ฉด ๋์ง ์์? public void OnePlay(string name) { Sound s = Array.Find(sounds, sound => sound.name == name); s.source.PlayOneShot(s.clip,0.3f); } ์ด๋ฌ์ผ?
์ํ๋ ๋ต๋ณ์ด์์ ใ ใ ์ฝ๋ ๊ฑฐ์ ๋ค ์ง ๊ฑฐ๊ฐ์๋ฐ ๋ด๊ฐ ๋ญ ํ๋๋ฅผ ๋์ณ๊ฐ์ง๊ณ ๋ชปํ๊ฑฐ๋ NAME๊ฐ ์๋๋ผ S.CLIP์ฐ๋๊ฒ ๋ต์ด์๋ค