- Home /
Question by
$$anonymous$$ · Jun 18, 2020 at 02:07 PM ·
editor-scriptingundo
Undo in Editor function
Hi there,
I'm trying to add undo, to a small script I'm using to override the material of some objects but I can't make it work. I want a single Undo record, for all MeshRenderer updates. Any ideas what I'm doing wrong?
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class MaterialOverride
{
[MenuItem("Assets/Testing/MaterialOverride")]
public static void MaterialOverrideButton()
{
try
{
List<MeshRenderer> renderers = new List<MeshRenderer>();
Material file = (Material)Selection.activeObject;
foreach (ObjectProperties prop in GameObject.FindObjectsOfType<ObjectProperties>())
{
GameObject go = prop.gameObject;
MeshRenderer msh = go.GetComponent<MeshRenderer>();
if (msh)
{
msh.sharedMaterial = file;
renderers.Add(msh);
}
}
Undo.RecordObjects(renderers.ToArray(), "Override material");
}
catch
{
}
}
}
Best regards, Nick
Comment