- Home /
How to have menu options perform an action using the Canvas UI system?
We have been attempting to use the canvas UI to make a menu for our game. However, we have been struggling to figure out how to actually fill in an action for the menu while using the Canvas UI system. We have it so that we can select different options, and it registers that the buttons are being pressed because we changed the colors to see that. However, we are struggling to figure out how to actually have the menu options do anything. Any information would be helpful, thank you.
Answer by Toon_Werawat · Apr 20, 2016 at 09:26 PM
If you want to add button function while in editor:
Click at button in hierarchy and at inspector Scrolldown to button and it will see On Click() (Red square) Press to add an action (Orange circle) and drag gameobject with script to at None (Object) (Example at green square) After that click at No Function (After gray arrow)
If you want to add button function runtime dynamicly:
public Button startGame;
void Start()
{
//For a better looking script & Multiple function
startGame.onClick.AddListener(delegate
{
Clicked();
LoadFileList();
});
//For a complex or only one function
startGame.onClick.AddListener(delegate { Clicked(); });
}
public void Clicked()
{
Debug.Log("Button has been pressed!!");
}
This has helped a great deal for our understanding; thanks. However, for "clicking" our menu option, we are using a key we defined with the input manager. How could we change it to do our menu option's action when our key is pressed while the button is "highlighted/selected"?
In other words we want the menu option to be pressed when our defined button is.
Would you rather do it as all in unity inspector or all in scripting?
Inspector: Select button you want > Add component > Event Trigger (just serarch it by typing "eve" and you will eventually found it.) > After add the component Press "Add New Event Type"
And you will get event selection. Just like at On Click()
Script: Create new script. Attach it to that button. Or attach it using this script
thatButton.gameObject.AddComponent<*That script*>();
And edit that script. Add this name space
using UnityEngine.EventSystems;
And add interface to the script. (After $$anonymous$$onoBehaviour)
public class CustomButtonEvent : $$anonymous$$onoBehaviour,IPointerEnterHandler,IPointerExitHandler
These lot more interface. You can read those script here (Look at sidebar)
After you add those interface. Just implement it. If you use Visual Studio. They will give you what you have to implement. But $$anonymous$$onoDeveloper? No idea :(
Anymore question? Feel free to ask. If I can. I'll help
PS: I'm try to attach image explain. But it error. So I have to explain by word. Sorry if something not work. I'm not really good at explain word.
Oh! I'm just realize. You want to do keypress at specific button. So. If you want key navigation. Unity already had it. On top of On Click (). It has "Navigation" to make arrow key select button around. You can change it to "Explicit" and drag button you want it to go. $$anonymous$$anually.
But if you mean. After select button. You want to press other key like F8? Or another that not a navigate button?
You have to something like this
using UnityEngine;
using UnityEngine.EventSystems;
public class CustomButtonEvent : $$anonymous$$onoBehaviour, ISelectHandler, IDeselectHandler
{
public bool whenSelectButton = false;
public void OnDeselect(BaseEventData eventData)
{
whenSelectButton = false;
}
public void OnSelect(BaseEventData eventData)
{
whenSelectButton = true;
}
void Update()
{
if (whenSelectButton)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F8))
{
Debug.Log("Custom function on the button!!!!");
}
}
}
}
Thank you so much for your help. I apologize for the gap in my responses but it has been a busy couple of weeks. Thanks to you though we have made tremendous progress. Right now we are attempting to use the menu to allow different in-game units to move. How we want it to work is by having the player select the unit, open the menu, select move, and is given a highlight of areas to move. We want the player to then move the cursor and select one of the highlighted tiles and have the unit go there. Our trouble though is we want the are that is highlighted to be limited by how far any specific unit can move and how to prevent them from moving on to tiles that are incompatible with them. For example, we can't have a tank go through a river or move more than four tiles. Any help on this issue would be greatly appreciated.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to Position the object perfectly with the Screen Bounds 0 Answers
How can I change jump direction? 1 Answer
2d spawn across screen 1 Answer