Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSVファイルをエクスポートするEditor拡張のサンプル
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
[CustomEditor(typeof(CsvExporter))]
public class CsvExporterEditor : Editor {
public override void OnInspectorGUI(){
var csvExporter = target as CsvExporter;
DrawDefaultInspector();
if (GUILayout.Button("敵データのエクスポート")){
OutputCsv(csvExporter);
}
}
void OutputCsv(CsvExporter csvExporter){
if (string.IsNullOrEmpty(csvExporter.fileName)){
Debug.LogWarning(csvExporter.name + " : 出力するCSVファイル名が入力されていません。");
return;
}
// プロジェクト内のEnemyDataを読み込む
string[] outputList = AssetDatabase.FindAssets("t:EnemyData");
List<EnemyData> enemyDataList = new List<EnemyData>();
foreach (string guid in outputList){
string path = AssetDatabase.GUIDToAssetPath(guid);
EnemyData data = AssetDatabase.LoadAssetAtPath<EnemyData>(path);
if (data != null){
enemyDataList.Add(data);
}
}
// CSVファイルの出力先を設定する
string outputPath = AssetDatabase.GetAssetPath(csvExporter);
outputPath = outputPath.Substring(0, outputPath.LastIndexOf(csvExporter.name)) + csvExporter.fileName + ".csv";
FileInfo fi = new FileInfo(outputPath);
StreamWriter sw = fi.CreateText();
string header = "enemyName,maxHp,atk,def,exp,gold";
sw.WriteLine(header);
// ScriptableObjectの内容をファイルに書き込む
foreach (EnemyData data in enemyDataList){
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(data.enemyName).Append(",")
.Append(data.maxHp).Append(",")
.Append(data.atk).Append(",")
.Append(data.def).Append(",")
.Append(data.exp).Append(",")
.Append(data.gold);
sw.WriteLine(sb.ToString());
}
// ファイルアクセスを閉じる
sw.Flush();
sw.Close();
// プロジェクト内のアセットの表示を更新する
AssetDatabase.Refresh();
Debug.Log("敵データのエクスポート完了!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.