using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 生成物体信息Csv
/// </summary>
public class GetTansform : EditorWindow
{
private static EditorWindow window;
[MenuItem("Tools/Transforms2Csv")]
static void DoSomething()
{
window = GetWindow(typeof(GetTansform), true, "生成Csv文件");
window.minSize = new Vector2(400, 200);
}
string map_name="Map_1";
private void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.LabelField("生成文件名");
GUILayout.Label("Namespace", GUILayout.Width(100));
map_name = GUILayout.TextField(map_name);
EditorGUILayout.Space();
EditorGUILayout.ObjectField("选中根节点", Selection.activeGameObject, typeof(GameObject), true);
EditorGUILayout.Space();
if (GUILayout.Button("确定"))
{
if (Selection.activeGameObject == null)
{
EditorUtility.DisplayDialog("错误", "未选中物体", "OK");
return;
}
Debug.Log(Selection.activeGameObject.name);
WriteCsv(Selection.activeGameObject.transform.GetComponentsInChildren<Transform>());
this.Close();
}
}
public class Date
{
public string name;
public Vector3 localPosition;
public Vector3 localEulerAngles;
public Vector3 localScale;
}
static void SaveJson(Transform[] trans)
{
string filePath = Application.dataPath + @"/Resources/transDate.json";
Debug.Log(Application.dataPath + @"/Resources/transDate.json");
//找到当前路径
FileInfo file = new FileInfo(filePath);
//判断有没有文件,有则打开文件,,没有创建后打开文件
StreamWriter sw = file.CreateText();
Transform tran;
int count = 0;
for (int i = 0; i < trans.Length; i++)
{
tran = trans[i];
//Debug.Log(tran.name + tran.localPosition + tran.localEulerAngles + tran.localScale);
Date date = new Date { name = tran.name, localPosition = tran.localPosition, localEulerAngles = tran.localEulerAngles, localScale = tran.localScale };
string json = JsonUtility.ToJson(date);
sw.WriteLine(json);
count++;
}
EditorUtility.DisplayDialog("恭喜", string.Format("导出数据{0}条", count), "OK");
//ToJson接口将你的列表类传进去,,并自动转换为string类型
//string json = JsonUtility.ToJson(dates);
//将转换好的字符串存进文件,
//注意释放资源
sw.Close();
sw.Dispose();
AssetDatabase.Refresh();
}
public void WriteCsv(Transform[] trans)
{
string filePath = Application.dataPath + "/Resources/Csv/" + map_name + ".csv";
//找到当前路径
FileInfo file = new FileInfo(filePath);
//判断有没有文件,有则打开文件,,没有创建后打开文件
StreamWriter sw = file.CreateText();
Transform tran;
int count = 0;
string text = "name,px,py,pz,ex,ey,ez,sx,sy,sz";
sw.WriteLine(text);
text = "string,float,float,float,float,float,float,float,float,float";
sw.WriteLine(text);
for (int i = 1; i < trans.Length; i++)
{
tran = trans[i];
if (tran.gameObject.layer != 8)
{
continue;
}
//Debug.Log(tran.name + tran.localPosition + tran.localEulerAngles + tran.localScale);
text = tran.name + "," + tran.localPosition.x+","+ tran.localPosition.y + "," + tran.localPosition.z
+ "," + tran.localEulerAngles.x + "," + tran.localEulerAngles.y + "," + tran.localEulerAngles.z +
"," + tran.localScale.x + "," + tran.localScale.y + "," + tran.localScale.z;
sw.WriteLine(text);
count++;
}
EditorUtility.DisplayDialog("恭喜", string.Format("导出数据{0}条", count), "OK");
sw.Close();
sw.Dispose();
AssetDatabase.Refresh();
}
}