From df07c12eda2936ee8f647b1c29d3ae2e9aae9c43 Mon Sep 17 00:00:00 2001 From: wlt233 <1486185683@qq.com> Date: Thu, 17 Nov 2022 15:59:08 +0800 Subject: [PATCH] refactor script --- Assets/Scenes/Card.cs | 159 +++++++++++ .../Scenes/{loadCard.cs.meta => Card.cs.meta} | 2 +- Assets/Scenes/Config.cs | 60 ++++ Assets/Scenes/Config.cs.meta | 11 + Assets/Scenes/FileUtil.cs | 71 +++++ Assets/Scenes/FileUtil.cs.meta | 11 + Assets/Scenes/loadCard.cs | 268 ------------------ Assets/Scenes/main.unity | 45 ++- Assets/Scenes/toolbar.cs | 2 +- Assets/StreamingAssets/config.ini | 4 +- 10 files changed, 333 insertions(+), 300 deletions(-) create mode 100644 Assets/Scenes/Card.cs rename Assets/Scenes/{loadCard.cs.meta => Card.cs.meta} (83%) create mode 100644 Assets/Scenes/Config.cs create mode 100644 Assets/Scenes/Config.cs.meta create mode 100644 Assets/Scenes/FileUtil.cs create mode 100644 Assets/Scenes/FileUtil.cs.meta delete mode 100644 Assets/Scenes/loadCard.cs diff --git a/Assets/Scenes/Card.cs b/Assets/Scenes/Card.cs new file mode 100644 index 0000000..1116e18 --- /dev/null +++ b/Assets/Scenes/Card.cs @@ -0,0 +1,159 @@ +using System; +using System.IO; +using System.Collections; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using UnityEngine; + +public class Card : MonoBehaviour +{ + GameObject cntCard; + AssetBundle bundle; + + int idx = 0; + int nextChangeTime = 0; + + + void Start() + { + + Config.LoadConfigFromIni(); + SetConfig(); + Config.LoadCardsInDir(); + InitIdx(); + LoadCard(Config.cardPath); + } + + void SetConfig() + { + if (Config.fullscreen == 1) + { + Screen.SetResolution(7680, 4320, true); + } + else + { + Screen.SetResolution(1280, 720, false); + } + + if (Config.cardPath == "" || Config.cardPath == "null") + { + Config.cardPath = FileUtil.OpenFile(); + Config.SaveConfigToIni(); + } + + Camera camera = GetComponent(); + camera.orthographicSize = Config.cameraSize; + camera.transform.position = + new Vector3(Config.cameraX, Config.cameraY, -10); + } + + void LoadCard(string cardPath) + { + if (cntCard) Destroy(cntCard); + if (bundle) bundle.Unload(true); + bundle = AssetBundle.LoadFromFile(cardPath); + foreach (var name in bundle.GetAllAssetNames()) + { + Debug.Log(name); + cntCard = (GameObject)Instantiate(bundle.LoadAsset(name)); + } + Config.cardPath = cardPath; + Config.SaveConfigToIni(); + nextChangeTime = (int)Time.time + Config.changeTime; + } + + void InitIdx() + { + for (int i = 0; i < Config.cardsCount; ++i) + { + if (Config.cardsList[i] == Config.cardPath) + { + idx = i; + } + } + } + + + void Update() + { + if (Config.autoChange == 1 && Time.time >= nextChangeTime && cntCard) + { + LoadNextCard(); + } + //SetResolution(); + } + + public void AdjustYAdd() + { + Config.cameraY += 0.1f; + Config.SaveConfigToIni(); + Camera camera = GetComponent(); + Vector3 vec = camera.transform.position; + vec.y = Config.cameraY; + camera.transform.position = vec; + + } + + public void AdjustYMinus() + { + Config.cameraY -= 0.1f; + Config.SaveConfigToIni(); + Camera camera = GetComponent(); + Vector3 vec = camera.transform.position; + vec.y = Config.cameraY; + camera.transform.position = vec; + } + + public void LoadNextCard() + { + if (Config.randomCard == 1) + { + System.Random ran = new System.Random(); + idx = ran.Next(Config.cardsCount); + } + else + { + idx++; + idx %= Config.cardsCount; + } + LoadCard(Config.cardsList[idx]); + } + + public void LoadNewCard() + { + LoadCard(FileUtil.OpenFile()); + Config.LoadCardsInDir(); + InitIdx(); + } + + public void Quit() + { + Application.Quit(); + } + + /* + public void SetResolution() + { + if (Screen.fullScreen) + { + return; + } + int curScreenWidth = Screen.width; + int curScreenHeight = Screen.height; + float curScale = (float)curScreenWidth / curScreenHeight; + if (curScale > 16 / 9) + { + int h = (int)((9 * curScreenWidth) / 16); + Screen.SetResolution(curScreenWidth, h, false); + + } + else if (curScale < 16 / 9) + { + int w = (int)((16 * curScreenHeight) / 9); + Screen.SetResolution(w, curScreenHeight, false); + + } + } + */ +} + diff --git a/Assets/Scenes/loadCard.cs.meta b/Assets/Scenes/Card.cs.meta similarity index 83% rename from Assets/Scenes/loadCard.cs.meta rename to Assets/Scenes/Card.cs.meta index dacd492..c82a6ed 100644 --- a/Assets/Scenes/loadCard.cs.meta +++ b/Assets/Scenes/Card.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 935c5f67534690c479ea90a8693eae77 +guid: 612ba488027ba19408caf4911dfa6132 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scenes/Config.cs b/Assets/Scenes/Config.cs new file mode 100644 index 0000000..82aedd1 --- /dev/null +++ b/Assets/Scenes/Config.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Collections; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using UnityEngine; + + + +public static class Config +{ + static INIParser ini = new INIParser(); + public static int fullscreen = 1; + public static int randomCard = 0; + public static int autoChange = 0; + public static int changeTime = 600; + public static string cardPath = ""; + public static float cameraSize = 7f; + public static float cameraX = 0f; + public static float cameraY = 0f; + public static string cardsDir; + public static int cardsCount; + public static List cardsList = new List(); + + + public static void LoadConfigFromIni() + { + ini.Open(Application.streamingAssetsPath + "/config.ini"); + fullscreen = ini.ReadValue("camera", "fullscreen", 1); + randomCard = ini.ReadValue("card", "random", 0); + autoChange = ini.ReadValue("card", "autochange", 0); + changeTime = ini.ReadValue("card", "time", 600); + cardPath = ini.ReadValue("card", "path", "null"); + cameraSize = (float)ini.ReadValue("camera", "size", 7f); + cameraX = (float)ini.ReadValue("camera", "x", 0f); + cameraY = (float)ini.ReadValue("camera", "y", 0f); + ini.Close(); + } + + public static void SaveConfigToIni() + { + ini.Open(Application.streamingAssetsPath + "\\config.ini"); + ini.WriteValue("card", "path", cardPath); + ini.WriteValue("camera", "y", cameraY); + ini.Close(); + } + + public static void LoadCardsInDir() + { + cardsList.Clear(); + FileInfo cardFileInfo = new FileInfo(cardPath); + cardsDir = cardFileInfo.DirectoryName; + DirectoryInfo cardDirInfo = cardFileInfo.Directory; + foreach (FileInfo file in cardDirInfo.GetFiles()) + { + cardsList.Add(cardsDir + "\\" + file.Name); + } + cardsCount = cardsList.Count; + } +} diff --git a/Assets/Scenes/Config.cs.meta b/Assets/Scenes/Config.cs.meta new file mode 100644 index 0000000..7feebad --- /dev/null +++ b/Assets/Scenes/Config.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4e3ab28dcdb2eaf428d8090f8d8ec436 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/FileUtil.cs b/Assets/Scenes/FileUtil.cs new file mode 100644 index 0000000..825b5d4 --- /dev/null +++ b/Assets/Scenes/FileUtil.cs @@ -0,0 +1,71 @@ + +using System; +using System.Runtime.InteropServices; +using UnityEngine; + +public static class FileUtil +{ + public static string OpenFile() + { + FileOpenDialog dialog = new FileOpenDialog(); + dialog.structSize = Marshal.SizeOf(dialog); + dialog.filter = "All Files\0*\0\0"; + dialog.file = new string(new char[256]); + dialog.maxFile = dialog.file.Length; + dialog.fileTitle = new string(new char[64]); + dialog.maxFileTitle = dialog.fileTitle.Length; + dialog.initialDir = UnityEngine.Application.dataPath; + dialog.title = "Open Card Asset Bundle"; + dialog.defExt = ""; + dialog.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008; + //OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_NOCHANGEDIR + if (DialogShow.GetOpenFileName(dialog)) + { + Debug.Log(dialog.file); + return dialog.file; + } + return ""; + } +} + + +[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] +public class FileOpenDialog +{ + public int structSize = 0; + public IntPtr dlgOwner = IntPtr.Zero; + public IntPtr instance = IntPtr.Zero; + public String filter = null; + public String customFilter = null; + public int maxCustFilter = 0; + public int filterIndex = 0; + public String file = null; + public int maxFile = 0; + public String fileTitle = null; + public int maxFileTitle = 0; + public String initialDir = null; + public String title = null; + public int flags = 0; + public short fileOffset = 0; + public short fileExtension = 0; + public String defExt = null; + public IntPtr custData = IntPtr.Zero; + public IntPtr hook = IntPtr.Zero; + public String templateName = null; + public IntPtr reservedPtr = IntPtr.Zero; + public int reservedInt = 0; + public int flagsEx = 0; +} + +public class DialogShow +{ + [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] + public static extern bool GetOpenFileName([In, Out] FileOpenDialog dialog); +} + + + + + + + diff --git a/Assets/Scenes/FileUtil.cs.meta b/Assets/Scenes/FileUtil.cs.meta new file mode 100644 index 0000000..2efb285 --- /dev/null +++ b/Assets/Scenes/FileUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dac2c00b78288c74f98aa01044c1b655 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/loadCard.cs b/Assets/Scenes/loadCard.cs deleted file mode 100644 index 88d3623..0000000 --- a/Assets/Scenes/loadCard.cs +++ /dev/null @@ -1,268 +0,0 @@ -using System; -using System.IO; -using System.Collections; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using UnityEngine; - -public class loadCard : MonoBehaviour -{ - GameObject cntCard; - AssetBundle bundle; - string cardDir; - List cards = new List(); - int idx = 0; - int cardCount; - int randomCard; - int autoChange; - int changeTime; - int nextChangeTime = 0; - - - void Start() - { - INIParser ini = new INIParser(); - ini.Open(Application.streamingAssetsPath + "/config.ini"); - - //Screen.fullScreen = Convert.ToBoolean(ini.ReadValue("camera", "fullscreen", 1)); - if (ini.ReadValue("camera", "fullscreen", 1) == 1) - { - Screen.SetResolution(7680, 4320, true); - } - else - { - Screen.SetResolution(1280, 720, false); - } - - randomCard = ini.ReadValue("card", "random", 0); - autoChange = ini.ReadValue("card", "autochange", 0); - changeTime = ini.ReadValue("card", "time", 0); - nextChangeTime = changeTime; - - string cardPath = ini.ReadValue("card", "path", "null"); - if (cardPath == "" || cardPath == "null") - { - cardPath = OpenFile(); - ini.WriteValue("card", "path", cardPath); - } - FileInfo cardFileInfo = new FileInfo(cardPath); - cardDir = cardFileInfo.DirectoryName; - DirectoryInfo cardDirInfo = cardFileInfo.Directory; - int i = 0; - foreach (FileInfo file in cardDirInfo.GetFiles()) - { - cards.Add(file.Name); - if (file.Name == cardFileInfo.Name) - idx = i; - i++; - } - cardCount = cards.Count; - - Camera camera = GetComponent(); - camera.orthographicSize = (float)ini.ReadValue("camera", "size", 7f); - float x = (float)ini.ReadValue("camera", "x", 0f); - float y = (float)ini.ReadValue("camera", "y", 0f); - camera.transform.position = new Vector3(x, y, -10); - - ini.Close(); - - - bundle = AssetBundle.LoadFromFile(cardPath); - foreach (var name in bundle.GetAllAssetNames()) - { - Debug.Log(name); - cntCard = (GameObject)Instantiate(bundle.LoadAsset(name)); - } - - } - - - void Update() - { - if (autoChange == 1 && Time.time >= nextChangeTime && cntCard) - { - LoadNextCard(); - nextChangeTime += changeTime; - } - //SetResolution(); - - //if (Input.GetMouseButtonDown(0)) LoadNextCard(); - - } - - public void AdjustYAdd() - { - Camera camera = GetComponent(); - Vector3 vec = camera.transform.position; - vec.y += 0.1f; - camera.transform.position = vec; - INIParser ini = new INIParser(); - ini.Open(Application.streamingAssetsPath + "/config.ini"); - ini.WriteValue("camera", "y", vec.y); - ini.Close(); - } - - public void AdjustYMinus() - { - Camera camera = GetComponent(); - Vector3 vec = camera.transform.position; - vec.y -= 0.1f; - camera.transform.position = vec; - INIParser ini = new INIParser(); - ini.Open(Application.streamingAssetsPath + "/config.ini"); - ini.WriteValue("camera", "y", vec.y); - ini.Close(); - - } - - public void LoadNextCard() - { - if (cntCard) - Destroy(cntCard); - if (bundle) - bundle.Unload(true); - - if (randomCard == 1) - { - System.Random ran = new System.Random(); - idx = ran.Next(0, cardCount); - } - string cardPath = cardDir + "\\" + cards[(++idx) % cardCount]; - INIParser ini = new INIParser(); - ini.Open(Application.streamingAssetsPath + "/config.ini"); - ini.WriteValue("card", "path", cardPath); - ini.Close(); - - bundle = AssetBundle.LoadFromFile(cardPath); - foreach (var name in bundle.GetAllAssetNames()) - { - Debug.Log(name); - cntCard = (GameObject)Instantiate(bundle.LoadAsset(name)); - } - } - - public void LoadNewCard() - { - if (cntCard) - Destroy(cntCard); - if (bundle) - bundle.Unload(true); - string cardPath = OpenFile(); - INIParser ini = new INIParser(); - ini.Open(Application.streamingAssetsPath + "/config.ini"); - ini.WriteValue("card", "path", cardPath); - ini.Close(); - - bundle = AssetBundle.LoadFromFile(cardPath); - foreach (var name in bundle.GetAllAssetNames()) - { - Debug.Log(name); - cntCard = (GameObject)Instantiate(bundle.LoadAsset(name)); - } - - FileInfo cardFileInfo = new FileInfo(cardPath); - cardDir = cardFileInfo.DirectoryName; - DirectoryInfo cardDirInfo = cardFileInfo.Directory; - int i = 0; - foreach (FileInfo file in cardDirInfo.GetFiles()) - { - cards.Add(file.Name); - if (file.Name == cardFileInfo.Name) - idx = i; - i++; - } - cardCount = cards.Count; - } - - public void Quit() - { - Application.Quit(); - } - - - void SetResolution() - { - if (Screen.fullScreen) - { - return; - } - int curScreenWidth = Screen.width; - int curScreenHeight = Screen.height; - float curScale = (float)curScreenWidth / curScreenHeight; - if (curScale > 16 / 9) - { - int h = (int)((9 * curScreenWidth) / 16); - Screen.SetResolution(curScreenWidth, h, false); - - } - else if (curScale < 16 / 9) - { - int w = (int)((16 * curScreenHeight) / 9); - Screen.SetResolution(w, curScreenHeight, false); - - } - - } - - - - - public string OpenFile() - { - FileOpenDialog dialog = new FileOpenDialog(); - dialog.structSize = Marshal.SizeOf(dialog); - dialog.filter = "All Files\0*\0\0"; - dialog.file = new string(new char[256]); - dialog.maxFile = dialog.file.Length; - dialog.fileTitle = new string(new char[64]); - dialog.maxFileTitle = dialog.fileTitle.Length; - dialog.initialDir = UnityEngine.Application.dataPath; - dialog.title = "打开卡面"; - dialog.defExt = ""; - dialog.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008; - //OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_NOCHANGEDIR - if (DialogShow.GetOpenFileName(dialog)) - { - Debug.Log(dialog.file); - return dialog.file; - } - return ""; - } - -} - - -[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] -public class FileOpenDialog -{ - public int structSize = 0; - public IntPtr dlgOwner = IntPtr.Zero; - public IntPtr instance = IntPtr.Zero; - public String filter = null; - public String customFilter = null; - public int maxCustFilter = 0; - public int filterIndex = 0; - public String file = null; - public int maxFile = 0; - public String fileTitle = null; - public int maxFileTitle = 0; - public String initialDir = null; - public String title = null; - public int flags = 0; - public short fileOffset = 0; - public short fileExtension = 0; - public String defExt = null; - public IntPtr custData = IntPtr.Zero; - public IntPtr hook = IntPtr.Zero; - public String templateName = null; - public IntPtr reservedPtr = IntPtr.Zero; - public int reservedInt = 0; - public int flagsEx = 0; -} - -public class DialogShow -{ - [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] - public static extern bool GetOpenFileName([In, Out] FileOpenDialog dialog); -} - diff --git a/Assets/Scenes/main.unity b/Assets/Scenes/main.unity index c2ec8c2..0f56326 100644 --- a/Assets/Scenes/main.unity +++ b/Assets/Scenes/main.unity @@ -268,15 +268,15 @@ MonoBehaviour: m_OnClick: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 519420030} - m_MethodName: LoadNextCard - m_Mode: 1 + - m_Target: {fileID: 11500000, guid: 612ba488027ba19408caf4911dfa6132, type: 3} + m_MethodName: set_name + m_Mode: 5 m_Arguments: m_ObjectArgument: {fileID: 0} m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine m_IntArgument: 0 m_FloatArgument: 0 - m_StringArgument: + m_StringArgument: LoadNextCard m_BoolArgument: 0 m_CallState: 2 --- !u!114 &334217503 @@ -504,7 +504,7 @@ MonoBehaviour: m_GameObject: {fileID: 519420028} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 935c5f67534690c479ea90a8693eae77, type: 3} + m_Script: {fileID: 11500000, guid: 612ba488027ba19408caf4911dfa6132, type: 3} m_Name: m_EditorClassIdentifier: --- !u!20 &519420031 @@ -642,15 +642,15 @@ MonoBehaviour: m_OnClick: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 519420030} - m_MethodName: AdjustYAdd - m_Mode: 1 + - m_Target: {fileID: 11500000, guid: 612ba488027ba19408caf4911dfa6132, type: 3} + m_MethodName: set_name + m_Mode: 5 m_Arguments: m_ObjectArgument: {fileID: 0} m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine m_IntArgument: 0 m_FloatArgument: 0 - m_StringArgument: + m_StringArgument: AdjustYMinus m_BoolArgument: 0 m_CallState: 2 --- !u!114 &653438703 @@ -767,15 +767,15 @@ MonoBehaviour: m_OnClick: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 519420030} - m_MethodName: LoadNewCard - m_Mode: 1 + - m_Target: {fileID: 11500000, guid: 612ba488027ba19408caf4911dfa6132, type: 3} + m_MethodName: set_name + m_Mode: 5 m_Arguments: m_ObjectArgument: {fileID: 0} m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine m_IntArgument: 0 m_FloatArgument: 0 - m_StringArgument: + m_StringArgument: LoadNewCard m_BoolArgument: 0 m_CallState: 2 --- !u!114 &777780071 @@ -1285,18 +1285,7 @@ MonoBehaviour: m_TargetGraphic: {fileID: 1770318495} m_OnClick: m_PersistentCalls: - m_Calls: - - m_Target: {fileID: 519420030} - m_MethodName: Quit - m_Mode: 1 - m_Arguments: - m_ObjectArgument: {fileID: 0} - m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine - m_IntArgument: 0 - m_FloatArgument: 0 - m_StringArgument: - m_BoolArgument: 0 - m_CallState: 2 + m_Calls: [] --- !u!114 &1770318495 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1411,12 +1400,12 @@ MonoBehaviour: m_OnClick: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 519420030} - m_MethodName: AdjustYMinus + - m_Target: {fileID: 11500000, guid: 612ba488027ba19408caf4911dfa6132, type: 3} + m_MethodName: m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} - m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_ObjectArgumentAssemblyTypeName: m_IntArgument: 0 m_FloatArgument: 0 m_StringArgument: diff --git a/Assets/Scenes/toolbar.cs b/Assets/Scenes/toolbar.cs index 860bba8..4296338 100644 --- a/Assets/Scenes/toolbar.cs +++ b/Assets/Scenes/toolbar.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using UnityEngine; -public class toolbar : MonoBehaviour +public class Toolbar : MonoBehaviour { private CanvasGroup bar; diff --git a/Assets/StreamingAssets/config.ini b/Assets/StreamingAssets/config.ini index 228dc6b..8aa5d27 100644 --- a/Assets/StreamingAssets/config.ini +++ b/Assets/StreamingAssets/config.ini @@ -1,5 +1,5 @@ [card] -path=C:\Users\14861\Desktop\20220712_assaultlily\all_assets_new\asset\asset_bundle\card_prefab\card_010001001 +path= ; 储存的卡面路径 random=1 ; 点击下一张时是否随机 0为按顺序 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.5 ; 卡面 y 轴位置,根据具体卡面调整