using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using UnityEngine; using UnityEngine.UI; public class UISaveLoadPanel : MonoBehaviour { public ScrollRect ScrollScene; public ScrollRect ScrollPose; public Button ButtonScene; public Button ButtonPose; public List ScenePresetEntries = new List(); public List PosePresetEntries = new List(); public static string SavePoseDirectory => ModelViewerSettings.Get("SavePoseDirectory", new ModelViewerSettings.Setting(Application.dataPath + "/../Poses/", ModelViewerSettings.SettingType.FolderPath)); public static string SaveSceneDirectory => ModelViewerSettings.Get("SaveSceneDirectory", new ModelViewerSettings.Setting(Application.dataPath + "/../Scenes/", ModelViewerSettings.SettingType.FolderPath)); private void Awake() { InitPoses(); InitScenes(); } public void AddPose(UIPoseContainer pose) { PosePresetEntries.Add(pose); } public void AddScene(UISceneContainer scene) { ScenePresetEntries.Add(scene); } public void OnPoseButton() { ButtonScene.interactable = true; ButtonPose.interactable = false; ScrollScene.gameObject.SetActive(false); ScrollPose.gameObject.SetActive(true); } public void OnSceneButton() { ButtonScene.interactable = false; ButtonPose.interactable = true; ScrollScene.gameObject.SetActive(true); ScrollPose.gameObject.SetActive(false); } public void OnDeleteButton() { ModelViewerMain.GetInstance().EmptySceneDefault(); } public bool GetPoseByName(string name, out UIPoseContainer pose) { pose = PosePresetEntries.FirstOrDefault(p => p.PoseName == name); return pose != null; } public bool GetSceneByName(string name, out UISceneContainer scene) { scene = ScenePresetEntries.FirstOrDefault(p => p.SceneData.Filename == name); return scene != null; } private void InitPoses() { string path = SavePoseDirectory; if(!Directory.Exists(path)) { Directory.CreateDirectory(path); } foreach (var fileName in Directory.GetFiles(path).Where(f => f.EndsWith(".pose"))) { try { string fName = Path.GetFileNameWithoutExtension(fileName); var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }; var bc = JsonConvert.DeserializeObject(File.ReadAllText(fileName), settings); UIPoseContainer.CreateNew(fName, bc, false); } catch (Exception e) { Debug.LogWarning(e); Error.Log(Color.red, "Loading " + fileName + " failed!"); } } } private void InitScenes() { string path = SaveSceneDirectory; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } List scenes = new List(); foreach (var fileName in Directory.GetFiles(path).Where(f => f.EndsWith(".scene"))) { try { string fName = Path.GetFileNameWithoutExtension(fileName); var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }; var bc = JsonConvert.DeserializeObject(File.ReadAllText(fileName), settings); if (string.IsNullOrEmpty(bc.Date)) { bc.Date = "1990-01-01 00:00"; } scenes.Add(bc); } catch (Exception e) { Debug.LogWarning(e); Error.Log(Color.red, "Loading " + fileName + " failed!"); } } foreach (var scene in scenes.OrderBy(s => DateTime.ParseExact(s.Date, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture)).ThenBy(s => s.Filename)) { UISceneContainer.CreateNew(scene, false); } } }