using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
namespace WildAttack
{
///
/// Global.csv表
///
public class GameModule : Singleton, IModule
{
#region Member
Dictionary globalDataDic;
#endregion
#region Override
public void SaveData(string[] lineArr)
{
globalDataDic = new Dictionary();
for (int i = 1; i < lineArr.Length; i++)
{
string[] data = lineArr[i].Split(',');
globalDataDic[data[0]] = data[1];
}
}
public IEnumerator LoadCSV(string fileName, Action callback)
{
//刻意延迟,避免过快加载完成,因为遇到过加载完了,但其它GameObject脚本的Start()还没执行
yield return new WaitForSeconds(0.2f);
string sPath = CSVUtils.GetStreamingAssetPath() + "/" + fileName;
UnityWebRequest www = UnityWebRequest.Get(sPath);
yield return www.SendWebRequest();
File.WriteAllText(Application.persistentDataPath + "/" + fileName, www.downloadHandler.text, Encoding.GetEncoding("utf-8"));
www.Dispose();
SaveData(CSVUtils.GetInstance().LoadFile(Application.persistentDataPath, fileName));
callback();
}
#endregion
#region Functions
///
/// string类型value
///
///
///
public float GetData(string key)
{
if (globalDataDic == null) return -1;
if (globalDataDic.ContainsKey(key))
{
if (key == "startMonsterXZ")
{
return -1;
}
if (key.Contains("RGB"))
{
return -1;
}
return float.Parse(globalDataDic[key]);
}
else
{
return -1;
}
}
///
/// 双参数
///
///
///
public Vector2 GetDoubleParams(string key)
{
if (globalDataDic.ContainsKey(key) && globalDataDic[key].Contains("|"))
{
string[] strs = globalDataDic[key].Split('|');
return new Vector2(float.Parse(strs[0]), float.Parse(strs[1]));
}
else
{
return Vector2.zero;
}
}
///
/// RGB值
///
///
///
public string GetRGB(string key)
{
if (globalDataDic.ContainsKey(key) && key.Contains("RGB"))
{
return globalDataDic[key];
}
else
{
return "";
}
}
#endregion
}
}