- Home /
How to create persistent handles in Editor Scene View?
Hello!
I'm trying to draw a line between objects in the Scene View that will appear regardless of whether the object(s) are selected or not. I've figured out how to make the line appear when the object is selected. The same question was asked and answered ages ago (2011), but that solution is causing me errors.
Here's my current Code...
Saved in the Editor folder:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(LinkedObjectSet))]
class LinkedObjectLineDrawer : Editor
{
void OnSceneGUI()
{
LinkedObjectSet connectedObjects = target as LinkedObjectSet;
if (connectedObjects.linkedObjects == null)
{
return;
}
Vector3 center = connectedObjects.transform.position;
for (int i = 0; i < connectedObjects.linkedObjects.Length; i++)
{
GameObject connectedObject = connectedObjects.linkedObjects[i];
if (connectedObject)
{
Handles.DrawLine(center, connectedObject.transform.position);
}
}
}
}
Script attached to each empty game object which contains the objects that should have a line drawn between them:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LinkedObjectSet : MonoBehaviour {
public GameObject[] linkedObjects = null;
}
The topic is addressed here, where the solution appears to be to use the Delegate function of SceneView. I gave that a try below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(LinkedObjectSet))]
class LinkedObjectLineDrawer : Editor
{
void OnEnable()
{
SceneView.onSceneGUIDelegate += this.OnSceneGUI;
}
void OnDisable()
{
SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
}
void OnSceneGUI(SceneView sceneView)
{
LinkedObjectSet connectedObjects = target as LinkedObjectSet;
if (connectedObjects.linkedObjects == null)
{
return;
}
Vector3 center = connectedObjects.transform.position;
for (int i = 0; i < connectedObjects.linkedObjects.Length; i++)
{
GameObject connectedObject = connectedObjects.linkedObjects[i];
if (connectedObject)
{
Handles.DrawLine(center, connectedObject.transform.position);
}
}
}
}
...but then receive a host of errors and a blank Scene view in the editors. Errors include:
TargetParameterCountException: parameters do not match signature (etc.) GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced)
Which has left me out of my depths, especially as this seems to still be undocumented territory.
Thanks in advance if anyone can point me in the right direction!
Answer by Bunny83 · Oct 10, 2018 at 12:42 AM
What you're doing here is of course wrong. The Editor class is only relevant when an object is selected. It actually receives the "OnSceneGUI" message which does not take any parameters. If you registrating a manual scene view callback you have to use a different name for that method. You can name it whatever you like, just not any of the built-in callback names.
Since you want a permanent line drawn, just use Gizmos. For those you don't need a seperate editor script. Inside a MonoBehaviour class you can define the OnDrawGizmos method.
Your answer
Follow this Question
Related Questions
Editor Script Handles.Label problem. Labels don't move with gameobject 1 Answer
Showing Handles in the Game View 1 Answer
Drawing position handle from ScriptableObject editor. 2 Answers
Is it possible to determine wich Handles.PositionHandle is currently focused? 1 Answer
Using Handles.DrawAAPolyLine 1 Answer