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
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
6

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
1

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 · Show 1 · 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 dev_cwj · Oct 12, 2021 at 08:05 AM 1
Share

it could be just one line of code.

Please refer to following.

 if ((!menuCommand.context?.Equals(Selection.activeObject)) ?? false) return;
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

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

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

Related Questions

EditorGUI add SortingLayer-like list to custom Editor 1 Answer

Set default object EditorGUILayout.ObjectField 2 Answers

How to set a prefab SpriteRenderer.sprite using script in editor? 1 Answer

What is the name of this List-like Editor Control? 2 Answers

ExecuteInEditMode not working in some scripts 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