Skip to content

Instantly share code, notes, and snippets.

@iwadon
Created May 13, 2016 03:25
Show Gist options
  • Save iwadon/40ea2f04163135649db7f71d9a64e6d9 to your computer and use it in GitHub Desktop.
Save iwadon/40ea2f04163135649db7f71d9a64e6d9 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class AudioObjectController : MonoBehaviour
{
public AudioClip _audioClip;
// Use this for initialization
void Start()
{
//PlayAudio1();
PlayAudio2();
}
// コルーチンで再生停止させる。
void PlayAudio1()
{
var src = gameObject.AddComponent<AudioSource>();
src.clip = _audioClip;
src.time = 3.0f;
src.Play();
StartCoroutine(StopAudioSource(2, src));
}
IEnumerator StopAudioSource(float time, AudioSource src)
{
yield return new WaitForSeconds(time);
src.Stop();
}
// SetScheduledEndTime()で再生停止する時刻を指定する。
void PlayAudio2()
{
var src = gameObject.AddComponent<AudioSource>();
src.clip = _audioClip;
src.time = 3.0f;
src.Play();
src.SetScheduledEndTime(AudioSettings.dspTime + 2.0f); // Play()よりあとに書く必要あり?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment