- Home /
Override default drag and drop behavior inside editor
I'm trying to override the default drag and drop behavior inside the scene view for a prefab so the object is created through a factory instead of being directly instantiated. My idea is to intercept the drag and drop event in the OnSceneGUI method to introduce my behavior. Currently the method looks like this:
void OnSceneGUI() {
if (Event.current.type == EventType.DragExited) {
// i have to use a flag because the DragExited event is fired twice (¿?)
if(!_creating) {
WorldBuilder.Instance.AddPlatform( _myScript.length,_myScript.type);
_creating = true;
}
DragAndDrop.AcceptDrag();
Event.current.Use();
}
}
This creates the object via the Factory but it also instantiates a new one. I tried to clear the DragAndDrop.objectReferences array and add manually the factory object but it doesn't work. Any idea on how i can achieve this?
Answer by jorgeprodriguez · May 03, 2016 at 06:24 PM
It has been a while but i couldn't find an elegant solution to this problem. I thought about adding the current _myScript object to the Factory instead of creating a new one but that goes against the pattern so i scraped the idea. For now i'm just destroying the object created:
void OnSceneGUI() {
if (Event.current.type == EventType.DragExited) {
// i have to use a flag because the DragExited event is fired twice (¿?)
if(!_creating) {
WorldBuilder.Instance.AddPlatform( _myScript.length,_myScript.type);
_creating = true;
if (Application.isPlaying) {
GameObject.Destroy(_myScript.gameObject);
} else {
GameObject.DestroyImmediate(_myScript.gameObject);
}
}
Event.current.Use();
}
}
This is a suboptimal solution but at least it works.
Your answer
Follow this Question
Related Questions
Bezier curve that update in real time. 1 Answer
Handles.matrix seems strange? 2 Answers
DragAndDrop class 1 Answer
Keep multiple custom handles visible in the Scene view 0 Answers
Drag.Exited editor event confusion 2 Answers