Undo.RecordObject freezing Unity

Hi

I’m currently making an Editor Extension which changes the topography of the terrain. I want to make sure you can undo the changes though, unfortunately onces I have changed the terrain and clicked undo Unity freezes. Here is a simplified version of the script i’m using:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class ResetTerrain : EditorWindow {

	[MenuItem ("Window/Reset Terrain")]
	static void ResetTerrain () {
		TerrainData tData = Terrain.activeTerrain.terrainData;
		Undo.RecordObject (tData, "Generate Terrain");
		int terrainWidthAsInt = tData.heightmapWidth;
		int terrainLengthAsInt = tData.heightmapHeight;
		float [,] heights = tData.GetHeights(0,0,terrainWidthAsInt,terrainWidthAsInt);
		for (int x = 0; x < terrainWidthAsInt; x++) {
			for (int y = 0; y < terrainLengthAsInt; y++) {
				heights[x,y] = 0;
			}
		}
		tData.SetHeights(0, 0, heights);
	}
}

This script simply sets the terrain to a flat plane. If I try to undo this step Unity freezes. If anyone has a solution or information on how to fix this please reply.

Thanks in advance.

I found that using RegisterCompleteObjectUndo (terrainData) rather than RecordObject (terrainData) fixes the problem.

It’s funny cause it seems to work with JavaScript and not C#.

I took this http://wiki.unity3d.com/index.php?title=HeightmapFromTexture and it worked as is.

I rewrote it in C# and it doesn’t. I remove the one line:
Undo.RecordObject(terrain.terrainData, “Heightmap From Texture”);
and it works as C# fine. I replaced that line with:
Undo.RegisterUndo(terrain.terrainData, “Heightmap From Texture”);
and it still works fine.