Created
January 15, 2018 10:56
-
-
Save blue0513/25134bb2567477a80f9636d3f3461cbe to your computer and use it in GitHub Desktop.
read csv setting file in remote server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.IO; | |
using System.Collections.Generic; | |
using System; | |
public class ReadSettingsFromServer : MonoBehaviour { | |
// 設定格納用Dictionary | |
public static Dictionary<string, string> Setting; | |
private string[] paralist; | |
private string filePath = "/Resources/CSV"; | |
private string fileName = "settings.csv"; | |
private string serverURL = "http://foo/bar/baz/settings.csv"; | |
void Awake(){ | |
createDir(); | |
} | |
void Start(){ | |
Setting = new Dictionary<string, string>(); | |
GetCSV(); | |
} | |
public void GetCSV(){ | |
string url; | |
if(NetConnection()){ | |
// リモートから引っ張る | |
url = serverURL; | |
StartCoroutine("GetData", url); | |
}else{ | |
// network接続失敗したら,ローカル設定を読む | |
string loadPath; | |
#if UNITY_EDITOR | |
loadPath = "file://" +Application.dataPath + filePath + "/" + fileName; | |
#else | |
loadPath = "file://" +Application.persistentDataPath + filePath + "/" + fileName; | |
#endif | |
StartCoroutine("GetData", loadPath); | |
} | |
} | |
private IEnumerator GetData(string _url){ | |
WWW www = new WWW(_url); | |
yield return www; | |
while (!www.isDone) { // ダウンロードの進捗を表示 | |
yield return null; | |
} | |
if (!string.IsNullOrEmpty(www.error)) { // ダウンロードでエラーが発生した | |
Debug.Log("Download Error"); | |
} else { // ダウンロードが正常に完了した | |
// ローカルに書き出す | |
#if UNITY_EDITOR | |
File.WriteAllBytes(Application.dataPath + filePath + "/" + fileName, www.bytes); | |
#else | |
File.WriteAllBytes(Application.persistentDataPath + filePath + "/" + fileName, www.bytes); | |
#endif | |
yield return new WaitForSeconds(0.5f); | |
Debug.Log("End GetData"); | |
StartCoroutine("GetParameters"); | |
} | |
} | |
private IEnumerator GetParameters(){ | |
WWW settingcsv; | |
// ローカルに引っ張ったcsvを読む | |
#if UNITY_EDITOR | |
settingcsv = new WWW("file://" + Application.dataPath + filePath + "/" + fileName); | |
#else | |
settingcsv = new WWW("file://" + Application.persistentDataPath + filePath + "/" + fileName); | |
#endif | |
yield return settingcsv; | |
// split | |
paralist = settingcsv.text.Split("\n"[0]); | |
// Settingsに格納 | |
for(int i = 0; i < paralist.Length; i++){ | |
string[] _fieldString = paralist[i].Split(","[0]); | |
Setting.Add(_fieldString[0],_fieldString[1]); | |
} | |
yield return new WaitForSeconds(0.3f); | |
Debug.Log("End GetParameters"); | |
} | |
private void createDir() { | |
#if UNITY_EDITOR | |
if(Directory.Exists(Application.dataPath + filePath)){ | |
Debug.Log ("Data Folder Exists"); | |
}else{ | |
Directory.CreateDirectory(Application.dataPath + filePath); | |
Debug.Log ("Folder Create"); | |
} | |
#else | |
if(Directory.Exists(Application.persistentDataPath + filePath)){ | |
Debug.Log ("Data Folder Exists"); | |
}else{ | |
Directory.CreateDirectory(Application.persistentDataPath + filePath); | |
Debug.Log ("Folder Create"); | |
} | |
#endif | |
} | |
private bool NetConnection(){ | |
switch ( Application.internetReachability ) { | |
//ネットワークに接続不可な場合の処理 | |
case NetworkReachability.NotReachable: | |
Debug.Log("network not connected"); | |
return false; | |
//break; | |
//ネットワークに接続可能な場合の処理 | |
default: | |
Debug.Log("network connected"); | |
return true; | |
//break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment