Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System.Collections;
  2. using System.IO;
  3. using UnityEngine;
  4.  
  5. namespace RhythmTool.Examples
  6. {
  7.     /// <summary>
  8.     /// The FileSelector shows a browser and imports a file, before analyzing and playing it.
  9.     /// </summary>
  10.     public class FileSelector : SongSelector
  11.     {
  12.         public Browser browser;
  13.  
  14.         public AudioImporter importer;
  15.  
  16.         private string cachePath;
  17.         private string rhythmDataPath;
  18.  
  19.         void Awake()
  20.         {
  21.             cachePath = Path.Combine(Application.persistentDataPath, "RhythmData");
  22.  
  23.             if (!Directory.Exists(cachePath))
  24.                 Directory.CreateDirectory(cachePath);
  25.  
  26.             browser.FileSelected += OnFileSelected;
  27.             importer.Loaded += OnLoaded;
  28.         }
  29.  
  30.         private void OnFileSelected(string path)
  31.         {
  32.             //Hide the browser.
  33.             browser.gameObject.SetActive(false);
  34.  
  35.             //Start importing the selected song.
  36.             importer.Import(path);
  37.         }
  38.  
  39.         private void OnLoaded(AudioClip audioClip)
  40.         {
  41.             //Clean up old resources.
  42.             Destroy(player.audioClip);
  43.             Destroy(player.rhythmData);
  44.  
  45.             //Assign AudioClip
  46.             player.audioClip = audioClip;
  47.  
  48.             rhythmDataPath = Path.Combine(cachePath, audioClip.name + ".rthm");
  49.  
  50.             if (File.Exists(rhythmDataPath))
  51.             {
  52.                 //If a file is found, load it and start playing the song.
  53.                 string json = File.ReadAllText(rhythmDataPath);
  54.                 RhythmData rhythmData = ScriptableObjectSerializer.FromJson<RhythmData>(json);
  55.                                
  56.                 player.rhythmData = rhythmData;
  57.  
  58.                 StopAllCoroutines();
  59.                 StartCoroutine(PlayOnNextFrame());        
  60.             }
  61.             else
  62.             {
  63.                 //If no RhythmData file was found, analyze the song and save the results when the analysis is done.
  64.                 RhythmData rhythmData = analyzer.Analyze(audioClip);
  65.                 player.rhythmData = rhythmData;
  66.  
  67.                 StopAllCoroutines();
  68.                 StartCoroutine(SaveRhythmData());
  69.             }
  70.         }
  71.  
  72.         public override void NextSong()
  73.         {
  74.             base.NextSong();
  75.  
  76.             //Show the browser.
  77.             browser.gameObject.SetActive(true);
  78.         }
  79.  
  80.         IEnumerator PlayOnNextFrame()
  81.         {
  82.             yield return null;
  83.             player.Play();
  84.         }
  85.  
  86.         IEnumerator SaveRhythmData()
  87.         {
  88.             while (!analyzer.isDone)
  89.                 yield return null;
  90.  
  91.             RhythmData rhythmData = analyzer.rhythmData;
  92.  
  93.             string json = ScriptableObjectSerializer.ToJson(rhythmData);
  94.             File.WriteAllText(rhythmDataPath, json);
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement