(unity小工具)C# 获取选择的Gameobject对象被引用的类名和字段名

复制代码
拖动到父节点的gameobject,太多后不好找。添加打印,获取类名和字段名。




[MenuItem("GameObject/获取对象引用的类名和字段名", false, 32)] public static void GetCurParentRefName() { Transform selectChild = Selection.activeTransform; if (selectChild == null) { Debug.LogError("You must select Obj first!"); return; } if (selectChild != null) { int id = selectChild.GetInstanceID(); int parentCount = 0; while (selectChild.parent != null) { parentCount++; selectChild = selectChild.parent; var coms = selectChild.GetComponents<Component>(); foreach (Component v in coms) { if (!(v is MonoBehaviour)) { continue; } System.Type clsType = v.GetType(); var properties = clsType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); foreach (System.Reflection.FieldInfo info in properties) { var attributes = info.GetCustomAttributes(typeof(System.ObsoleteAttribute), true); if (attributes.Length > 0) { continue; } var realValue = info.GetValue(v) as UnityEngine.Component; if (realValue && realValue.transform) { if (realValue.transform.GetInstanceID() == id) { Debug.Log(string.Format("向上层数:{0}|节点名:{1}|类名:{2}|字段名:{3}", parentCount, selectChild.name, clsType.Name, info.Name)); return; } } } } } } Debug.Log("not find !"); }
复制代码

 

posted @   sun_dust_shadow  阅读(661)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示