You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
4.5 KiB

2 years ago
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;
bool updatedConfig = false;
bool writeConfigInside = false;
2 years ago
void Start()
{
FileUtil.WatchFile(Application.streamingAssetsPath,
"config.ini", new FileSystemEventHandler(OnIniChanged));
2 years ago
Config.LoadConfigFromIni();
SetConfig();
Config.LoadCardsInDir();
InitIdx();
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;
}
}
2 years ago
void SetConfig()
{
if (Config.fullscreen == 1)
{
Screen.SetResolution(7680, 4320, true);
2 years ago
}
else
{
Screen.SetResolution(1280, 720, false);
}
if (Config.cardPath == "" || Config.cardPath == "null")
{
Config.cardPath = FileUtil.OpenFile();
Config.SaveConfigToIni();
writeConfigInside = true;
2 years ago
}
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();
writeConfigInside = true;
2 years ago
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 (updatedConfig)
{
Config.LoadConfigFromIni();
SetConfig();
Config.LoadCardsInDir();
this.InitIdx();
this.LoadCard(Config.cardPath);
updatedConfig = false;
}
2 years ago
if (Config.autoChange == 1 && Time.time >= nextChangeTime && cntCard)
{
LoadNextCard();
}
//SetResolution();
}
public void AdjustYAdd()
{
Config.cameraY += 0.1f;
Config.SaveConfigToIni();
writeConfigInside = true;
2 years ago
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();
writeConfigInside = true;
2 years ago
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);
}
}
*/
}