0
votes

A bit new to Unity, I know that you can get a prefab from the instance of the GameObject (right click on GameObject and Select Prefab). But is there a way to select the GameObjects and Components that were used to created a Prefab? (from the Prefab)

1
Note: if this question is about using the Unity Editor rather than programming it would be considered off-topic on SO (a programming Q&A site). You can try gamedev.stackexchange.com or Unity Answers, and include more detail or an image.LearnCocos2D

1 Answers

1
votes

Try this example out. It contains two flavors of selection, one to select all components attached to the prefab, and the other to select just the monobehaviours attached to the prefab.

What this script will do is check what components / monobehaviours are attached to the currently selected GameObject (or prefab) and will output them to the log, as well as select them all.

Copy the below code, put it into a new script named "PrefabComponentSelectorEditor" and save it in a folder called "Editor".

You should then see a Menu Item called "AxS -> Prefab Component Selector". Click on it, and see the console as well as the inspector.

The code is commented, but in case you have any questions, feel free to ask.

P.S. Remember to use ONLY ONE of the flavours (i.e. Monobehvaiour or Component). Comment / Uncomment the code accordingly. I have left the component version uncommented, and the monobehaviour version is right below it.

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

        }
    }
}