Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- public class CustomWindow : EditorWindow
- {
- private bool showContextMenu;
- private Vector2 mousePosition;
- private List<string> menuItems;
- private GUISkin contextMenuSkin;
- private GUIStyle buttonStyle;
- private float buttonHeight;
- private Vector2 scrollPosition;
- private string searchString;
- private float topOffset = 25f;
- // Add a menu to the Unity Editor which when selected
- // will call this static method
- [MenuItem("My Window/Show")]
- public static void ShowWindow()
- {
- // Create a new instance of the window
- CustomWindow window = GetWindow<CustomWindow>();
- // This isn't required it just prevents the window
- // from being resized smaller than 800x600. The same
- // can be done for the maxSize
- window.minSize = new Vector2(800, 600);
- // Show the newly created window
- window.Show();
- }
- private void OnGUI()
- {
- // Check if the skin has been loaded, if not load it
- if (contextMenuSkin == null)
- {
- contextMenuSkin = AssetDatabase.LoadAssetAtPath<GUISkin>("Assets/ContextMenuSkin.guiskin");
- buttonStyle = contextMenuSkin.GetStyle("contextMenuButton");
- }
- // Specifies where and how big to draw the menu
- Rect clickedArea = new Rect(mousePosition.x, mousePosition.y, 120, (6 * buttonHeight) + 5 + topOffset);
- // Get the current event being processed
- Event current = Event.current;
- // Check to see if the the mouse was clicked
- if (current.type == EventType.MouseDown)
- {
- // Check if it was the right mouse button
- if (current.button == 1)
- {
- if (!showContextMenu)
- {
- // If we aren't currently showing the context
- // menu, get the current mouse position and
- // set showContextMenu to true
- mousePosition = current.mousePosition;
- showContextMenu = true;
- }
- else
- {
- // Hide the context menu
- showContextMenu = false;
- }
- // Use the event causing other GUI elements to ignore it
- current.Use();
- }
- else
- {
- // If we click outside the menu we want to hide it
- if (!clickedArea.Contains(current.mousePosition))
- showContextMenu = false;
- }
- }
- // Initialize the list of menu items
- menuItems = new List<string>();
- // Add simple strings to the list
- for(int i = 0; i < 6; i++)
- menuItems.Add("Menu " + i);
- buttonHeight = buttonStyle.CalcSize(new GUIContent(menuItems[0])).y;
- // Check the flag we set above to see if we can display the menu
- if (showContextMenu)
- {
- // Specify where we want to draw the controls contained inside
- // this area
- GUILayout.BeginArea(clickedArea);
- {
- // Use a Scroll View in case we have a lot of menu items. We don't to
- // display a huge menu
- scrollPosition = GUILayout.BeginScrollView(scrollPosition, contextMenuSkin.scrollView);
- {
- // Use a text field but style it as a search field. Due to a type in Unity,
- // make sure you take note of the misspelling of the GUI style
- searchString = GUILayout.TextField(searchString, GUI.skin.FindStyle("ToolbarSeachTextField")) ?? string.Empty;
- List<string> filteredMenuItems = menuItems.Where(menuItem => menuItem.Contains(searchString)).ToList();
- // Loop through all the menu items
- for (int i = 0; i < filteredMenuItems.Count; i++)
- {
- // Draw a button for each menu item
- if (GUI.Button(new Rect(0, i * buttonHeight + topOffset, 105, buttonHeight), filteredMenuItems[i], buttonStyle))
- {
- showContextMenu = false;
- // Button code goes here
- }
- }
- }
- GUILayout.EndScrollView();
- }
- GUILayout.EndArea();
- }
- }
- }
Add Comment
Please, Sign In to add comment