- Home /
Undo in Editor only executed when I switch focus to Hierarchy
I am currently trying to write a custom editor that uses drag and drop. I have managed to get it running by setting the controlID. Unfortunately, when I do this Undo only works when I switch focus away from the Scene. The weirdest thing is that while it does nothing the menu item for Undo still highlights. Am I somehow using the controlID in a wrong way, or do I need to forward the keyboard controls somehow?
I have built a minimal example, that has the problem. I add a behavior to a simple Game Object like a cube:
public class CubeBehavior : MonoBehaviour {
public int value = 0;
}
Next I create a custom editor that increases the value with every mouse click. I also register this change for undo. It works, but only once I set focus outside of the Scene.
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor (typeof(CubeBehavior))]
public class CubeEditor : Editor {
CubeBehavior cubeBehavior;
void OnEnable() {
cubeBehavior = (CubeBehavior)target;
SceneView.onSceneGUIDelegate -= OnScene;
SceneView.onSceneGUIDelegate += OnScene;
}
public override void OnInspectorGUI() {
EditorGUILayout.BeginHorizontal();
cubeBehavior.value = EditorGUILayout.IntField (cubeBehavior.value);
EditorGUILayout.EndHorizontal();
}
void OnScene(SceneView sceneview) {
Event e = Event.current;
int controlID = GUIUtility.GetControlID (FocusType.Passive);
EventType eventType = e.GetTypeForControl (controlID);
Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
Vector3 mousePos = r.origin;
if (eventType == EventType.MouseUp) {
Undo.RegisterUndo (target, "Edit " + target);
Undo.IncrementCurrentGroup ();
((CubeBehavior) target).value ++;
GUIUtility.hotControl = controlID;
e.Use ();
}
}
}
Your answer
Follow this Question
Related Questions
unity not undoing transform changes? 1 Answer
How to record hideFlags for Undo/Redo 0 Answers
Editor undo generic collection C# 0 Answers
Undo.RecordObject doesn't work 2 Answers