Search Unity

How can I set the selected editor object to an object within the Prefab Mode

Discussion in 'Prefabs' started by FritzC, Nov 27, 2020.

  1. FritzC

    FritzC

    Joined:
    Nov 15, 2017
    Posts:
    15
    I have an editor that opens a prefab and then attempts to add a GameObject within the Prefab Mode.

    Using PrefabUtility.EditPrefabContentsScope I'm successfully able to add a new object to the prefab however I am unable to set the editor's selection to this new object. This is what my code looks like:

    Code (CSharp):
    1. if (GUILayout.Button("Add test object")) {
    2.     using (EditPrefabContentsScope editingScope = new EditPrefabContentsScope(assetPath)) {
    3.         GameObject root = editingScope.prefabContentsRoot;
    4.         GameObject newObj = new GameObject("TEST");
    5.  
    6.         newObj.transform.SetParent(root.transform);
    7.         Selection.objects = new Object[] {newObj};
    8.     }
    9. }
    This results in nothing being selected.
     
  2. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    217
    Your 'newObj' GameObject no longer exists after exiting the EditPrefabContentsScope. The newObj has been saved to the Prefab Asset file, then imported as part of the imported Prefab Asset artifact and finally merged to all instances in the open scenes.
     
  3. FritzC

    FritzC

    Joined:
    Nov 15, 2017
    Posts:
    15
    So is there any way to accomplish this?
     
  4. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    What would you like to have selected after running your script? The specific object in the asset? or the root GameObject in the asset as if the user selected the prefab in the project browser? or the object in some Prefab Instance in the scene?
     
  5. FritzC

    FritzC

    Joined:
    Nov 15, 2017
    Posts:
    15
    I am adding a GameObject as a child of a prefab that is open in Prefab Mode. I would like this new GameObject to be selected after adding it.
     
    Last edited: Dec 4, 2020
  6. FritzC

    FritzC

    Joined:
    Nov 15, 2017
    Posts:
    15
    Bumping
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    If it's already open in prefab mode, you don't have to use the editingScope, you can just put the object in there. The editing scope is for editing prefabs that are not open in edit mode.
     
    SteenLund likes this.
  8. FritzC

    FritzC

    Joined:
    Nov 15, 2017
    Posts:
    15
    I appreciate the tip however I'm still unable to set the selected object to a GameObject within the prefab editor.
     
  9. zloivan

    zloivan

    Joined:
    Mar 5, 2018
    Posts:
    4
    2022 and no answer yet...
     
  10. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    217
    As Baste stated you should not use EditPrefabContentsScope here, but use PrefabStage API instead.

    Try something like this:

    Code (CSharp):
    1.  
    2. static void OnSomeEventDuringPrefabMode()
    3. {
    4.     var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
    5.     if (prefabStage == null)
    6.         return;
    7.  
    8.     var yourGameObject = new GameObject("TheGameObject");
    9.     yourGameObject.transform.parent = prefabStage.prefabContentsRoot.transform;
    10.     EditorUtility.SetDirty(yourGameObject);
    11.  
    12.     Selection.activeGameObject = yourGameObject;
    13. }
    14.