Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
}
}
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;
}
}
@netpyoung

This comment has been minimized.

Copy link

@netpyoung netpyoung commented Jul 1, 2013

thx share the good tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment