Created
May 13, 2016 03:25
-
-
Save iwadon/40ea2f04163135649db7f71d9a64e6d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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