Difference between revisions of "Context Menu"
Bablakeluke (talk | contribs) (→3D) |
Bablakeluke (talk | contribs) (→3D) |
||
Line 8: | Line 8: | ||
== 3D == | == 3D == | ||
− | + | Tick '''Window > PowerUI > Handle 3D Input''' and attach something like this to your GameObjects: | |
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
Line 27: | Line 27: | ||
context.add("&Open;",delegate(Option o){ | context.add("&Open;",delegate(Option o){ | ||
− | // 'open' | + | // 'open' the door! |
}); | }); | ||
Line 38: | Line 38: | ||
If you use this approach, note that '''the event will bubble up the scene hierarchy''', in a similar way to it bubbling through the DOM. This way, if PowerUI's input ray hits a collider which is a child of an NPC or a door etc, then it will reach the root NPC object and you'll get a successful handle. | If you use this approach, note that '''the event will bubble up the scene hierarchy''', in a similar way to it bubbling through the DOM. This way, if PowerUI's input ray hits a collider which is a child of an NPC or a door etc, then it will reach the root NPC object and you'll get a successful handle. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
== 2D == | == 2D == |
Revision as of 05:10, 19 April 2017
Context menus pop up to display one or more options to the user, depending on what it was that they interacted with. They come in a huge variety of shapes and sizes - from radial "Sims style" to ordinary lists that show up when you right click. PowerUI has a built in context menu system which sits on top of the widget system enabling templates of context menu's to be easily created and shared.
Contents
Context menu's can be created for either 2D or 3D objects. The flow of both has been made as consistent as possible - essentially, you need to respond to the contextmenu event.
3D
Tick Window > PowerUI > Handle 3D Input and attach something like this to your GameObjects:
using UnityEngine;
using System.Collections;
using Dom;
using ContextMenus;
public class My3DContextMenu : MonoBehaviour {
public void OnContextMenu(ContextEvent e){
// *this* GameObject has been asked for a context menu.
// E.g. if this gameObject is some kind of door, then:
context.add("&Open;",delegate(Option o){
// 'open' the door!
});
}
}
If you use this approach, note that the event will bubble up the scene hierarchy, in a similar way to it bubbling through the DOM. This way, if PowerUI's input ray hits a collider which is a child of an NPC or a door etc, then it will reach the root NPC object and you'll get a successful handle.
2D
HTML elements receive a contextmenu event, which you can connect to however you like (usually with addEventListener("contextmenu",..)). Use ContextEvent.add in there too in order to build up the options. For example, you might have an inventory. Each item, when clicked on, generates a context menu with options like "use" or "drop". You'd add each of those via ContextEvent.add.
ContextEvent has a field called template - it's the name of one of your widget templates:
// Get it as a context event:
ContextEvent context=e as ContextEvent;
// Set the template to use:
context.template="widgetTemplateName";
By default, it's "menulist" - the name of a built in context menu. Conveniently, this approach means different objects can respond with completely different context menu styles too; for example, a 3D NPC might display a radial context menu (like in the Sims) and a 2D item in an inventory displays the default menulist.
Menulist - the built in default
You can find the source for menulist here:
PowerUI\Source\Extras\Context Menus\Built in\Menulist