- Home /
Manually triggering a script from the editor (utility, macro etc.)
I am wondering whether I can use the power scripts to save me some significant manual editing/setup. I'll go into my particular scenario below, but I think the utility of doing this extends to a wide range of "edit time" utility scripts that basically function as macros, doing what you would otherwise have to tediously do by hand.
The question is not so much how to write these scripts, that is straightforward. But how to trigger them from the editor (i.e. only when I want to explicitly do so)
My scenario is: I am using unityCar to do a drving simulation. I'd like to duplicate cars but give them different paint jobs (green, yellow, etc.) Now I could go in after selecting and duplicating my original and modify each relevant material of the mesh renderers in the clone. But I'd much rather just have a script do that once. After that the color of the car is changed and I don't have to call the script again (unless I want to change it again in future)
Setting aside this particular use, I think there are probably many other applications of this type of utility script or almost a editing "macro" for various purposes.
Questions:
how would I trigger execution of such a script from the editor ? I am aware of [the [ExecuteInEditMode] directive][1], which allows scripts to be run in Edit mode also, but they trigger automatically on Update, OnGUI, OnRenderObject which is not really whats called for here. I'd want an explicit, manual trigger for the script.
whats a good place to attach the script to (I'd assumed for simplicity to the car prefab itself so the object the script applies to is already implied, but I guess the script itself is really a "global static" - no big deal either way, just wanted an opinion if theres a good reason to do it one way vs the other
[1]: http://docs.unity3d.com/Documentation/ScriptReference/ExecuteInEditMode.html
Answer by VivienS · Jul 27, 2012 at 07:58 AM
Hi there.
I came across this neat little script a while ago and it saved me a lot of time since! (It is also on UnifyCommunity, but the page is offline, as I'm posting this):
https://gist.github.com/975335
It uses the undocumented MacroEvaluator class of Unity. Works in 3.4.x and 3.5.x though. From what I understand, MacroEvaluator takes only JavaScript code.
Cheers, Vivien
Here's the code, btw., as it seems the link is not working! (source is of course the above link.)
using UnityEditor;
using UnityEditor.Macros;
using UnityEngine;
/// <summary>
/// Allows use of the undocumented MacroEvaluator class. Use at your own risk.
/// </summary>
public class Macros : EditorWindow
{
string macro = "";
/// <summary>
/// Adds a menu named "Macros" to the Window menu.
/// </summary>
[MenuItem ("Window/Macros")]
static void Init ()
{
CreateInstance<Macros>().ShowUtility();
}
void OnGUI ()
{
macro = EditorGUILayout.TextArea(macro, GUILayout.ExpandHeight(true));
if (GUILayout.Button("Execute")) {
MacroEvaluator.Eval(macro);
}
}
}