Not sure how to use the undo system.

How exactly do I get this to work, I copied most of it from the documentation, can someone help me out?

using UnityEditor;
using UnityEngine;

public class EasymapEditor : MonoBehaviour 
{
	[MenuItem("Tools/EasyMap")]
	static void EasyMap ()
	{
		//print ("EasyMap Spawned!");
		//Instantiate (Resources.Load ("EasyMap", typeof(GameObject)));
		//Undo.RegisterCreatedObjectUndo(EasyMap, "Create " + EasyMap);
		Instantiate (Resources.Load ("EasyMap", typeof(GameObject)));
		Undo.RegisterCreatedObjectUndo (EasyMap, "Create " + EasyMap.//EasyMap);
		Selection.activeObject = EasyMap;
	}
}

I know the error is were I put the two slashes, I put EasyMap there because I wasn’t sure what else to type there.

bump bump bump

I created a new empty project, added a cube with a rigidbody and attached your script from above and it worked. So the script is fine. For a test, can you download the complete space shooter tutorial from the asset store and check if it works?

Very strange. Just to be sure: The script you're using is the same that you posted here? And when you added the Debug.Log(), the output was x=0 y=0 even when you pressed any of the WASD keys? If yes, then i'm out of ideas.

2 Answers

2

First of all, don’t use Instantiate inside Editor code. Instantiate will break the prefab connection. Use PrefabUtility.InstantiatePrefab.

Next thing is you don’t do anything with the instance that the method returns. That’s the instance you want to register to the Undo system with Undo.RegisterCreatedObjectUndo.

GameObject prefab = (GameObject)Resources.Load ("EasyMap", typeof(GameObject));
GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
Undo.RegisterCreatedObjectUndo (instance, "Create  EasyMap");
Selection.activeObject = instance;

This of course assumes that you have a prefab inside the resources folder that is named “EasyMap”.

Thanks, that fixed it.

Here is your code with a few changes. Notice the new line:

Undo.IncrementCurrentGroup();

It allows for a “single object undo”, so only the next change will be undone.
Hope this helps!

 using UnityEditor;
 using UnityEngine;
 
 public class EasymapEditor : MonoBehaviour 
 {
     [MenuItem("Tools/EasyMap")]
     static void EasyMap ()
     {
         // single undo
         Undo.IncrementCurrentGroup();

         //print ("EasyMap Spawned!");
         //Instantiate (Resources.Load ("EasyMap", typeof(GameObject)));
         Instantiate (Resources.Load ("EasyMap", typeof(GameObject)));

         // add for undo
         Undo.RegisterCreatedObjectUndo(EasyMap, "Create" + EasyMap.name);

         Selection.activeObject = EasyMap;
     }
 }

@doublemax The ship in the complete game moves if that's what you mean. But in my project and scene, it won't move for whatever reason.

@doublemax You're referring to the Complete_Game scene already located in the Assets folder, right?