Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Preview for Prefabs

Discussion in 'Editor & General Support' started by Spy-Shifty, Aug 7, 2015.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Hi all,

    I want to rendere a preview of my Prefab in the Inspector like the preview of a normal model.

    So I have overridden the HasPreviewGUI (returns true) methode and the OnPreviewGUI.
    But I don't know how to render the prefab now!

    Can someone help me please...

    EDIT:

    I got this to work:
    Code (CSharp):
    1. private Camera cam;
    2.         public override void OnPreviewGUI(Rect r, GUIStyle background) {
    3.             base.OnPreviewGUI(r, background);
    4.             if (cam == null) {
    5.                 var camObj = new GameObject();
    6.                 camObj.hideFlags = HideFlags.HideAndDontSave;
    7.                 camObj.transform.position = new Vector3(0.5f, 2.0f, -5.0f);
    8.                 camObj.transform.eulerAngles = new Vector3(19.0f, -5.0f, 0.0f);
    9.                 cam = camObj.AddComponent<Camera>();
    10.                 cam.fieldOfView = 19;
    11.             }
    12.  
    13.             Handles.DrawCamera(r, cam, DrawCameraMode.TexturedWire);
    14.         }
    The problem:
    It also renders it in the Game View... and renders the Game View...

    Thx in advance.
     
    Last edited: Aug 7, 2015
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Not 100% if i understand you correctly but previewing a gameobject / prefab can be done via a custom editor.
    http://docs.unity3d.com/ScriptReference/Editor.OnPreviewGUI.html
    You have to change the editor style to shometing other thern in the example or it will torw errors.
    I would recommend EditorStyles.foldout for example
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Look like Prefabs with more then one object behave differently then prefabs with only one object. Sorry for the misinterpretation of your question.
     
  4. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Ok I solved my main problem....
    I want to draw icons for buttons in an editor window based on the preview actually...

    But I did something wrong on creating the prefab, so it has no preview for that.

    So the question is solved... I created a new prefab and I get now a preview!

    Thx you anyway :)
     
  5. startassets

    startassets

    Joined:
    Feb 13, 2017
    Posts:
    11
    Hello Spy-Shifty, I've just finished my own preview asset, maybe it will be useful for you: https://forum.unity3d.com/threads/powerful-preview.460157/.
    Looks and behaves just like the native preview.