1.原因
最近模型经常要建模人员返工修改导致,模型上的一些数据,和tag值没次都要手动修改,用这个脚本,可以记录下unity的修改数据,最后在自动修改新模型的参数,只要建模那边不乱修改模型的一些物体名字.
2.思路
通过编辑器把在unity里面修改的数据记录下来,转json保存,在拿到建模返工的模型后,通过读取json对新模型的参数修改为之前记录下来的样子.唯一有点技术含量的就是通过递归遍历完模型的子物体.
3.代码
using LitJson;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class GameobjectModeData : Editor
{
//txt路径
private static string path = Application.dataPath + "/Editor/模型数据.txt";
private static Dictionary<string , List<modelClass>> modelClassDic = new Dictionary<string , List<modelClass>>();
[MenuItem(@"模型数据/保存数据")]
public static void SetTransforms()
{
ReadJson();
//transforms是Selection类的静态字段,其返回的是选中的对象的Transform
Transform[] transforms = Selection.transforms;
List<modelClass> ItemList = new List<modelClass>();
//将字典中的信息打印出来
foreach (Transform item in transforms)
{
for (int i = 0; i < item.childCount; i++)
{
Transform form = item.GetChild(i);
ItemList.AddRange(getChildModelClass(form , form.name));
}
modelClassDic[item.name] = ItemList;
}
WriteJson(ItemList);
Debug.Log(transforms[0].name+"模型参数转json完成");
}
[MenuItem(@"模型数据/读取数据")]
public static void GetTransforms()
{
ReadJson();
Transform[] transforms = Selection.transforms;
List<modelClass> ItemList = new List<modelClass>();
//将字典中的信息打印出来
foreach (Transform item in transforms)
{
if (modelClassDic.ContainsKey(item.name))
{
ItemList = modelClassDic[item.name];
for (int i = 0; i < ItemList.Count; i++)
{
modelClass model = ItemList[i];
Transform tra = item.Find(model.luJing);
if (tra)
{
tra.tag = model.tag;
tra.gameObject.layer = model.layer;
}
}
}
}
Debug.Log(transforms[0].name + "读取json,设置模型参数完成");
}
//递归
private static List<modelClass> getChildModelClass(Transform _tra , string str)
{
List<modelClass> modelList = new List<modelClass>();
modelList.Add(getModelclass(_tra , str));
for (int i = 0; i < _tra.childCount; i++)
{
Transform form = _tra.GetChild(i);
modelList.AddRange(getChildModelClass(form , str + "/" + form.name));
}
return modelList;
}
//单个gameobject保存数据
private static modelClass getModelclass(Transform _tra , string str)
{
return new modelClass(str , _tra.tag , _tra.gameObject.layer);
}
/// <summary>
/// 保存模型上的数据
/// </summary>
/// <param name="modelList"></param>
private static void WriteJson(List<modelClass> modelList)
{
string values = JsonMapper.ToJson(modelClassDic);
FileStream file = new FileStream(path , System.IO.FileMode.Create);
byte[] bts = System.Text.Encoding.UTF8.GetBytes(values);
file.Write(bts , 0 , bts.Length);
if (file != null)
{
file.Close();
}
}
/// <summary>
/// 读取之前保存的模型数据
/// </summary>
private static void ReadJson()
{
StreamReader streamReader = new StreamReader(path);
string str = streamReader.ReadToEnd();
if(str!=""|| str!=null)
modelClassDic = JsonMapper.ToObject<Dictionary<string , List<modelClass>>>(str);
streamReader.Close();
streamReader.Dispose();
}
}
/// <summary>
/// 保存数据类
/// </summary>
public class modelClass
{
public string luJing;
public string tag;
public int layer;
public modelClass()
{
}
public modelClass(string luJing , string tag , int layer)
{
this.luJing = luJing;
this.tag = tag;
this.layer = layer;
}
}
4.最后一点
脚本放到editor文件夹下,生成的txt文档,要提前创建好,放到editor文件夹下,这个文件夹不会参与到最后的打包,只能在编辑器下使用.
|