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
2
Question by Tohaveaname · Apr 06, 2016 at 02:05 PM · c#buttonaudioscripting beginnerhighlight

How do I run a script when a button is highlighted

I just need to run a sound file when a button has been highlighted but not selected. How would I go about this?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Toon_Werawat · Apr 06, 2016 at 02:34 PM

If you use Sprite. Do what @KdRWaylander said. But if you use new unity ui.

Use Unity event system

By click at that button. And add component Event Trigger. Then add OnPointerEnter (When mouse highlight it)

But if you want as a script. Just add script to that button and set that button interacable to false (in case if script not work) Then just add interface to that class like this.

 public class ButtonHighlight : Monobehaviour , IPointerEnterHandler
 .....
 public void OnPointer()
 {
     Debug.Log("Pointer over!);
 }

IPointerHandler is need this namespace

 using UnityEngine.EventSystem;

Hope that what you looking for... :)

Comment
Add comment · Show 1 · 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 Tohaveaname · Apr 11, 2016 at 02:22 PM 0
Share

Thanks this works great for the mouse but how would I use the event system to allow for using the keyboard?

avatar image
6

Answer by Tohaveaname · Apr 12, 2016 at 04:37 PM

Turns out this was what I was looking for:

 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.EventSystems;

 public class ReadMeButton : MonoBehaviour, ISelectHandler , IPointerEnterHandler
 {
     // When highlighted with mouse.
     public void OnPointerEnter(PointerEventData eventData)
     {
         // Do something.
         Debug.Log("<color=red>Event:</color> Completed mouse highlight.");
     }

     // When selected.
     public void OnSelect(BaseEventData eventData)
     {
         // Do something.
         Debug.Log("<color=red>Event:</color> Completed selection.");
     }
 }
Comment
Add comment · 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
2

Answer by KdRWaylander · Apr 06, 2016 at 02:10 PM

Hi,

Try this kind of message:

  • OnMouseEnter

  • OnMouseOver

  • OnMouseExit

http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseEnter.html http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseExit.html

They're called in any script on the the gameobject (tho it must have a collider).

 void OnMouseEnter () {
    // [...]
 }
Comment
Add comment · Show 3 · 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 Tohaveaname · Apr 11, 2016 at 01:36 PM 0
Share

Thanks guys this works great for the mouse but how would I use the event system to allow for using the keyboard?

avatar image KdRWaylander Tohaveaname · Apr 11, 2016 at 01:39 PM 0
Share

How would you highlight a button with keyboard ? :o

avatar image Tohaveaname KdRWaylander · Apr 11, 2016 at 02:20 PM 0
Share

You set the buttons in the Unity editor to have a explicit menu navigation then dictate where in the UI up, down, left, right on the keyboard will take you. You need to add .Select() function to highlight the first button you want in the menu.

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

Adding audio to gun shot 2 Answers

UnityWebRequest hangs on isDone but not always 1 Answer

Please help with playing music for button click 2 Answers

Multiple Cars not working 1 Answer

Delayed Button Sounds 0 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