Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

What is PreviewSceneStage?

Discussion in '2020.1 Beta' started by DoctorShinobi, Oct 17, 2019.

  1. DoctorShinobi

    DoctorShinobi

    Joined:
    Oct 5, 2012
    Posts:
    219
    In the new alpha release there's a patch node:
    .
    What does it mean?
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    The online scripting reference doesn't seem to be up to date with the latest alpha, but if you install the docs locally, they should be. Search for
    PreviewSceneStage
    .
     
  3. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Examples of stages include Main Stage (where your regular scenes live) and Prefab Stage (where you edit Prefabs). Stages show up in the breadcrumb bar at the top of the Scene View, except when you're in Main Stage. By making a class that inherits from PreviewSceneStage, you can now make your own type of stage as well, for custom editor tooling purposes.
     
    phobos2077, DrummerB, Djayp and 6 others like this.
  4. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    It might serve your purposes. For now, what you can do with PreviewSceneStage is similar to Prefab Mode in isolation - that is, you won't see your normal scenes at the same time while your custom stage is open.
     
  6. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    phobos2077 and DoctorShinobi like this.
  7. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    Oh boy, I'm so excited this is a thing!!!!!!
     
  8. Wobbers

    Wobbers

    Joined:
    Dec 31, 2017
    Posts:
    55
    Sounds great, but how do to get started with this?
    I have this so far but calling Show() doesn't do anything. What else is required?
    Code (CSharp):
    1. public class StageTest : PreviewSceneStage
    2. {
    3.     protected override GUIContent CreateHeaderContent()
    4.     {
    5.         return new GUIContent("Test");
    6.     }
    7.  
    8.     protected override void OnCloseStage()
    9.     {
    10.         Debug.Log("Bye Stage");
    11.     }
    12.  
    13.     [MenuItem("stages/test")]
    14.     static void Show()
    15.     {
    16.         var inst = CreateInstance<StageTest>();
    17.         inst.scene = EditorSceneManager.NewPreviewScene();
    18.         inst.OnFirstTimeOpenStageInSceneView(SceneView.currentDrawingSceneView);
    19.         inst.OnOpenStage();
    20.     }
    21. }
     
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
  10. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Wobbers likes this.
  11. bigcheese_

    bigcheese_

    Joined:
    Sep 6, 2011
    Posts:
    31
    I know this is question probably doesn't fit here, but I've struggled to find anything about stage for 2019.4, even though I can use the experimental PrefabStage and PrefabStageUtility, I can't find any way to open those stages, any chance you can put me in the right right direction. Is there any proper use of stages for 2019.4?
     
  12. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    You can't manually create a new stage in 2019.4; only the functionality here is exposed:
    https://docs.unity3d.com/2019.4/Documentation/ScriptReference/SceneManagement.StageUtility.html

    A new stage is automatically created if you open a Prefab Asset using AssetDatabase.OpenAsset:
    https://docs.unity3d.com/2019.4/Documentation/ScriptReference/AssetDatabase.OpenAsset.html
     
    Samuel411 likes this.
  13. Samuel411

    Samuel411

    Joined:
    Dec 20, 2012
    Posts:
    646
    @runevision Here's a little video of something I worked on to allow for multiple scene views to be open. There's an issue where it won't work unless you have a prefab staged but it works well to explain the concept. http://samuelarminana.com/u/1478894f9-6efc-46bf-a972-50eaeab4bd69.mp4

    Do you see a future where the ability to open/close stages is opened?
    Also I noticed that the stage breadcrumbs for the prefab editing is always one layer deep, is there situations where that isn't the case?

    It'd be really awesome if you opened prefabs into different scene views and the hierarchy updated based on the focus or preview scene views had their own internal hierarchy.

    Here's the code for any reference, its simple.
    * Any prefab, doesn't matter which, must be opened/staged before you preview an object, otherwise the PreviewScene will not load any objects.

    Code (CSharp):
    1.  
    2.  
    3. public class PreviewWindow : SceneView
    4. {
    5.     public UnityEngine.Object selectedObj;
    6.     public Scene sceneLoaded;
    7.  
    8.     [MenuItem("Assets/Preview/Preview Asset")]
    9.     public static void ShowWindow()
    10.     {
    11.         if (Selection.objects.Length > 1)
    12.         {
    13.             Debug.LogError("Your selection must include a single object.");
    14.             return;
    15.         }
    16.         else if (Selection.objects.Length <= 0)
    17.         {
    18.             Debug.LogError("No object selected to preview.");
    19.             return;
    20.         }
    21.  
    22.         if (Selection.activeGameObject == null)
    23.         {
    24.             Debug.LogError("No Game Objects selected, only GameObjects/Prefabs are supported now");
    25.             return;
    26.         }
    27.  
    28.         // Create the window
    29.         PreviewWindow window = CreateWindow<PreviewWindow>("Preview");
    30.  
    31.         // Get the object you're selecting in the Unity Editor
    32.         window.selectedObj = Selection.activeObject;
    33.         window.titleContent = window.GetName();
    34.  
    35.         // Load a new preview scene
    36.         scene = EditorSceneManager.NewPreviewScene();
    37.      
    38.         window.sceneLoaded = scene;
    39.         window.sceneLoaded.name = window.name;
    40.         window.customScene = window.sceneLoaded;
    41.  
    42.         window.drawGizmos = false;
    43.  
    44.         window.SetupScene();
    45.  
    46.         window.Repaint();
    47.     }
    48.  
    49.     private static Scene scene;
    50.  
    51.     public override void OnEnable()
    52.     {
    53.         base.OnEnable();
    54.  
    55.         // Set title name
    56.         titleContent = GetName();
    57.     }
    58.  
    59.     public override void OnDisable()
    60.     {
    61.         base.OnDisable();
    62.     }
    63.  
    64.     private new void OnDestroy()
    65.     {
    66.         base.OnDestroy();
    67.     }
    68.  
    69.     void SetupScene()
    70.     {
    71.         // Create lighting
    72.         GameObject lightingObj = new GameObject("Lighting");
    73.         lightingObj.transform.eulerAngles = new Vector3(50, -30, 0);
    74.         lightingObj.AddComponent<Light>().type = UnityEngine.LightType.Directional;
    75.  
    76.         // Create the object we're selecting
    77.         GameObject obj = GameObject.Instantiate(selectedObj as GameObject);
    78.  
    79.         // Move the objects to the preview scene
    80.         EditorSceneManager.MoveGameObjectToScene(obj, customScene);
    81.         EditorSceneManager.MoveGameObjectToScene(lightingObj, customScene);
    82.  
    83.         Selection.activeObject = obj;
    84.  
    85.         // Zoom the scene view into the new object
    86.         FrameSelected();
    87.     }
    88.  
    89.     private GUIContent GetName()
    90.     {
    91.         if (selectedObj == null)
    92.             return new GUIContent("NuLL");
    93.  
    94.         // Setup the title GUI Content (Image, Text, Tooltip options) for the window
    95.         GUIContent titleContent = new GUIContent(selectedObj.name);
    96.         if (selectedObj is GameObject)
    97.         {
    98.             titleContent.image = EditorGUIUtility.IconContent("GameObject Icon").image;
    99.         }
    100.         else if (selectedObj is SceneAsset)
    101.         {
    102.             titleContent.image = EditorGUIUtility.IconContent("SceneAsset Icon").image;
    103.         }
    104.  
    105.         return titleContent;
    106.     }
    107.  
    108.     new void OnGUI()
    109.     {
    110.         base.OnGUI();
    111.     }
    112. }
    113.  
     
    Last edited: Sep 24, 2020
    Midiphony-panda and Prodigga like this.
  14. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    We considered something like that (along with other variations of addressing the same use case) when we originally developed the improved Prefabs featureset for 2018.3 but there's a range of technical challenges that would need to be solved to be able to make it work nicely and robustly and it was not viable at the time.

    It's something we're aware of, but we're currently deeming it not as vital as many other requested Prefab improvements (that are also requested much more frequently), so we do not currently have plans to work on it in the foreseeable future.
     
  15. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Is there a roadmap or anything similar for what the prefab department is working on? What are these 'frequently requested improvements'? Sounds like your department has a plan, why not share that on the roadmap page.
     
    DoctorShinobi likes this.
  16. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    I'll forward the feedback about the roadmap page.

    Too see some of the things we've been working on lately, have a look at this video:

    Right now we're primarily focused on fixing bugs though.
     
    Prodigga likes this.
  17. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    Bump

    It's quite tricky to find documentation about those subjects, aside from @Samuel411 findings (nice investigation btw).