- Home /
Question by
hexdump · Oct 05, 2016 at 11:42 PM ·
editor-scriptingonscenegui
How to draw a line in scene view by pressing and then dragging with the mouse?
Hi,
I would like to be able to draw a line between 2 gameobjects in sceneview. I have managed to detect the first click on the first object but when I drag unity starts to paint the selection box like it ussually does instead of my line.
Here the code I'm using:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(TriggerEventEmitter))]
public class EventBinding : Editor
{
// draw lines between a chosen game object
// and a selection of added game objects
Transform starting = null;
void OnSceneGUI()
{
// get the chosen game object
TriggerEventEmitter t = target as TriggerEventEmitter;
if (t == null)
return;
Event e= Event.current;
if(e.type == EventType.MouseDown)
{
Vector2 pos = Event.current.mousePosition;
Ray ray = HandleUtility.GUIPointToWorldRay(pos);
RaycastHit2D hit2d = Physics2D.GetRayIntersection(ray,Mathf.Infinity);
if (hit2d.collider != null)
{
starting = hit2d.collider.transform;
Debug.Log("Yeah");
Event.current.Use();
if (Event.current.type == EventType.Layout)
{
HandleUtility.AddDefaultControl(0);
}
}
}
else if(e.type==EventType.MouseDrag)
{
Color c = Handles.color;
Handles.color = Color.cyan;
Handles.DrawLine(starting.position, e.mousePosition);
Handles.color = c;
Event.current.Use();
}
}
}
Could anybody give any tip on this?
Thanks.
Comment
Your answer