Unfortunately, you can only browse your own bug reports unless someone gives you the URL to one of theirs - and then you can see all of theirs as well.
It’s over a year later. Do we have any word on how to actually implement our own Undo behavior?
Okay, sorry, I was checking the iPhone docs instead of the 2.6 ones. I don’t find it clear on how to use something other than an undo for the entire scene, if you need to use DestroyImmediate on something. Is that even possible?
Regardless, while scene undo works fine in Unity, it crashes Unity iPhone every time. Lame. Please hurry up with 3.0!
I posted a simple editor script that moves a game object to (0, 0, 0) and implements undo behaviour. There’s not much to it, just make sure that the objectToUndo parameter is the specific component that you wish to be reset. That is, the script I posted wouldn’t work properly if the RegisterUndo function was supplied with the game object rather than the transform.
You’ve tried that with a DestroyImmediate somewhere? Also, doesn’t your script make it so that you’d need to use an undo for every moved game object, in a random order, instead of one undo for the whole operation?
No, I was just providing an example of where undo does work, since the previous posts might give the impression that the feature is broken completely. Unfortunately, I doubt that undoing after a DestroyImmediate is possible. As I understand it, calling RegisterUndo saves the component’s parameters in memory then restores them on undo, which wouldn’t work if the object no longer exists.
Correct. I agree that it’s not ideal, though I usually only move a single object at a time anyway, or don’t need to undo after moving multiple objects.
Actually no, when you make a bunch of undo operations at once, then Unity is smart enough to undo the whole lot in one go. Therefore, I would change this code:
static void MenuMoveToOrigin () {
// Move each selected transform to (0, 0, 0)
foreach (Transform t in Selection.transforms) {
Undo.RegisterUndo(t, "Move " + t.name);
t.position = Vector3.zero;
Debug.Log("Moving " + t.name + " to origin");
}
}
to this:
static void MenuMoveToOrigin () {
// Move each selected transform to (0, 0, 0)
if (Selection.transforms.Length > 1) {
Undo.RegisterUndo(Selection.transforms, "Move Transforms To Origin");
}
else {
Undo.RegisterUndo(Selection.activeTransform, "Move " + Selection.activeTransform.name + " To Origin");
}
foreach (Transform t in Selection.transforms) {
t.position = Vector3.zero;
Debug.Log("Moving " + t.name + " to origin");
}
}
Because otherwise the undo label is misleading if there’s more than one object in your selection. If you leave the individual RegisterUndo operations in the loop, you still only get one undo action out of it regardless if there’s one transform or many. (Which can be useful in some cases–I discovered this when making Stitchscape.)
Even though this thread is really old, it is exactly what I was looking for.
I setup my editor script the exact same way you have yours setup above, except I don’t use “Selection.Transforms”, here’s my code below:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class PreviewLighting : ScriptableWizard {
static Shader vertexLitShader = Shader.Find( "SRShaders/Outlined/Outline Diffuse Vertex Lit" );
[MenuItem ("Tools/Preview Lighting")]
static void ReplaceShaders ()
{
Shader rShader = Shader.Find( "SRShaders/Outlined/Outline Diffuse" );
Renderer[] renderers = FindObjectsOfType( typeof( Renderer )) as Renderer[];
Debug.Log(rShader.name);
Undo.RegisterUndo(renderers, "Mass Set Materials");
// Only iterate through renderers to make sure we don't try to access empty game objects( parents ).
foreach( Renderer rend in renderers )
{
rend.sharedMaterial.shader = rShader;
}
Debug.Log("oldShaders: " + oldShaders.Length);
}
}
But the undo doesn’t work, so my question is, is it possible to Undo assigning different shaders to all objects in the scene, or is Undo only for things like position, rotations, scale, ect…?
hi i just stumbled across this and was a big help, however because ive only been self teaching myself java and c# for a couple months this niggled me as theres no real explanation that a noob like me can understand lol.
i finally got it working and realised that it must be before the action takes place in order for it to save the operation. obvious now that i think about it as i wouldn’t put a destroy command at the start of a script.
i also didn’t find anywhere a clear statement which says all you need to do is put one line of code before the operation takes place
Undo.RegisterUndo(Selection.transforms, “undo”);
i found all the examples have a load of other bits of code which are confusing to a learner