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 CSmithh · Mar 01, 2019 at 02:39 AM · uievents

Better Code/Event System for Menu

I am working on a menu that is meant to have buttons that show quotation marks when you select them (like Silent Hill).

alt text Scene View and Heirarchy

alt text Event Trigger (used for both buttons)

I created event triggers inside the "BTN_" objects that change the alpha of the "Quote1" child object from 0 to 1 when the mouse is hovered over and back to 0 when the mouse isn't over the button. Is there a way to make a better event system that also reacts to "wasd" and arrow keys? I'm preferably aiming to organize the menu and triggers in C#, but using components in the inspector is fine.

Packages Used - TextMesh Pro

EDIT: The parent system of my button is:
BTN_ (the button) NewGameBtn (the text) Quote1 (the quotation marks)

u2.png (14.6 kB)
u4.png (52.9 kB)
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
1
Best Answer

Answer by WarmedxMints · Mar 01, 2019 at 02:58 AM

You could make a class that inherits from EventTrigger to deal with it. Like so..

 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class ButtonController : EventTrigger
 {
     private string _buttonText;
 
     private Text _buttonTextBox;
 
     private void Start()
     {
         _buttonTextBox = GetComponentInChildren<Text>();
         _buttonText = _buttonTextBox.text;
     }
 
     public override void OnPointerEnter(PointerEventData eventData)
     {
         UpdateButtonText(true);
     }
 
     public override void OnPointerExit(PointerEventData eventData)
     {
         UpdateButtonText(false);
     }
 
     public override void OnSelect(BaseEventData eventData)
     {
         UpdateButtonText(true);
     }
 
     public override void OnDeselect(BaseEventData eventData)
     {
         UpdateButtonText(false);
     }
 
     private void UpdateButtonText(bool selected)
     {
         _buttonTextBox.text = selected ? "\"" + _buttonText + "\"" : _buttonText;
     }
 }

If you remove the event trigger component and your quote child object, you can just put the above script on the button.

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 CSmithh · Mar 02, 2019 at 08:41 PM 0
Share

I removed the quotes and the trigger component. I put the code on the button and if I move my mouse over the button or click it, nothing happens. I forgot to mention that the "NewGameBtn" object that's inside the main "BTN_" object is the text itself.
Is it better to use this code for the normal Unity UI Text?
I am also getting two NullReferenceException errors that point to lines 14 and 42
14:"_buttonText = _buttonTextBox.text"
42: this function - "_buttonTextBox.text = selected ..." inside the UpdateButtonText bool.

avatar image WarmedxMints CSmithh · Mar 02, 2019 at 08:51 PM 0
Share

That code is for the Unity UI text. Create a new standard button and try it. All you need to do with that code is change the text component of a normal button to your font and shade of red. If you don't have a text component on the button, the code will not work as it is looking for one.

avatar image CSmithh WarmedxMints · Mar 05, 2019 at 02:21 AM 0
Share

It works for the mouse, but what about using the arrow keys and wasd?

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

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IDropHandler OnDrop() only being called on parent of dragged item. 0 Answers

How to seperate/decouple UI and player code 0 Answers

Adding a function to a UI 4.6 Button called on another game object in c# 0 Answers

Can't change UI Button Listeners 1 Answer

How to vertical drag button inside horizontal scrollrect 2 Answers


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