获取物体子物体transform信息生成.Csv(任何文件)

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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值