3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UniRX使ってUnityでずっと音を垂れ流す

Last updated at Posted at 2019-01-31

// Intervalで垂れ流す処理
IObservable<long> PlaySeObservable(double _time)
{
    return Observable.Interval(TimeSpan.FromSeconds(_time));
}

音の再生のくわしいやり方とかは、
他で調べてね


[SerializeField]
private AudioClip audioClip;
[SerializeField]
private AudioSource audioSource;

void Start()
{
    float seTime = audioClip.length;
    PlaySeObservable(seTime).Subscribe(_ => {
        // 垂れ流せ!
        audioSource.PlayOneShot(audioClip);
    });
}

ずっと垂れ流しでうるせぇなぁと思うので、


bool flag;
IObservable<long> PlaySeObservable(double _time)
{
    return Observable.Interval(TimeSpan.FromSeconds(_time)).TakeWhile(_ => !flag);
}

なんかのフラグをTrueにしたら、とめるようにする


// onclickのEventとか
public void OnClick(){
    // 止まれ
    flag = true;
}

しかしながら、
ループ再生したいなら、
AudioSourceコンポーネントのloopオプションにチェックいれて、
ループ設定すればいいだけですね

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?