Search Unity

Render prefab environment in game view when editing prefab?

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

  1. noanoa

    noanoa

    Joined:
    Apr 17, 2014
    Posts:
    225
    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

    hunterofakara

    Joined:
    Oct 4, 2018
    Posts:
    21
    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
    6.  
    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;
    18.  
    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.     }
    29.    
    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).
     
    Seromu, rscopic and Singtaa like this.
  3. DriesVienne

    DriesVienne

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

    Nomosoro

    Joined:
    Jan 6, 2017
    Posts:
    3
    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.

    Note:
    • 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.
     
  5. Redhook_Galen

    Redhook_Galen

    Joined:
    Nov 21, 2018
    Posts:
    11
    I just ran into this while trying to get a character to render to a render texture for UI. The easiest way for our UX Designer to edit screens is in prefab mode but the camera doesn't render by default so I needed to call.

    Code (CSharp):
    1. camera.scene = gameObject.scene;
    2. camera.Render();
    Then camera.Render(); every frame I want to see an update.
     
    Seromu, kinondoni and IggyZuk like this.
  6. IggyZuk

    IggyZuk

    Joined:
    Jan 17, 2015
    Posts:
    43
    This works like a charm, thanks :)