Last active
August 29, 2015 14:16
-
-
Save enpel/6a763d7191ca40521353 to your computer and use it in GitHub Desktop.
CSVとかマスターなデータとか読み込む用。
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.Collections.Generic; | |
using System.Linq; | |
public class CSVDataHolder<T> where T: ICSVData, new() | |
{ | |
Dictionary<string, List<T>> masterData = new Dictionary<string, List<T>>(); | |
public void Load(string filePath) | |
{ | |
masterData.Clear(); | |
TextAsset asset = Resources.Load(filePath) as TextAsset; | |
var text = asset.text; | |
var lines = text.Split('\n').ToList(); | |
List<T> list = new List<T>(); | |
foreach(var line in lines) | |
{ | |
T t = new T(); | |
if (t.Parse(line)) | |
{ | |
Debug.Log(t.ToLog()); | |
list.Add(t); | |
} | |
} | |
IEnumerable<string> keys = list.Select(item => item.Key); | |
foreach(var key in keys) | |
{ | |
var keyList = list.Where(item => item.Key == key); | |
if (keyList == null || keyList.Count() == 0) | |
continue; | |
masterData[key] = keyList.ToList(); | |
} | |
} | |
} |
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.Collections.Generic; | |
using System.Linq; | |
public interface ICSVData | |
{ | |
string Key { get; } | |
bool Parse(string line); | |
string ToLog(); | |
} |
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; | |
public class Sample : MonoBehaviour | |
{ | |
// Use this for initialization | |
void Start () | |
{ | |
Debug.Log("----"); | |
CSVDataHolder<MasterData> a = new CSVDataHolder<MasterData>(); | |
a.Load("testcsv"); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
} | |
public class MasterData : ICSVData | |
{ | |
public string Key { get { return Id.ToString(); } } | |
public int Id { get; private set; } | |
public string text { get; private set; } | |
public bool flag { get; private set; } | |
public bool Parse(string line) | |
{ | |
if (line.IsCommentLine()) | |
return false; | |
var split = line.Split(','); | |
if (split.Length < 3) | |
return false; | |
Id = int.Parse(split[0]); | |
text = split[1]; | |
flag = split[2] == "1"; | |
return true; | |
} | |
public string ToLog() | |
{ | |
return string.Format("--MasterData({0}): {1}, {2}, {3}",Key,Id,text,flag); | |
} | |
} |
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; | |
public static class CSVExtensions | |
{ | |
public static bool IsCommentLine(this string line) | |
{ | |
return line.StartsWith("//"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment