Dunno what language you need or prefer, but I use JavaScript (UnityScript)...so...yeah. The first method uses StartCoroutine: this is a good/great? workaround if you need to use a parameter(s).
public var m_complainTime : float;
public var m_complainClip : AudioClip;
private var _audio : AudioSource;
function Start ()
{
_audio = GetComponent(AudioSource);
StartCoroutine(PlayDelayedClip(m_complainTime, m_complainClip));
}
public function PlayDelayedClip(time : float, clip : AudioClip)
{
yield WaitForSeconds(time);
_audio.PlayOneShot(clip);
}
However, when using JavaScript it's not necessary to use StartCoroutine, so you can call the function as so:
function Start ()
{
_audio = GetComponent(AudioSource);
PlayDelayedClip(m_complainTime, m_complainClip);
}
Or you could do it this way, though using Update() is not preferred since it runs every frame:
public var m_complainTime : float;
public var m_complainClip : AudioClip;
private var _hasPlayedClip : boolean;
private var _timer : float;
private var _audio : AudioSource;
function Start ()
{
_audio = GetComponent(AudioSource);
_hasPlayedClip = false;
_timer = 0;
}
function Update ()
{
if (!_hasPlayedClip)
{
PlayDelayedClip(m_complainTime, m_complainClip);
}
}
public function PlayDelayedClip(time : float, clip : AudioClip)
{
if (_timer < time)
{
_timer += Time.deltaTime;
Debug.Log(_timer);
}
else
{
_timer = 0;
_hasPlayedClip = true;
_audio.PlayOneShot(m_complainClip);
}
}
Hope this helps. This can easily be translated into C# as well...
↧