Unity:有没有办法获得Prefab中使用的所有GameObjects / Components?

时间:2015-01-07 18:33:14

标签: unity3d

有点新的Unity,我知道你可以从GameObject的实例中获得一个预制件(右键单击GameObject和Select Prefab)。但有没有办法选择用于创建预制件的游戏对象和组件? (来自预制)

1 个答案:

答案 0 :(得分:1)

试试这个例子。它包含两种选择,一种用于选择附加到预制件的所有组件,另一种用于选择附加到预制件上的 monobehaviours

这个脚本将做的是检查哪些组件/ monobehaviours附加到当前选择的GameObject(或预制件),并将它们输出到日志,以及全部选择它们。

复制以下代码,将其放入名为" PrefabComponentSelectorEditor"的新脚本中。并将其保存在名为"编辑器"。

的文件夹中

然后您应该看到名为" AxS - >的菜单项。预制组件选择器"。单击它,然后查看控制台和检查器。

代码已被评论,但如果您有任何疑问,请随时提出。

P.S。请记住只使用其中一种口味(即Monobehvaiour或Component)。相应地注释/取消注释代码。我已将组件版本取消注释,monobehaviour版本就在它下方。

using UnityEngine;
using UnityEditor;
using System.Collections;

public class PrefabComponentSelectorEditor : EditorWindow {

    [MenuItem("AxS/Prefab Component Selector")]
    public static void SelectComponents () {
        Debug.Log ("GOs "+Selection.activeGameObject);
        if(Selection.activeGameObject != null) {

            //This will select all the COMPONENTS attached to the prefab

            //Let's first get all the components this GameObject has
            Component[] components = Selection.activeGameObject.GetComponents<Component>() as Component[];
            //If the length of components is > 0 (always WILL be, since every GameObject is
            //guarenteed to have a Transform Component at the very minimum
            if(components.Length > 0) {
                //Print all the components to console
                foreach(Component component in components)
                    Debug.Log (component);
                //Set the current selection to the components
                Selection.objects = (Object[])components;
            }




            //This will select all the MONOBEHAVIOURS attached to the prefab

            //First get all Monobehavious attached
            //MonoBehaviour[] behaviours = Selection.activeGameObject.GetComponents<MonoBehaviour>() as MonoBehaviour[];

            //Check if the length of the array is > 0. This is required, since a GameObject 
            //may have no scripts attached to it
            //if(behaviours.Length > 0) {
                  //Print all attached Monobehvaiours to console
            //    foreach(MonoBehaviour behaviour in behaviours)
            //        Debug.Log (behaviour);
                  //Set the current selection to the Monobehaviours
        //        Selection.objects = (Object[])behaviours;
            //}



            //The difference between COMPONENT and MONOBEHAVIOUR
            //A Monobehaviour is basically any script you make, so if you use the monobehaviour version
            //it will only select the scripts you attach to the prefab
            //A component is anything you can see on the inspector, including Unity's inbuilt components
            //Essentially, all Monobehaviours are Components

            //Eg. If your prefab has a single script called "ExampleScript" attached to it, and has a BoxCollider
            //Monobehaviour version will output just the ExampleScript
            //Component version will output the Transform, BoxCollider and ExampleScript

        }
    }
}