Search Unity

Render prefab environment in game view when editing prefab?

Discussion in 'Prefabs' started by noanoa, Oct 9, 2018.

  1. noanoa


    Apr 17, 2014
    Is it possible to render prefab environment(and the prefab being edited) in game view when editing a prefab? I've tried making canvas overlay mode and attaching camera etc(in the prefab environment scene) but it seems when editing a prefab, game view is stuck to the scene I was before entering prefab editing view.
  2. hunterofakara


    Oct 4, 2018
    From what I can tell, the game view always renders the currently 'active' scene, which is never the prefab scene (Unity complains and messes up if you try to SetActiveScene to a preview/prefab scene).

    You can set a camera to render to a texture in the prefab environment scene, but you'd still need to display that texture in the active scene to see it.

    The simplest workaround I could get is to have a camera in your regular scene with a script attached to swap it's rendering scene to the prefab scene each time you edit a prefab:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. #if UNITY_EDITOR
    4. using UnityEditor.Experimental.SceneManagement;
    5. #endif
    7. [ExecuteAlways]
    8. public class CameraSwap : MonoBehaviour
    9. {
    10. #if UNITY_EDITOR
    11.     Camera cam;
    12.     bool showingPrefabScene = false;
    13.     private void OnEnable()
    14.     {
    15.         cam = GetComponent<Camera>();
    16.         PrefabStage.prefabStageOpened -= OnPrefabStageOpened;
    17.         PrefabStage.prefabStageOpened += OnPrefabStageOpened;
    19.         PrefabStage.prefabStageClosing -= OnPrefabStageClosing;
    20.         PrefabStage.prefabStageClosing += OnPrefabStageClosing;
    21.         OnPrefabStageOpened(PrefabStageUtility.GetCurrentPrefabStage());//check on recompile
    22.     }
    23.     private void OnDisable()
    24.     {
    25.         PrefabStage.prefabStageOpened -= OnPrefabStageOpened;
    26.         PrefabStage.prefabStageClosing -= OnPrefabStageClosing;
    27.         OnPrefabStageClosing(null);//ensure rendering regular scene again when closing and just before recompile
    28.     }
    30.     private void OnPrefabStageOpened(PrefabStage stage)
    31.     {
    32.         if( !showingPrefabScene )
    33.         {
    34.             if( stage != null )
    35.             {
    36.                 cam.scene = stage.scene;//set camera to render preview scene
    37.                 showingPrefabScene = true;
    38.             }
    39.         }
    40.     }
    41.     private void OnPrefabStageClosing( PrefabStage stage )
    42.     {
    43.         if( showingPrefabScene )
    44.         {
    45.             cam.scene = SceneManager.GetActiveScene();//return to normal scene
    46.             showingPrefabScene = false;
    47.         }
    48.     }
    49. #endif
    50. }

    You'd also have to make sure the camera it's attached to is pointed towards the origin or wherever your prefabs are located when editing.

    It still has some flaws I noticed:
    Seems to render the background/skybox of the active scene based on camera settings.
    Still doesn't show Debug.DrawLine (but will show custom gizmos if gizmo toggle is enabled on game view).
  3. DriesVienne


    Jun 22, 2013
    We would also benefit from having the scene view camera render the 'Prefab Editing Enviroment'-scene's skybox, when editing a prefab.
  4. Nomosoro


    Jan 6, 2017
    Well, for those of you who still cannot have a better workaround, here is what I've got:
    1. Simply create another empty scene, maybe name it 'Prefab_previewing_scene'.
    2. Drag your prefab into the scene, if it is a UI prefab, set up a canvas per your need and drag your UI prefab into it.
    3. Setup your camera whatever the way you want to test it.
    4. Open your prefab, edit it, until you wanna save the result, Ctrl+S to save the prefab view the change updated in the camera.

    • I know this is the obvious solution, I just wanna remind those who forget the simplest workaround is just to have these very basic setups and you are all to go to test your prefab's render result in Game View.
    • For those of you who enabled auto-save in prefab editing, you will find it update every time you modify the prefab. However, I will not recommend doing so, since your prefab will grow larger and probably gonna be nested in other prefabs, which will cause a chained prefab saving, usually cost 2-3s at least, and it will destroy your smooth editing experience.