add hot reload (v0.8)

master
wlt233 2 years ago
parent b06f33fe1d
commit 571f355767

@ -12,11 +12,14 @@ public class Card : MonoBehaviour
int idx = 0; int idx = 0;
int nextChangeTime = 0; int nextChangeTime = 0;
bool updatedConfig = false;
bool writeConfigInside = false;
void Start() void Start()
{ {
FileUtil.WatchFile(Application.streamingAssetsPath,
"config.ini", new FileSystemEventHandler(OnIniChanged));
Config.LoadConfigFromIni(); Config.LoadConfigFromIni();
SetConfig(); SetConfig();
Config.LoadCardsInDir(); Config.LoadCardsInDir();
@ -24,6 +27,24 @@ public class Card : MonoBehaviour
LoadCard(Config.cardPath); 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() void SetConfig()
{ {
if (Config.fullscreen == 1) if (Config.fullscreen == 1)
@ -34,13 +55,12 @@ public class Card : MonoBehaviour
{ {
Screen.SetResolution(1280, 720, false); Screen.SetResolution(1280, 720, false);
} }
if (Config.cardPath == "" || Config.cardPath == "null") if (Config.cardPath == "" || Config.cardPath == "null")
{ {
Config.cardPath = FileUtil.OpenFile(); Config.cardPath = FileUtil.OpenFile();
Config.SaveConfigToIni(); Config.SaveConfigToIni();
writeConfigInside = true;
} }
Camera camera = GetComponent<Camera>(); Camera camera = GetComponent<Camera>();
camera.orthographicSize = Config.cameraSize; camera.orthographicSize = Config.cameraSize;
camera.transform.position = camera.transform.position =
@ -59,6 +79,7 @@ public class Card : MonoBehaviour
} }
Config.cardPath = cardPath; Config.cardPath = cardPath;
Config.SaveConfigToIni(); Config.SaveConfigToIni();
writeConfigInside = true;
nextChangeTime = (int)Time.time + Config.changeTime; nextChangeTime = (int)Time.time + Config.changeTime;
} }
@ -76,6 +97,16 @@ public class Card : MonoBehaviour
void Update() 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) if (Config.autoChange == 1 && Time.time >= nextChangeTime && cntCard)
{ {
LoadNextCard(); LoadNextCard();
@ -87,6 +118,7 @@ public class Card : MonoBehaviour
{ {
Config.cameraY += 0.1f; Config.cameraY += 0.1f;
Config.SaveConfigToIni(); Config.SaveConfigToIni();
writeConfigInside = true;
Camera camera = GetComponent<Camera>(); Camera camera = GetComponent<Camera>();
Vector3 vec = camera.transform.position; Vector3 vec = camera.transform.position;
vec.y = Config.cameraY; vec.y = Config.cameraY;
@ -98,6 +130,7 @@ public class Card : MonoBehaviour
{ {
Config.cameraY -= 0.1f; Config.cameraY -= 0.1f;
Config.SaveConfigToIni(); Config.SaveConfigToIni();
writeConfigInside = true;
Camera camera = GetComponent<Camera>(); Camera camera = GetComponent<Camera>();
Vector3 vec = camera.transform.position; Vector3 vec = camera.transform.position;
vec.y = Config.cameraY; vec.y = Config.cameraY;

@ -25,7 +25,7 @@ public static class Config
public static void LoadConfigFromIni() public static void LoadConfigFromIni()
{ {
ini.Open(Application.streamingAssetsPath + "/config.ini"); ini.Open(Application.streamingAssetsPath + @"\config.ini");
fullscreen = ini.ReadValue("camera", "fullscreen", 1); fullscreen = ini.ReadValue("camera", "fullscreen", 1);
randomCard = ini.ReadValue("card", "random", 1); randomCard = ini.ReadValue("card", "random", 1);
autoChange = ini.ReadValue("card", "autochange", 1); autoChange = ini.ReadValue("card", "autochange", 1);
@ -39,7 +39,7 @@ public static class Config
public static void SaveConfigToIni() public static void SaveConfigToIni()
{ {
ini.Open(Application.streamingAssetsPath + "\\config.ini"); ini.Open(Application.streamingAssetsPath + @"\config.ini");
ini.WriteValue("card", "path", cardPath); ini.WriteValue("card", "path", cardPath);
ini.WriteValue("camera", "y", cameraY); ini.WriteValue("camera", "y", cameraY);
ini.Close(); ini.Close();
@ -53,7 +53,7 @@ public static class Config
DirectoryInfo cardDirInfo = cardFileInfo.Directory; DirectoryInfo cardDirInfo = cardFileInfo.Directory;
foreach (FileInfo file in cardDirInfo.GetFiles()) foreach (FileInfo file in cardDirInfo.GetFiles())
{ {
cardsList.Add(cardsDir + "\\" + file.Name); cardsList.Add(cardsDir + @"\" + file.Name);
} }
cardsCount = cardsList.Count; cardsCount = cardsList.Count;
} }

@ -1,10 +1,12 @@
 using System;
using System; using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using UnityEngine; using UnityEngine;
public static class FileUtil public static class FileUtil
{ { // My Super Fucking File Manager
public static string OpenFile() public static string OpenFile()
{ {
FileOpenDialog dialog = new FileOpenDialog(); FileOpenDialog dialog = new FileOpenDialog();
@ -26,6 +28,15 @@ public static class FileUtil
} }
return ""; 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;
}
} }

@ -6,7 +6,7 @@ random=1
autochange=1 autochange=1
; 是否自动切换卡面 0为否 1为是 ; 是否自动切换卡面 0为否 1为是
time=600 time=600
; 自动切换卡面的时间,单位为秒 ; 自动切换卡面的时间,单位为秒(改太短的话可能会让热加载爆炸)
[camera] [camera]
fullscreen=1 fullscreen=1
@ -15,5 +15,5 @@ size=7.0
; 摄像机范围用来调整卡面缩放。16:9 的屏幕 7.04:3 的屏幕 9.5;也可以自己看情况调整 ; 摄像机范围用来调整卡面缩放。16:9 的屏幕 7.04:3 的屏幕 9.5;也可以自己看情况调整
x=0.0 x=0.0
; 卡面 x 轴位置,大部分时候保持 0.0 就可以了 ; 卡面 x 轴位置,大部分时候保持 0.0 就可以了
y=0.0 y=0
; 卡面 y 轴位置,根据具体卡面调整 ; 卡面 y 轴位置,根据具体卡面调整

@ -121,7 +121,7 @@ PlayerSettings:
16:10: 1 16:10: 1
16:9: 1 16:9: 1
Others: 1 Others: 1
bundleVersion: 0.7 bundleVersion: 0.8
preloadedAssets: [] preloadedAssets: []
metroInputSource: 0 metroInputSource: 0
wsaTransparentSwapchain: 0 wsaTransparentSwapchain: 0

Loading…
Cancel
Save