- Home /
Editor GUI: Object preview icon
Hi
I'm trying to show icons of game objects in my custom inspector attribute, I would like to use a small picture of the game object in a button, exactly like the ones we can find in this video.
The question is, given an GameObject, how do I display its icon? Something like: GUILayout/EditorGUILayout.Button(x) where x is the desired parameter, which is supposed to be the preview icon for the prefab.
Answer by timviana · Apr 01, 2012 at 02:21 AM
I found the answer:
[Formerly]Texture2D EditorUtility.GetAssetPreview(Object obj);
[Currently]Texture2D AssetPreview.GetAssetPreview(Object obj);
This function creates an object preview snapshot and returns as Texture2D.
Credit goes to blikstad on IRC: http://forum.unity3d.com/threads/128626-Asset-Preview-level-design-tool.
Perhaps you would consider voting for a rock-solid implementation of EditorUtility.GetAssetPreview
via http://feedback.unity3d.com/forums/15792-unity/suggestions/2834294-solid-implementation-of-editorutility-getassetprev
Updated answer, if someone gets here in 2014:
it's Texture2D AssetPreview.GetAssetPreview(Object obj); and AssetPreview is in UnityEditor namespace
thank you- I've edited the answer since I missed your comment initially!
Answer by Bunny83 · Apr 01, 2012 at 01:33 AM
Unity uses their internal class PreviewRenderUtility to directly render a mesh preview in the inspector or the ObjectSelector. As already said it's an internal class so it can't be used (at least not that easy).
This internal class needs to use RenderTextures to be able to render to a texture. There's also the internal function EditorUtility.SetTemporarilyAllowIndieRenderTexture()
which can do this, but as "user" you have no direct access to those functions.
If you have Unity pro you can create your own preview renderer or use the DrawMesh function to directly draw the mesh on the button. In the indy version it's getting a bit more complicated. You can also use a temporary camera and render the object directly, but i guess it takes a lot fiddling to get it working.
I found the answer on IRC, I tried to post here but it's waiting a moderator, it's an undocumented function and it isn't internal.