119 lines
3.5 KiB
C#
119 lines
3.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SaveLoadPrep : MonoBehaviour
|
|
{
|
|
public SaveLoadData saveLoadData;
|
|
public Text txtPath;
|
|
|
|
public Button saveBtn;
|
|
public Button loadBtn;
|
|
|
|
public GameObject saveContentPanel;
|
|
public Button confirmSave;
|
|
|
|
public GameObject loadContentPanel;
|
|
public Transform loadContentParent;
|
|
public GameObject loadContentItemPrefab;
|
|
|
|
|
|
public static SaveLoadPrep instance;
|
|
void Awake(){
|
|
saveBtn.onClick.AddListener(Save);
|
|
loadBtn.onClick.AddListener(ShowLoadContent);
|
|
instance =this;
|
|
|
|
|
|
}
|
|
|
|
void Start(){
|
|
if(PrepConnector.saveLoadData != null){
|
|
saveLoadData = PrepConnector.saveLoadData;
|
|
Debug.Log("Loading music file at " + saveLoadData.musicFile);
|
|
MusicLoader.instance.OnFileSelected(saveLoadData.musicFile, true);
|
|
PrepController.instance.SetMarkers(saveLoadData.hits);
|
|
}
|
|
}
|
|
|
|
|
|
public void ShowLoadContent(){
|
|
loadContentPanel.SetActive(true);
|
|
|
|
PopulateContentPanel();
|
|
}
|
|
|
|
public void OnFileSelected(string file){
|
|
loadContentPanel.SetActive(false);
|
|
LoadFile(Application.persistentDataPath + file);
|
|
}
|
|
|
|
public void LoadFile(string file){
|
|
string fileTxt = File.ReadAllText(file);
|
|
saveLoadData = JsonUtility.FromJson<SaveLoadData>(fileTxt);
|
|
|
|
MusicLoader.instance.OnFileSelected(saveLoadData.musicFile, true);
|
|
PrepController.instance.SetMarkers(saveLoadData.hits);
|
|
}
|
|
|
|
|
|
public void Save(){
|
|
saveLoadData = new SaveLoadData(txtPath.text);
|
|
List<float> unsortedHits = new List<float>();
|
|
foreach(GameObject marker in PrepController.markers){
|
|
unsortedHits.Add(marker.transform.position.x / AudioAnalyzer.instance.zoomingYMult);
|
|
}
|
|
|
|
saveLoadData.hits = Helpers.BubbleSort(unsortedHits);
|
|
|
|
saveContentPanel.SetActive(true);
|
|
string fileName = txtPath.text;
|
|
if(fileName.Contains("/")){
|
|
string[] bits = fileName.Split("/");
|
|
fileName = bits[bits.Length-1];
|
|
}else {
|
|
string[] bits = fileName.Split("\\");
|
|
fileName = bits[bits.Length-1];
|
|
}
|
|
fileName = fileName.Split('.')[0];
|
|
saveContentPanel.GetComponentInChildren<InputField>().text = fileName;
|
|
}
|
|
|
|
public void SaveConfirmed(){
|
|
saveContentPanel.SetActive(false);
|
|
string fileName = Application.persistentDataPath + "/" + saveContentPanel.GetComponentInChildren<InputField>().text + ".json";
|
|
Debug.Log("Saving to " + fileName);
|
|
File.WriteAllText(fileName , JsonUtility.ToJson(saveLoadData));
|
|
}
|
|
|
|
public void PopulateContentPanel(){
|
|
for(int i=0; i < loadContentParent.childCount; i++){
|
|
|
|
Destroy(loadContentParent.GetChild(i).gameObject);
|
|
}
|
|
|
|
string[] files =Directory.GetFiles(Application.persistentDataPath);
|
|
foreach(string file in files){
|
|
if(!file.Contains(".json")){continue;}
|
|
|
|
GameObject newItem = Instantiate(loadContentItemPrefab, loadContentParent);
|
|
newItem.AddComponent<OnContentFileSelected>();
|
|
newItem.GetComponentInChildren<Text>().text = file.Replace(Application.persistentDataPath, "");
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
[System.Serializable]
|
|
public class SaveLoadData{
|
|
public string musicFile;
|
|
public List<float> hits;
|
|
|
|
public SaveLoadData(string file){
|
|
musicFile = file;
|
|
hits = new List<float>();
|
|
}
|
|
} |