Author Topic: Creation of context menus  (Read 1578 times)

LordVe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Creation of context menus
« on: July 21, 2014, 11:59:41 AM »
What is the Best method for creating right-click context menus when certain colliders are hit.  I am attempting to create a Map editor and would like to be able to Right-Click my map to select the tool to be used.  So far, I can not seem to get it to work.  This context menu would have at most 3 levels.  I would like the context menu to appear where the mouse is clicked, rather than make it a static location.

Plutoman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 1
    • View Profile
Re: Creation of context menus
« Reply #1 on: July 21, 2014, 01:09:47 PM »
Recently did something similar for this;

Create a parent gameobject - this one will be moved to the position you click. Under it, use a UIGrid (or something similar) with organized buttons. That, or you can create all the buttons and position them appropriately under the objects.

Have your own script on the parent that knows about the button objects. These button objects will be whatever you'd like, combinations of sprites, labels, etc, backgrounds, whatever it needs to look fancy.

You either need to use a UICamera fall through, or you can query Input for mouse down functionality and check if the UICamera.hoveredObject == null to prevent it from coming up under an NGUI button. Either way you go, you will need an Open method on the parent script that takes in the parameters of what buttons you need to display and what functionality they have. Iterate through the list with two for loops; one going from 0 to the button count you need, and then from that count to the end of the total button list. The first will be shown and given the proper functionality, the latter will be hidden so the list is only as long as needed.

I have these in quite a few locations at this point, so I dynamically create buttons as I go and reposition as appropriate on the grid. As I add buttons to the grid, I'll add them to the list of elements. Or if I preset some of them (I also have a circular context menu), I fill them in as needed.

To access different levels, have the buttons themselves re-open the ParentScript at the same location with a different set of parameters. Have the menu close if anything else is clicked; so in the same code to open this, you'd also close it if nothing else was clicked (or something similar; maybe you could be creative with OnHover to only have it open while the mouse is over it). Closing it is as simple as disabling the top gameobject. Just keep a reference to it from wherever you open the script from - if opening it on specific colliders, just have those objects OnClick events from NGUI call the open for this.

Example;
  1. public class ParentScript : Monobehaviour
  2. }
  3.  
  4.         public List<GUIElement> Elements; //fill this in in Unity - I rebased all of the NGUI elements to GUIElements, so as to standardize it with my own custom elements that are with the GUI but not based upon anything NGUI-related, but you could just as easily use UIWidget, which would cover most of it.
  5.  
  6.         public void Open(Buttons[] parameters)
  7.         {
  8.                 //move the transform
  9.                 MoveAndShowThisObject(UICamera.lastHit.point); //The UICamera will store the RaycastHit info
  10.                 //Alternatively, use your own Ray from Camera.ScreenToWorld (sp?) and get the hit info from that.
  11.  
  12.                 for (int i = 0; i < parameters.Count; i++)
  13.                 {
  14.                         if (Elements.Count > i) ShowButton(Elements[i]); //configure the button here, I use delegates and change the sprites and other things like that, since I have a 50-60 different contextual options for a spell menu in my RPG.
  15.                 }
  16.                 for (int i = parameters.Count; i < Elements.Count; i++)
  17.                 {
  18.                         if (Elements.Count > i) HideButton(Elements[i]);
  19.                 }
  20.         }
  21. }
  22.