Render prefab environment in game view when editing prefab?

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.

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:

CameraSwap code

using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor.Experimental.SceneManagement;
#endif

[ExecuteAlways]
public class CameraSwap : MonoBehaviour
{
#if UNITY_EDITOR
    Camera cam;
    bool showingPrefabScene = false;
    private void OnEnable()
    {
        cam = GetComponent<Camera>();
        PrefabStage.prefabStageOpened -= OnPrefabStageOpened;
        PrefabStage.prefabStageOpened += OnPrefabStageOpened;

        PrefabStage.prefabStageClosing -= OnPrefabStageClosing;
        PrefabStage.prefabStageClosing += OnPrefabStageClosing;
        OnPrefabStageOpened(PrefabStageUtility.GetCurrentPrefabStage());//check on recompile
    }
    private void OnDisable()
    {
        PrefabStage.prefabStageOpened -= OnPrefabStageOpened;
        PrefabStage.prefabStageClosing -= OnPrefabStageClosing;
        OnPrefabStageClosing(null);//ensure rendering regular scene again when closing and just before recompile
    }
   
    private void OnPrefabStageOpened(PrefabStage stage)
    {
        if( !showingPrefabScene )
        {
            if( stage != null )
            {
                cam.scene = stage.scene;//set camera to render preview scene
                showingPrefabScene = true;
            }
        }
    }
    private void OnPrefabStageClosing( PrefabStage stage )
    {
        if( showingPrefabScene )
        {
            cam.scene = SceneManager.GetActiveScene();//return to normal scene
            showingPrefabScene = false;
        }
    }
#endif
}

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 Likes

We would also benefit from having the scene view camera render the ‘Prefab Editing Enviroment’-scene’s skybox, when editing a prefab.

1 Like

Well, for those of you who still cannot have a better workaround, here is what I’ve got:

  • Simply create another empty scene, maybe name it ‘Prefab_previewing_scene’.
  • 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.
  • Setup your camera whatever the way you want to test it.
  • 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.

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.

camera.scene = gameObject.scene;
camera.Render();

Then camera.Render(); every frame I want to see an update.

5 Likes

This works like a charm, thanks :slight_smile: