diff --git a/Assets/Scenes/Card.cs b/Assets/Scenes/Card.cs index 1116e18..0a9ad58 100644 --- a/Assets/Scenes/Card.cs +++ b/Assets/Scenes/Card.cs @@ -12,11 +12,14 @@ public class Card : MonoBehaviour int idx = 0; int nextChangeTime = 0; + bool updatedConfig = false; + bool writeConfigInside = false; void Start() { - + FileUtil.WatchFile(Application.streamingAssetsPath, + "config.ini", new FileSystemEventHandler(OnIniChanged)); Config.LoadConfigFromIni(); SetConfig(); Config.LoadCardsInDir(); @@ -24,23 +27,40 @@ public class Card : MonoBehaviour LoadCard(Config.cardPath); } + private void OnIniChanged(object sender, FileSystemEventArgs e) + { + + if (e.ChangeType != WatcherChangeTypes.Changed) + { + return; + } + if (writeConfigInside) + { + writeConfigInside = false; + } + else + { + Debug.Log("Ini changed"); + updatedConfig = true; + } + } + void SetConfig() { if (Config.fullscreen == 1) { - Screen.SetResolution(7680, 4320, true); + Screen.SetResolution(7680, 4320, true); } else { Screen.SetResolution(1280, 720, false); } - if (Config.cardPath == "" || Config.cardPath == "null") { Config.cardPath = FileUtil.OpenFile(); Config.SaveConfigToIni(); + writeConfigInside = true; } - Camera camera = GetComponent(); camera.orthographicSize = Config.cameraSize; camera.transform.position = @@ -59,6 +79,7 @@ public class Card : MonoBehaviour } Config.cardPath = cardPath; Config.SaveConfigToIni(); + writeConfigInside = true; nextChangeTime = (int)Time.time + Config.changeTime; } @@ -76,6 +97,16 @@ public class Card : MonoBehaviour void Update() { + if (updatedConfig) + { + Config.LoadConfigFromIni(); + SetConfig(); + Config.LoadCardsInDir(); + this.InitIdx(); + this.LoadCard(Config.cardPath); + updatedConfig = false; + } + if (Config.autoChange == 1 && Time.time >= nextChangeTime && cntCard) { LoadNextCard(); @@ -87,6 +118,7 @@ public class Card : MonoBehaviour { Config.cameraY += 0.1f; Config.SaveConfigToIni(); + writeConfigInside = true; Camera camera = GetComponent(); Vector3 vec = camera.transform.position; vec.y = Config.cameraY; @@ -98,6 +130,7 @@ public class Card : MonoBehaviour { Config.cameraY -= 0.1f; Config.SaveConfigToIni(); + writeConfigInside = true; Camera camera = GetComponent(); Vector3 vec = camera.transform.position; vec.y = Config.cameraY; diff --git a/Assets/Scenes/Config.cs b/Assets/Scenes/Config.cs index b61c466..114b87f 100644 --- a/Assets/Scenes/Config.cs +++ b/Assets/Scenes/Config.cs @@ -25,7 +25,7 @@ public static class Config public static void LoadConfigFromIni() { - ini.Open(Application.streamingAssetsPath + "/config.ini"); + ini.Open(Application.streamingAssetsPath + @"\config.ini"); fullscreen = ini.ReadValue("camera", "fullscreen", 1); randomCard = ini.ReadValue("card", "random", 1); autoChange = ini.ReadValue("card", "autochange", 1); @@ -39,7 +39,7 @@ public static class Config public static void SaveConfigToIni() { - ini.Open(Application.streamingAssetsPath + "\\config.ini"); + ini.Open(Application.streamingAssetsPath + @"\config.ini"); ini.WriteValue("card", "path", cardPath); ini.WriteValue("camera", "y", cameraY); ini.Close(); @@ -53,7 +53,7 @@ public static class Config DirectoryInfo cardDirInfo = cardFileInfo.Directory; foreach (FileInfo file in cardDirInfo.GetFiles()) { - cardsList.Add(cardsDir + "\\" + file.Name); + cardsList.Add(cardsDir + @"\" + file.Name); } cardsCount = cardsList.Count; } diff --git a/Assets/Scenes/FileUtil.cs b/Assets/Scenes/FileUtil.cs index 825b5d4..4d6de22 100644 --- a/Assets/Scenes/FileUtil.cs +++ b/Assets/Scenes/FileUtil.cs @@ -1,10 +1,12 @@ - -using System; +using System; +using System.IO; +using System.Collections; +using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; public static class FileUtil -{ +{ // My Super Fucking File Manager public static string OpenFile() { FileOpenDialog dialog = new FileOpenDialog(); @@ -26,6 +28,15 @@ public static class FileUtil } return ""; } + + public static void WatchFile(string dirPath, string filename, FileSystemEventHandler handler) + { + var watcher = new FileSystemWatcher(dirPath); + watcher.NotifyFilter = NotifyFilters.LastWrite; + watcher.Changed += handler; + watcher.Filter = filename; + watcher.EnableRaisingEvents = true; + } } diff --git a/Assets/StreamingAssets/config.ini b/Assets/StreamingAssets/config.ini index 07dc778..785bc85 100644 --- a/Assets/StreamingAssets/config.ini +++ b/Assets/StreamingAssets/config.ini @@ -6,7 +6,7 @@ random=1 autochange=1 ; 是否自动切换卡面 0为否 1为是 time=600 -; 自动切换卡面的时间,单位为秒 +; 自动切换卡面的时间,单位为秒(改太短的话可能会让热加载爆炸) [camera] fullscreen=1 @@ -15,5 +15,5 @@ size=7.0 ; 摄像机范围,用来调整卡面缩放。16:9 的屏幕 7.0;4:3 的屏幕 9.5;也可以自己看情况调整 x=0.0 ; 卡面 x 轴位置,大部分时候保持 0.0 就可以了 -y=0.0 +y=0 ; 卡面 y 轴位置,根据具体卡面调整 diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 5b3badd..273319b 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -121,7 +121,7 @@ PlayerSettings: 16:10: 1 16:9: 1 Others: 1 - bundleVersion: 0.7 + bundleVersion: 0.8 preloadedAssets: [] metroInputSource: 0 wsaTransparentSwapchain: 0