celinedrules

CustomContextMenu

Jul 11th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEngine;
  5.  
  6. public class CustomWindow : EditorWindow
  7. {
  8.     private bool showContextMenu;
  9.     private Vector2 mousePosition;
  10.     private List<string> menuItems;
  11.     private GUISkin contextMenuSkin;
  12.     private GUIStyle buttonStyle;
  13.     private float buttonHeight;
  14.     private Vector2 scrollPosition;
  15.     private string searchString;
  16.     private float topOffset = 25f;
  17.  
  18.     // Add a menu to the Unity Editor which when selected
  19.     // will call this static method
  20.     [MenuItem("My Window/Show")]
  21.     public static void ShowWindow()
  22.     {
  23.         // Create a new instance of the window
  24.         CustomWindow window = GetWindow<CustomWindow>();
  25.         // This isn't required it just prevents the window
  26.         // from being resized smaller than 800x600. The same
  27.         // can be done for the maxSize
  28.         window.minSize = new Vector2(800, 600);
  29.         // Show the newly created window
  30.         window.Show();
  31.     }
  32.  
  33.     private void OnGUI()
  34.     {
  35.         // Check if the skin has been loaded, if not load it
  36.         if (contextMenuSkin == null)
  37.         {
  38.             contextMenuSkin = AssetDatabase.LoadAssetAtPath<GUISkin>("Assets/ContextMenuSkin.guiskin");
  39.             buttonStyle = contextMenuSkin.GetStyle("contextMenuButton");
  40.         }
  41.  
  42.         // Specifies where and how big to draw the menu
  43.         Rect clickedArea = new Rect(mousePosition.x, mousePosition.y, 120, (6 * buttonHeight) + 5 + topOffset);
  44.        
  45.         // Get the current event being processed
  46.         Event current = Event.current;
  47.        
  48.         // Check to see if the the mouse was clicked
  49.         if (current.type == EventType.MouseDown)
  50.         {
  51.             // Check if it was the right mouse button
  52.             if (current.button == 1)
  53.             {
  54.                 if (!showContextMenu)
  55.                 {
  56.                     // If we aren't currently showing the context
  57.                     // menu, get the current mouse position and
  58.                     // set showContextMenu to true
  59.                     mousePosition = current.mousePosition;
  60.                     showContextMenu = true;
  61.                 }
  62.                 else
  63.                 {
  64.                     // Hide the context menu
  65.                     showContextMenu = false;
  66.                 }
  67.                
  68.                 // Use the event causing other GUI elements to ignore it
  69.                 current.Use();
  70.             }
  71.             else
  72.             {
  73.                 // If we click outside the menu we want to hide it
  74.                 if (!clickedArea.Contains(current.mousePosition))
  75.                     showContextMenu = false;
  76.             }
  77.         }
  78.        
  79.         // Initialize the list of menu items
  80.         menuItems = new List<string>();
  81.        
  82.         // Add simple strings to the list
  83.         for(int i = 0; i < 6; i++)
  84.             menuItems.Add("Menu " + i);
  85.  
  86.         buttonHeight = buttonStyle.CalcSize(new GUIContent(menuItems[0])).y;
  87.        
  88.         // Check the flag we set above to see if we can display the menu
  89.         if (showContextMenu)
  90.         {
  91.             // Specify where we want to draw the controls contained inside
  92.             // this area
  93.             GUILayout.BeginArea(clickedArea);
  94.             {
  95.                 // Use a Scroll View in case we have a lot of menu items.  We don't to
  96.                 // display a huge menu
  97.                 scrollPosition = GUILayout.BeginScrollView(scrollPosition, contextMenuSkin.scrollView);
  98.                 {
  99.                     // Use a text field but style it as a search field.  Due to a type in Unity,
  100.                     // make sure you take note of the misspelling of the GUI style
  101.                     searchString = GUILayout.TextField(searchString, GUI.skin.FindStyle("ToolbarSeachTextField")) ?? string.Empty;
  102.  
  103.                     List<string> filteredMenuItems = menuItems.Where(menuItem => menuItem.Contains(searchString)).ToList();
  104.                    
  105.                     // Loop through all the menu items
  106.                     for (int i = 0; i < filteredMenuItems.Count; i++)
  107.                     {
  108.                         // Draw a button for each menu item
  109.                         if (GUI.Button(new Rect(0, i * buttonHeight + topOffset, 105, buttonHeight), filteredMenuItems[i], buttonStyle))
  110.                         {
  111.                             showContextMenu = false;
  112.                            
  113.                             // Button code goes here
  114.                         }
  115.                     }
  116.                 }
  117.                 GUILayout.EndScrollView();
  118.             }
  119.             GUILayout.EndArea();
  120.         }
  121.     }
  122. }
Add Comment
Please, Sign In to add comment