Context Menu

A context menu is a menu in a graphical user interface that appears upon user interaction, such as a right-click mouse operation. A context menu offers a limited set of choises that are availible in the current state. Usually the availible choises are actions related to the selected object.

This script shows how to display a Context Menu.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DevionGames.UIWidgets;
using ContextMenu = DevionGames.UIWidgets.ContextMenu;
using UnityEngine.EventSystems;

public class ContextMenuTrigger : MonoBehaviour, IPointerDownHandler
{
    //Choices
    public string[] menu;

    private ContextMenu m_ContextMenu;

    // Start is called before the first frame update
    private void Start()
    {
        //Find the context menu widget in scene
        this.m_ContextMenu = WidgetUtility.Find<ContextMenu>("ContextMenu");
    }

    /// <summary>
    /// Detect current clicks on the GameObject (the one with the script attached)
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerDown(PointerEventData eventData)
    {
        //Check right click
        if (eventData.button == PointerEventData.InputButton.Right)
        {
            //Clear previous choices
            this.m_ContextMenu.Clear();
            for (int i = 0; i < menu.Length; i++)
            {
                string menuItem = menu[i];
                //Add new choice
                m_ContextMenu.AddMenuItem(menuItem, delegate { Debug.Log("Used - " + menuItem); });
            }
            //Show context menu
            this.m_ContextMenu.Show();
        }
    }
}