- Home /
Question by
Chudroy · May 09 at 10:49 AM ·
user interfacecontextmenu
Procedural Context Menu
Hi!
I’m making a point and click game, and I want to design a context menu that opens when clicking on an item in the inventory bar.
So far, i have managed to instantiate a prefab UI image with a script “ContextMenu”.
I want the context menu to instantiate specific buttons based on available interactions with the right clicked inventory item.
For example, with some items in inventory bar, you can “Use” them, and “Examine” them. They have a script on them that implements the interfaces IUsable and IExaminable, each with a method I want to hook up to a button via scripting.
I would like to avoid writing code such as:
public void Setup(InventoryItem item)
{
var examinableItem = item as IExaminable;
//create a button
button1.GetComponent<Button>().onClick.AddListener(() =>
examinableItem.Examine());
var useableItem = item as IUsable;
//create a button
button2.GetComponent<Button>().onClick.AddListener(() =>
examinableItem.Use());
}
Do you have any suggestions?
Thanks for your time!
Comment