Created
July 1, 2013 13:49
-
-
Save tsubaki/5900938 to your computer and use it in GitHub Desktop.
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 System.IO; | |
using Excel; | |
using UnityEditor; | |
using UnityEngine; | |
public class ExcelDatalProcessor : AssetPostprocessor | |
{ | |
//出力先. | |
private static readonly string exportPath = "Assets/Terasurware/App Data xlsx.asset"; | |
// 入力元. | |
private static readonly string filePath = "Assets/Terasurware/app data.xls"; | |
// ①.Excelファイルインポート時に反応する. | |
static void OnPostprocessAllAssets ( | |
string[] importedAssets, | |
string[] deletedAssets, | |
string[] movedAssets, | |
string[] movedFromAssetPaths) | |
{ | |
foreach (string asset in importedAssets) { | |
if (!filePath.Equals (asset)) | |
continue; | |
// ②.データの形状に対応するScriptableObjectを生成する。既存の物があれば使用; | |
FileData data = (FileData)AssetDatabase.LoadAssetAtPath (exportPath, typeof(FileData)); | |
if (data == null) { | |
data = ScriptableObject.CreateInstance<FileData> (); | |
AssetDatabase.CreateAsset ((ScriptableObject)data, exportPath); | |
} | |
// ③.Excelを解析し、2で取得したScriptableObjectに流しこむ. | |
data.dataList.Clear (); | |
using (FileStream stream = File.Open (filePath, FileMode.Open, FileAccess.Read)) { | |
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader (stream); | |
excelReader.NextResult (); | |
excelReader.Read (); | |
while (excelReader.Read()) { | |
Debug.Log (excelReader.GetString (0)); | |
FileData.Data d = new FileData.Data (); | |
d.version = excelReader.GetInt32 (1); | |
d.url = excelReader.GetString (2); | |
data.dataList.Add (d); | |
} | |
excelReader.Close (); | |
} | |
// ④.変更を確定する. | |
ScriptableObject obj = AssetDatabase.LoadAssetAtPath (exportPath, typeof(ScriptableObject)) as ScriptableObject; | |
obj.hideFlags = HideFlags.NotEditable; | |
EditorUtility.SetDirty (obj); | |
} | |
} | |
} |
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; | |
public class FileData : ScriptableObject | |
{ | |
public List<Data> dataList; | |
[System.Serializable] | |
public class Data | |
{ | |
public string url; | |
public int version; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx share the good tips.