public static void Play(string name, Vector3 SoundPos = new Vector3()) { Sound_SO s = Array.Find(Sounds.ToArray(), sound => sound.name == name); if (s == null) { Debug.Log("Sound with the name " + name + " not found"); return; } if (s.isMusic) { if (s.source != null) { s.source.Play(); } else { AudioSource src = new GameObject().AddComponent <AudioSource>(); src.clip = s.clip; src.volume = s.volume; src.loop = s.loop; s.source = src; //Sounds.Add(s); s.source.Play(); } } else { GameObject soundObj = GameObject.Instantiate(soundObjPrefabs); soundObj.name = name + "_Sound"; soundObj.transform.position = SoundPos; AudioSource src = soundObj.AddComponent <AudioSource>(); SoundObject obj = soundObj.GetComponent <SoundObject>(); src.clip = s.clip; src.volume = s.volume; src.loop = s.loop; obj.Init(s.clip.length + 0.3f); src.Play(); } }
public static void Stop(string name) { Sound_SO s = Array.Find(Sounds.ToArray(), sound => sound.name == name); if (s == null) { Debug.Log("Sound with the name " + name + " not found"); return; } if (s.source != null) { s.source.Stop(); } }