- Home /
Undo Class works inconsistently
I am absolutely stumped by Unity's Undo class. Everytime I use it, it seems pretty straight forward at first only to realize that it never works as expected later on. This time it's especially weird because occassionally it seems to work just as it should, but it also fails to undo stuff correctly half of the time. Namely, the _tiles variable of type List seems to mess up a lot. Half the time, when I destroy tiles using the DestroyTile function and undo it afterwards, the list is recreated so it has the same amount of entries it had previously, but all of them sans one now contain the Vector2 (0, 0) instead of the value it had. Both List and Vector2Int should be serialize, but since I wasn't sure about Vector2Int I also tried with the vanilla Vector2 with identical results. Any help would be appreciated. I also tried using Undo.RecordObject in the Create/Destroy functions themselves, but it didn't make a difference.
StageCreatorEditor:
private void OnSceneGUI() {
var e = Event.current;
var stageCreator = (StageCreator)target;
//Get control ID
var id = GUIUtility.GetControlID(6, FocusType.Passive);
//Place and remove tiles
if (e.type == EventType.MouseDown) {
GUIUtility.hotControl = id;
Undo.RegisterCompleteObjectUndo(stageCreator, "Stage Creator Change");
Undo.FlushUndoRecordObjects();
e.Use();
}
if (e.type == EventType.MouseDrag && GUIUtility.hotControl == id) {
switch (e.button) {
case 0:
stageCreator.CreateTile(cursorPosition);
e.Use();
break;
case 1:
stageCreator.DestroyTile(cursorPosition);
e.Use();
break;
}
}
if (e.type == EventType.MouseUp && GUIUtility.hotControl == id) {
GUIUtility.hotControl = 0;
e.Use();
}
}
StageCreator:
public List<Vector2Int> _tiles = new List<Vector2Int>();
//Create a tile
public void CreateTile(Vector2Int position) {
if (!_tiles.Contains(position) && CursorIsInBounds() && _selectedTile != null) {
_tiles.Add(position);
}
}
//Remove a tile
public void DestroyTile(Vector2Int position) {
if (_tiles.Contains(position) && CursorIsInBounds()) {
_tiles.Remove(position);
}
}
Your answer
Follow this Question
Related Questions
Undo has an interval within which it won't be registered 0 Answers
How to deal with object deletion Undo states and variables referencing the objects involved? 2 Answers
Why does UWP Package size getting so big? 1 Answer
Why Unity says my GPU is DX10 when it is DX12? 0 Answers
【LuminSDK Unity】Deploy to Magic Leap One and MLDB error: API level requested does not support 1 Answer