I am building a custom window and I am trying to reuse Unity's Scene view to be able to draw directly from this specific window.
I manage to reproduce the correct window by extend UnityEditor.SceneView and here's what I have:
And here's the code:
[EditorWindowTitle(title = "Shape Editor", useTypeNameAsIconName = false)]
public class StrokeEditor : SceneView
{
[MenuItem("Recognizer/Shape Editor")]
public static void Init()
{
var w = GetWindow<StrokeEditor>();
w.in2DMode = true;
EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
}
protected override void OnGUI()
{
using (new GUILayout.HorizontalScope())
{
GUILayout.Button("Add Stroke");
GUILayout.Button("Edit Stroke");
GUILayout.Button("Delete Stroke");
}
base.OnGUI();
}
}
With this, I might be almost done.
Is this the right way to procede ?
I feel that something is wrong because whenever I use EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);, it creates also a new scene to the main scene view. (I want the main scene view to stay unchanged)
I should also be able to see the tools from the scene view like:
Is there any better way to achieve what I want ?
EDIT 1:
The final usage of all of this is to be able to draw 2D shapes within the window by clicking and dragging the mouse like gestures with mobile phones. From that, I'll be able to get some of the position to feed one of my algorithm...



You can use the new
GraphView. This gives you some of the things you are looking for for "free", mainly to zoom and pan the view. Since ShaderGraph uses this, it should be easier to construct nodes, select them and move them around, if that is something you want to.Here is a toy example of a custom editor window that allows you to edit list of points in a scriptable object:
Shape.cs
- simple scriptable object with a list of points.
ShapeEditorWindow.cs
- editor window with a toolbar and a graphview that opens scriptable objects of type Shape.
ShapeEditorGraphView.cs
- graphview with zoom, grid, pan (with ContentDragger) and shape editor.
Unfortunately the grid background and the grid lines are the same color, so in order to see the grid lines we have to write a style sheet and set the GridBackground properties. This file has to be in Editor/Resources, and gets loaded with
styleSheets.Add(Resources.Load<StyleSheet>("ShapeEditorGraph"));Editor/Resources/ShapeEditorGraph.uss
ShapeManipulator.cs
- draws and edits the shape. This is similar to RectangleSelector.
The is just example code. The goal was to have something working and drawing to the screen.