UniversalViewer/Assets/Scripts/ModelViewerBase/UI/UISaveLoadPanel.cs

133 lines
4.2 KiB
C#
Raw Normal View History

2024-04-21 22:38:26 +08:00
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
2024-04-21 22:38:26 +08:00
{
public ScrollRect ScrollScene;
public ScrollRect ScrollPose;
2024-04-21 22:38:26 +08:00
public Button ButtonScene;
public Button ButtonPose;
2024-04-21 22:38:26 +08:00
public List<UISceneContainer> ScenePresetEntries = new List<UISceneContainer>();
public List<UIPoseContainer> PosePresetEntries = new List<UIPoseContainer>();
2024-04-21 22:38:26 +08:00
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);
}
2024-04-21 22:38:26 +08:00
public void AddScene(UISceneContainer scene)
{
ScenePresetEntries.Add(scene);
}
2024-04-21 22:38:26 +08:00
public void OnPoseButton()
{
ButtonScene.interactable = true;
ButtonPose.interactable = false;
ScrollScene.gameObject.SetActive(false);
ScrollPose.gameObject.SetActive(true);
}
2024-04-21 22:38:26 +08:00
public void OnSceneButton()
{
ButtonScene.interactable = false;
ButtonPose.interactable = true;
ScrollScene.gameObject.SetActive(true);
ScrollPose.gameObject.SetActive(false);
}
2024-04-21 22:38:26 +08:00
public void OnDeleteButton()
{
ModelViewerMain.GetInstance().EmptySceneDefault();
}
public bool GetPoseByName(string name, out UIPoseContainer pose)
{
pose = PosePresetEntries.FirstOrDefault(p => p.PoseName == name);
return pose != null;
}
2024-04-21 22:38:26 +08:00
public bool GetSceneByName(string name, out UISceneContainer scene)
{
scene = ScenePresetEntries.FirstOrDefault(p => p.SceneData.Filename == name);
return scene != null;
}
2024-04-21 22:38:26 +08:00
private void InitPoses()
{
string path = SavePoseDirectory;
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
2024-04-21 22:38:26 +08:00
foreach (var fileName in Directory.GetFiles(path).Where(f => f.EndsWith(".pose")))
{
try
2024-04-21 22:38:26 +08:00
{
string fName = Path.GetFileNameWithoutExtension(fileName);
var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto };
var bc = JsonConvert.DeserializeObject<KeyframeData>(File.ReadAllText(fileName), settings);
UIPoseContainer.CreateNew(fName, bc, false);
}
catch (Exception e)
{
Debug.LogWarning(e);
Error.Log(Color.red, "Loading " + fileName + " failed!");
2024-04-21 22:38:26 +08:00
}
}
}
2024-04-21 22:38:26 +08:00
private void InitScenes()
{
string path = SaveSceneDirectory;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
List<SceneSerializable> scenes = new List<SceneSerializable>();
2024-04-21 22:38:26 +08:00
foreach (var fileName in Directory.GetFiles(path).Where(f => f.EndsWith(".scene")))
{
try
2024-04-21 22:38:26 +08:00
{
string fName = Path.GetFileNameWithoutExtension(fileName);
var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto };
var bc = JsonConvert.DeserializeObject<SceneSerializable>(File.ReadAllText(fileName), settings);
if (string.IsNullOrEmpty(bc.Date))
2024-04-21 22:38:26 +08:00
{
bc.Date = "1990-01-01 00:00";
2024-04-21 22:38:26 +08:00
}
scenes.Add(bc);
2024-04-21 22:38:26 +08:00
}
catch (Exception e)
2024-04-21 22:38:26 +08:00
{
Debug.LogWarning(e);
Error.Log(Color.red, "Loading " + fileName + " failed!");
2024-04-21 22:38:26 +08:00
}
}
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);
}
2024-04-21 22:38:26 +08:00
}
}