Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question ContextMenuItem with parameter

Discussion in 'Editor & General Support' started by dvillaverde-drakhar, Nov 11, 2022.

  1. dvillaverde-drakhar

    dvillaverde-drakhar

    Joined:
    Aug 29, 2018
    Posts:
    11
    Hi,

    Is any way of making ContextMenuItem methods context dependent the same way as MenuItem does?

    For example, turn this:
    Code (CSharp):
    1. [ContextMenuItem("Sort", nameof(SortFirstList))]
    2. public List<int> firstList;
    3.  
    4. [ContextMenuItem("Sort", nameof(SortSecondList))]
    5. public List<int> secondList;
    6.  
    7. public void SortFirstList()
    8. {
    9.     firstList.Sort();
    10. }
    11.  
    12. public void SortSecondList()
    13. {
    14.     secondList.Sort();
    15. }
    into this:
    Code (CSharp):
    1. [ContextMenuItem("Sort", nameof(SortList))]
    2. public List<int> firstList;
    3.  
    4. [ContextMenuItem("Sort", nameof(SortList))]
    5. public List<int> secondList;
    6.  
    7. public void SortList(MenuCommand command)
    8. {
    9.     (command.context as List<int>).Sort();
    10. }
    Thank you