SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using UniRx;
  3. using UnityEngine;
  4.  
  5. namespace KotonohaAnone.Presenter
  6. {
  7.     public class BeatTransmitter : MonoBehaviour
  8.     {
  9.         [SerializeField] private AudioSource audioSource;
  10.         [SerializeField] private float bpm;
  11.  
  12.         private readonly Subject<Unit> beatSubject = new Subject<Unit>();
  13.  
  14.         //  æ›²ã®ä½•ãƒ“ート目か(前Update時)
  15.         private int previousBeatCount = 0;
  16.  
  17.         private void Update()
  18.         {
  19.             float beatTime = 60f / bpm;
  20.  
  21.             int currentBeatCount = Mathf.FloorToInt(audioSource.time / beatTime);
  22.             if(currentBeatCount != previousBeatCount)
  23.             {
  24.                 beatSubject.OnNext(Unit.Default);
  25.             }
  26.  
  27.             previousBeatCount = currentBeatCount;
  28.         }
  29.  
  30.         private void OnDestroy()
  31.         {
  32.             beatSubject.Dispose();
  33.         }
  34.  
  35.         //  ãƒ“ートを観測する
  36.         //  1拍ごとに発行される
  37.         public IObservable<Unit> ObserveBeat()
  38.         {
  39.             return beatSubject;
  40.         }
  41.     }
  42.  
  43.     //  åˆ©ç”¨å´
  44.     class Usage : MonoBehaviour
  45.     {
  46.         void SomeMethod()
  47.         {
  48.             BeatTransmitter beat = GetComponent<BeatTransmitter>();
  49.             beat.ObserveBeat()
  50.                 .Subscribe(_=>
  51.                 {
  52.                     //  ãƒ“ートごとに何かする
  53.                 })
  54.                 .AddTo(this);
  55.         }
  56.     }
  57.  
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top