• 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
1
Question by yatagarasu · Jan 03, 2014 at 12:04 PM · editor-scripting

How to execute MenuItem for multiple objects once.

How can I write custom editor MenuItem command so it will be called for all selected objects.

Now it is called for every selected object, but I want to handle all selected objects in one command (through Selection.gameObjects i think) and suppress call for every selected object.

Comment
Add comment · Show 1
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
avatar image Torresmo · Mar 15, 2016 at 07:00 PM 0
Share

I am getting the same problem. It seems that an issue was filed: https://issuetracker.unity3d.com/issues/customeditor-multiple-fuction-call-from-custom-menuitem-when-calling-from-hierarchy

The solution proposed by gungnir works though.

5 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by gungnir · Feb 05, 2016 at 02:41 AM

Use the parametered version of the method:

 static public void MethodName(MenuCommand menuCommand)
 {
             //Prevent executing multiple times when right-clicking.
         if (Selection.objects.Length > 1)
         {
             if (menuCommand.context != Selection.objects[0])
             {
                 return;
             }
         }
 }
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
avatar image
4

Answer by electric_jesus · Jul 13, 2018 at 03:31 PM

Unfortunately the option above doesn't work if you want to manage selection inside your custom menu method. Here is my workaround:

 private static float _lastMenuCallTimestamp = 0f;
 [UnityEditor.MenuItem("GameObject/Menu Option", priority = 0)]
 private static void YourMenuOption() {
         if (Time.unscaledTime.Equals(_lastMenuCallTimestamp)) return;
         // place your code here
         _lastMenuCallTimestamp = Time.unscaledTime;
 }

I've also tried using EditorApplication.timeSinceStartup and Time.timeSinceStartup but I couldn't get a persistent timestamp with those. It seems like timeSinceStartup values are synchronized with the system time and keep updating even when Unity freezes (e.g. when you're trying to apply your custom menu method to a large number of objects).

Comment
Add comment · Show 4 · 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
avatar image lgarczyn · Dec 05, 2019 at 11:30 PM 0
Share

That is definitely a hack, there has to be a better way.

avatar image pdhr lgarczyn · Dec 06, 2019 at 09:44 AM 1
Share

Well, the Unity $$anonymous$$m themselves acknowledge this 'bug' and have said not planning to do anything about it in fear of breaking code where users rely on the current functionality. And the provide no proper alternative, so I think for the time being this is a perfectly fine workaround.

avatar image pdhr · Dec 05, 2019 at 11:31 PM 1
Share

This answer is (almost) good, but you just need to replace the Time.unscaledTime with EditorApplication.timeSinceStartup. Then it properly works in the editor.

avatar image Freznosis · May 14, 2020 at 04:30 AM 0
Share

I can't believe Unity don't plan on fixing this (or adding an option to ignore the rest of the selection). I'm glad this answer at least works. It is definitely a hacky workaround but I guess that's what you have to deal with when developing on an engine like Unity. Thank you electric_jesus!

(Also EditorApplication.timeSinceStartup didn't work for me either, Time.unscaledTime works fine)

avatar image
2

Answer by johnnyjacques · Apr 17, 2020 at 01:19 AM

Another option is to simply deselect the menu items after the first call.

  private static void YourMenuOption() {
          if (Selection.objects.Length <= 0) return;
          // place your code here
          Selection.objects = null;
  }




Comment
Add comment · Show 3 · 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
avatar image Delmadan · Apr 24, 2020 at 01:25 PM 0
Share

This should be the top answer.

avatar image pdhr Delmadan · Apr 24, 2020 at 01:28 PM 1
Share

It's not the best solution for all use cases though, because if you have a large, painstakingly selected selection, losing that selection after using this tool can be totally not what you want.

avatar image Delmadan pdhr · Apr 25, 2020 at 02:15 AM 0
Share

That's a very good point!

avatar image
0

Answer by LW · Jan 18, 2019 at 04:22 PM

Not an answer but crazily enough, if you call the menu item from the top menu bar (Unity 2018.2.19f1) it only calls it once aannndddd the MenuCommand.context is null... which is probably a bug.

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
avatar image
0

Answer by fcnaud · Nov 13, 2020 at 10:12 AM

as @LW said, MenuCommand.context could be null.

so try this code. Can work both right click and top bar menu.

 [MenuItem("GameObject/YourMenuItem", false, 49)]
 public static void YourMenuItem(MenuCommand menuCommand)
 {
     if (ShouldExecute(menuCommand))
     {
         Debug.Log($"{menuCommand.context} {Selection.activeObject}");
         // ... do something
     }
 }
 
 
 private static bool ShouldExecute(MenuCommand menuCommand)
 {
     if (menuCommand.context == null) return true;
 
     if (menuCommand.context == Selection.activeObject) return true;
 
     return false;
 }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 People are following this question.

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

Related Questions

What's the C# equivalent of @script ExecuteInEditMode() ? 2 Answers

How to identify an asset? 2 Answers

OnSceneGUI called without any object selected 4 Answers

C# Editor script in Unity 1 Answer

Is it possible to ensure that certain game objects don't get saved in the scene or otherwise hook into the default save scene to run custom editor code? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges