Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FarmerJoe · Apr 20, 2016 at 08:28 PM · c#2d game

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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!!");
 }



how.png (59.0 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FarmerJoe · Apr 22, 2016 at 07:57 PM 0
Share

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.

avatar image Toon_Werawat FarmerJoe · Apr 22, 2016 at 11:15 PM 0
Share

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!!!!");
             }
         }
     }
 }

avatar image FarmerJoe Toon_Werawat · May 03, 2016 at 07:10 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges