unity获取ui宽高_Unity 获取UI界面结构的工具

该博客介绍了一个Unity编辑器扩展脚本,用于自动从选中的GameObject导出UI组件结构,生成C#脚本,方便快速搭建和管理UI面板。用户在Unity编辑器中选择目标对象后,脚本会遍历所有子对象,根据指定的后缀映射生成相应的UI组件字段,并提供Awake和Destroy方法中的初始化和释放逻辑。
摘要由CSDN通过智能技术生成

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值