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); }