refactor script

master
wlt233 2 years ago
parent c49477a927
commit df07c12eda

@ -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>();
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<Camera>();
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<Camera>();
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);
}
}
*/
}

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 935c5f67534690c479ea90a8693eae77
guid: 612ba488027ba19408caf4911dfa6132
MonoImporter:
externalObjects: {}
serializedVersion: 2

@ -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<string> cardsList = new List<string>();
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;
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4e3ab28dcdb2eaf428d8090f8d8ec436
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dac2c00b78288c74f98aa01044c1b655
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -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<string> cards = new List<string>();
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>();
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<Camera>();
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<Camera>();
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);
}

@ -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:

@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
public class toolbar : MonoBehaviour
public class Toolbar : MonoBehaviour
{
private CanvasGroup bar;

@ -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.04:3 的屏幕 9.5;也可以自己看情况调整
x=0.0
; 卡面 x 轴位置,大部分时候保持 0.0 就可以了
y=0.0
y=-0.5
; 卡面 y 轴位置,根据具体卡面调整

Loading…
Cancel
Save