Skip to content

Instantly share code, notes, and snippets.

@JoseMiguelPizarro
Created April 6, 2021 15:17
Show Gist options
  • Save JoseMiguelPizarro/cc137e959389016daf4a920ab50b7711 to your computer and use it in GitHub Desktop.
Save JoseMiguelPizarro/cc137e959389016daf4a920ab50b7711 to your computer and use it in GitHub Desktop.
Rendering preview window in Unity
using UnityEngine;
[CreateAssetMenu(menuName ="Data/PreviewData")]
public class PreviewData : ScriptableObject
{
public Color previewColor;
}
using UnityEditor;
using UnityEngine;
namespace Assets.PreviewSceneTests
{
[CustomEditor(typeof(PreviewData))]
public class PreviewDataEditor : Editor
{
private PreviewSceneWindow preview;
public override void OnInspectorGUI()
{
using (var changes = new EditorGUI.ChangeCheckScope())
{
base.OnInspectorGUI();
if (changes.changed)
{
if (preview != null)
preview.Refresh();
}
}
if (GUILayout.Button("Preview"))
preview = PreviewSceneWindow.Open(target as PreviewData);
}
}
}
using UnityEngine;
public class PreviewObject : MonoBehaviour
{
[SerializeField]
private MeshRenderer meshRenderer;
public void SetPreviewData(PreviewData data)
{
meshRenderer.sharedMaterial.color = data.previewColor;
}
}
using UnityEditor;
using UnityEngine;
public class PreviewSceneWindow : EditorWindow
{
public static PreviewSceneWindow Open(PreviewData data)
{
var window = GetWindow<PreviewSceneWindow>(typeof(SceneView));
window.SetData(data);
return window;
}
private PreviewData data;
private PreviewRenderUtility preview;
private PreviewObject previewObject;
private Camera camera => preview.camera;
public void OnEnable()
{
preview = new PreviewRenderUtility();
SetupPreviewObject();
SetupCamera();
}
public void OnDisable()
{
preview.Cleanup();
}
private void OnGUI()
{
DrawPreview();
}
public void Refresh()
{
previewObject.SetPreviewData(data);
Repaint();
}
private void SetupPreviewObject()
{
previewObject = CreatePreviewObject();
preview.AddSingleGO(previewObject.gameObject);
}
private void SetupCamera()
{
camera.farClipPlane = 100;
camera.fieldOfView = 40;
camera.transform.position = new Vector3(0, 0, -10);
}
private PreviewObject CreatePreviewObject()
{
var oreviewObject = Resources.Load<PreviewObject>("PreviewCube");
return Instantiate(oreviewObject);
}
private void DrawPreview()
{
using (new GUILayout.VerticalScope())
GUILayout.FlexibleSpace(); //This trick calculates the rect's dimensions
var rect = GUILayoutUtility.GetLastRect();
if (Event.current.type == EventType.Repaint)
{
preview.BeginPreview(rect, GUIStyle.none);
preview.Render(true);
var previewTexture = preview.EndPreview();
GUI.DrawTexture(rect, previewTexture, ScaleMode.StretchToFill, false);
}
else if (Event.current.type == EventType.MouseDrag)
{
HandleMouseDrag();
Repaint();
}
}
private void HandleMouseDrag()
{
var drag = new Vector3(Event.current.delta.y, Event.current.delta.x, 0);
var rotation = Quaternion.Euler(-drag);
previewObject.transform.rotation *= rotation;
}
public void SetData(PreviewData data)
{
this.data = data;
Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment