- Home /
DrawCamera and Gizmos in EditorWindow
Hi,
How do I draw Gizmos in the Handles.DrawCamera render? It says in the documentation that it would draw gizmos and handles in the returned render, but with no luck... here is the code:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class TileEditor : EditorWindow {
private const int __sideBarWidth = 200;
private Camera __camera;
private Tile __selectedTile;
[MenuItem("Level Designer/Tile Editor... %k")]
public static void ShowTileEditor() {
TileEditor window = EditorWindow.GetWindow<TileEditor>("Tile editor");
window.autoRepaintOnSceneChange = true;
}
[MenuItem("Level Designer/Tile Editor... %k", true)]
private static bool ShowTileEditorValidator() {
return true;
}
private void OnFocus() {
this.__selectedTile = SectionEditor.selectedTile;
if (this.__selectedTile != null) {
this.__camera = EditorUtility.CreateGameObjectWithHideFlags("editorCamera", HideFlags.HideInHierarchy, typeof(Camera), typeof(Light)).GetComponent<Camera>();
Handles.SetCamera(this.__camera);
this.__camera.GetComponent<Light>().range = 200;
this.__camera.backgroundColor = Color.blue;
this.__camera.transform.position = this.__selectedTile.transform.position;
this.__camera.transform.position += (Vector3.up) * 100;
this.__camera.transform.LookAt(this.__selectedTile.transform.position);
}
}
private void OnDestroy() {
this.OnLostFocus();
}
private void OnLostFocus() {
if (this.__camera != null) {
Object.DestroyImmediate(this.__camera.gameObject);
this.__camera = null;
}
}
private void OnGUI() {
if (this.__camera != null) {
Handles.DrawCamera(new Rect(0, 0, this.position.width, this.position.height), this.__camera);
Handles.PositionHandle(this.__selectedTile.transform.position, Quaternion.identity);
Handles.BeginGUI();
{
Gizmos.DrawCube(Vector3.zero, Vector3.one);
GUI.enabled = this.__selectedTile.forwardTwist == 0;
EditorGUILayout.LabelField("Skew", EditorStyles.miniBoldLabel);
this.__selectedTile.skew = EditorGUILayout.IntSlider(this.__selectedTile.skew, -90, 90, GUILayout.Width(TileEditor.__sideBarWidth));
GUI.enabled = this.__selectedTile.skew == 0;
EditorGUILayout.LabelField("Twist", EditorStyles.miniBoldLabel);
this.__selectedTile.forwardTwist = EditorGUILayout.IntSlider(this.__selectedTile.forwardTwist, -90, 90, GUILayout.Width(TileEditor.__sideBarWidth));
GUI.enabled = true;
}
Handles.EndGUI();
this.Repaint();
}
}
[DrawGizmo(GizmoType.NotSelected|GizmoType.Selected|GizmoType.SelectedOrChild|GizmoType.Active|GizmoType.Pickable)]
private static void RenderSectionGizmo(Section section, GizmoType gizmoType) {
section.OnDrawGizmos();
}
}
So both the Gizmo and the Handle (PositionHandle) are not being drawn. What could be going on? The GUI on the other hand IS drawn. Also the DrawGizmo attribute does not work, yet it shows everytime the objects it SHOULD draw (checked it by Debug.Logging the name of the objects shown).
Thnx!
Answer by Bunny83 · Sep 10, 2013 at 07:55 AM
You should call Handles.SetCamera every OnGUI vefore you call DrawCamera. The Handles class is used by all SceneViews, so calling SetCamera once won't help much.
Thank you sir! You are a life saver! It was not working as expected, the Handles looked all messed up, this was the thing I was looking for! Big up!
This used to work in Unity 3.5, but in Unity 4.x the dimensions for SetCamera and DrawCamera don't work any more. It seems to be coupled with the size of the Game viewport (to see everything I want, I need to fullscreen my Game viewport, otherwise it only shows a part of what I am doing). Any ideas how to fix that.
Answer by jeev · Sep 10, 2013 at 07:25 AM
Oohh interesting! For those of you reading, the handles are actually drawn in the Editor window, using Vector2s! Using HandleUtility you can convert these (through a rather complicated chain of methods) to World coordinates. This applies only to Handles though, not to Gizmos, that still does not work, but I guess it would make sense that you would just add these manually based on GUI coordinates.