*
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class RandomSoundPlayer : MonoBehaviour {
|
||||
public AudioClip[] clips;
|
||||
public AudioSource target;
|
||||
public GameObject newObjectPrototype;
|
||||
public bool forceInstantinate;
|
||||
|
||||
public void Play(bool newObject = false){
|
||||
if (forceInstantinate) {
|
||||
newObject = true;
|
||||
}
|
||||
|
||||
if (!newObject && (target == null)) {
|
||||
Debug.Log("Can't play because audio source is null", this);
|
||||
return;
|
||||
}
|
||||
if (clips == null || clips.Length == 0) {
|
||||
Debug.Log("Can't play because clips are null", this);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
var sound = clips[Random.Range(0, clips.Length - 1)];
|
||||
if (newObject) {
|
||||
GameObject g;
|
||||
AudioSource source;
|
||||
|
||||
|
||||
|
||||
if (newObjectPrototype != null) {
|
||||
g = Instantiate(newObjectPrototype);
|
||||
g.transform.parent = null;
|
||||
source = g.GetComponent<AudioSource>();
|
||||
source.clip = clips[0];
|
||||
source.playOnAwake = true;
|
||||
g.SetActive(true);
|
||||
}
|
||||
else {
|
||||
g = new GameObject("SingleShotTmpSound");
|
||||
source = g.AddComponent<AudioSource>();
|
||||
source.PlayOneShot(sound);
|
||||
}
|
||||
g.transform.position = transform.position;
|
||||
|
||||
Destroy(g, sound.length);
|
||||
}
|
||||
else {
|
||||
if (target != null) {
|
||||
target.PlayOneShot(sound);
|
||||
}
|
||||
else {
|
||||
Debug.LogWarning("Audio source not set!", this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user