Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unitarian411 · Jul 22, 2012 at 09:31 PM · editortriggerexecuteineditmode

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:

  1. 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.

  2. 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
  }
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Run script in Edit mode when paused? 3 Answers

What Does "ExecuteInEditMode" Actually Do? 1 Answer

Can't open scripts with monodevelop? 1 Answer

AssetPreview.GetMiniTypeThumbnail not working for custom scripts 0 Answers

UniSciTE in version 3.x Script Edtior 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges