usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingUnityEditor;usingUnityEngine;public classCreateSprite {//当前操作的对象
private staticGameObject CurGo;//后缀对应的组件类型
public static Dictionary typeMap = new Dictionary()
{
{"sp", typeof(UISprite).Name },
{"txt", typeof(UILabel).Name },
{"btn", typeof(UIButton).Name },
{"go", typeof(GameObject).Name},
};//脚本模版
private staticCreateSpriteUnit info;//在Project窗口下,选中要导出的界面,然后点击GameObject/导出脚本
[MenuItem("GameObject/导出脚本")]public static voidCreateSpriteAction()
{
GameObject[] gameObjects=Selection.gameObjects;//保证只有一个对象
if (gameObjects.Length==1)
{
info= newCreateSpriteUnit();
CurGo= gameObjects[0];
ReadChild(CurGo.transform);
info.classname= CurGo.name + "UIPanel";
info.WtiteClass();
info= null;
CurGo= null;
}else{
EditorUtility.DisplayDialog("警告", "你只能选择一个GameObject", "确定");
}
}//遍历所有子对象,GetChild方法只能获取第一层子对象。
public static voidReadChild(Transform tf)
{foreach (Transform child intf)
{string[] typeArr = child.name.Split(‘_‘);if (typeArr.Length > 1)
{string typeKey = typeArr[typeArr.Length - 1];if(typeMap.ContainsKey(typeKey))
{
info.evenlist.Add(new UIInfo(child.name, typeKey, buildGameObjectPath(child).Replace(CurGo.name + "/","")));
}
}if (child.childCount > 0)
{
ReadChild(child);
}
}
}//获取路径,这个路径是带当前对象名的,需要用Replace替换掉头部
private static stringbuildGameObjectPath(Transform obj)
{var buffer = newStringBuilder();while (obj != null)
{if (buffer.Length > 0)
buffer.Insert(0, "/");
buffer.Insert(0, obj.name);
obj=obj.parent;
}returnbuffer.ToString();
}
}//导出脚本的模版
public classCreateSpriteUnit
{public stringclassname;public string template = @"using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class @ClassName
{
@fields
public void OnAwake(GameObject viewGO)
{
@body1
}
public void OnDestroy()
{
@body2
}
}";//缓存的所有子对象信息
public List evenlist = new List();///
///把拼接好的脚本写到本地。///(自己可以个窗口支持改名和选择路径,真实工程里是带这些功能的)///
public voidWtiteClass()
{bool flag = true;bool throwOnInvalidBytes = false;
UTF8Encoding encoding= newUTF8Encoding(flag, throwOnInvalidBytes);bool append = false;
StreamWriter writer= new StreamWriter(Application.dataPath + "/" + classname + ".cs", append, encoding);
writer.Write(GetClasss());
writer.Close();
AssetDatabase.Refresh();
}//脚本拼接
public stringGetClasss()
{var fields = newStringBuilder();var body1 = newStringBuilder();var body2 = newStringBuilder();for (int i = 0; i < evenlist.Count; i++)
{
fields.AppendLine("\t" +evenlist[i].field);
body1.AppendLine("\t\t" +evenlist[i].body1);
body2.AppendLine("\t\t" +evenlist[i].body2);
}
template= template.Replace("@ClassName", classname).Trim();
template= template.Replace("@body1", body1.ToString()).Trim();
template= template.Replace("@body2", body2.ToString()).Trim();
template= template.Replace("@fields", fields.ToString()).Trim();returntemplate;
}
}//子对象信息
public classUIInfo{public stringfield;public stringbody1;public stringbody2;public UIInfo(string name, string typeKey, stringpath)
{
field= string.Format("public {0} {1};", CreateSprite.typeMap[typeKey], name);if (typeKey == "go")
{
body1= string.Format("{0} = viewGO.transform.Find(\"{1}\").gameObject;", name, path, CreateSprite.typeMap[typeKey]);
}else{
body1= string.Format("{0} = viewGO.transform.Find(\"{1}\").GetComponent();", name, path, CreateSprite.typeMap[typeKey]);
}
body2= string.Format("{0} = null;", name);
}
}