Search Unity

I cannot manage to use Undo …

Discussion in 'Immediate Mode GUI (IMGUI)' started by Shudrum, Nov 6, 2015.

  1. Shudrum

    Shudrum

    Joined:
    Apr 3, 2011
    Posts:
    63
    Hi all !

    Maybe I'm stupid, but I can't manage to use Undo … I've tried many things, RecordObject, RecordObjects and other things, tried SetDirty, Repaint, etc … Nothing.

    This little example doesn't work … Can you help me ?

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. public class MyWindow : EditorWindow
    6. {
    7.     [MenuItem("Shudrum/My window")]
    8.     public static void ShowWindow()
    9.     {
    10.         EditorWindow thisWindow = EditorWindow.GetWindow(typeof(MyWindow));
    11.  
    12.         GUIContent title = new GUIContent();
    13.         title.text = "Toolbar";
    14.  
    15.         thisWindow.titleContent = title;
    16.     }
    17.  
    18.     void OnGUI()
    19.     {
    20.         if (GUI.Button(new Rect(0, 0, 32, 32), "Test")) {
    21.             Undo.RecordObjects(Selection.gameObjects, "Translate");
    22.  
    23.             foreach (GameObject currentGameObject in Selection.gameObjects) {
    24.                 currentGameObject.transform.position += Vector3.forward;
    25.             }
    26.         }
    27.     }
    28. }
    Thank you !
     
  2. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    I'm replying just so I get alerts on replies to see if you get anything more useful than I have in the past. Good luck with this. Signs of a bad system are:

    1) You can't find any useful information in the docs
    2) You can't find any useful information on google
    3) You can't get any useful information when you post on the official forum

    The unity undo system is a bad system. I've been trying to get it to work for months and have given up. I'm hoping my asset gets approved without undo functionality because its near impossible to figure out how to get it to work with anything more than the simplest use case.
     
  3. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    I believe it's not working as you're modifying the "Transform" not the "GameObject", because of this, unity doesn't detect any changes.

    In your loop try:

    Code (CSharp):
    1. Undo.RegisterCompleteObjectUndo(currentGameObject, "Translate");
    2. currentGameObject.transform.position += Vector3.forward;
    3. EditorUtility.SetDirty(currentGameObject);
    You could also try Recording the "Transform" instead of the "GameObject"