obsidian/笔记文件/2.笔记/拿到脚本TessObject上的GameObject实体和检测是否为prefab预制体.md
2025-03-26 00:02:56 +08:00

1.2 KiB

#unity/日常积累

            GameObject go = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Point_1.prefab", typeof(GameObject)) as GameObject;
            Component[] aaa = go.GetComponents(typeof(Component));
            for (int i = 0; i < aaa.Length; i++)
            {
                if (aaa[i].GetType().Name == "TessObject")
                {
                    
                    FieldInfo[] proper = aaa[i].GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | BindingFlags.NonPublic);
                    for (int j = 0; j < proper.Length; j++)
                    {
                        Type gameObject = typeof(GameObject);
                        if (proper[j].FieldType == gameObject)
                        {
                            GameObject sss =
                                PrefabUtility.GetCorrespondingObjectFromOriginalSource(
                                    proper[j].GetValue(aaa[i]) as GameObject);
                            string rootPath = AssetDatabase.GetAssetPath(sss);
                            Debug.Log("path:" + rootPath);
                        }
                    }
                    Debug.Log(aaa[i].GetType().Name);
                }
            }