Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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