Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 zakkar · Jun 07, 2012 at 11:02 PM · soundonguionmouseoverover

OnMouseOver for OnGUI

Hello

Please i want to know how can i play a sound when my mouse is over a GUI.Button, i have made this code but the sound only play when i leave the GUI.Button, how can i fix it or is there any other issue to make this?

  GUI.Button(new Rect(Screen.width*0.083f,250,200,50),new GUIContent("Play","Over"));    
             if(GUI.tooltip=="Over" )
             {
             audio.clip=over;    
             audio.Play();
             }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hirudora · Jun 07, 2012 at 11:39 PM

Have you tried removing : audio.clip=over; ?

I think it will only play the sound, if is OVER

Thanks, Hirudora!

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 Bunny83 · Jun 07, 2012 at 11:40 PM 0
Share

Uhm that doesn't make mush sense ;)

over is propably a variable that hold the AudioClip he wants to play.

avatar image
0

Answer by Bunny83 · Jun 07, 2012 at 11:49 PM

I guess the problem is that you play the sound each frame as long as the tooltip is "Over". This will effectively retrigger the sound each frame so it get stuck at the start. When you leave the button the last triggered play can finally play ;)

You need to detect changes of the tooltip. You could use an "old"-variable to see when it changes:

 // I guess C# ?
 private string oldToolTip = "";
 
 void OnGUI()
 {
     // [...]
     GUI.Button(new Rect(Screen.width*0.083f,250,200,50),new GUIContent("Play","Over"));
     if (Event.current.type == EventType.Repaint)
     {
         if (GUI.tooltip != oldToolTip) // only once when the tooltip changed
         {
             if(GUI.tooltip == "Over" ) // On mouse over event
             {
                 audio.clip=over;  
                 audio.Play();
             }
             // if you need it:
             if (oldToolTip == "Over") // On mouse out event
             {
                 // ...
             }
             oldToolTip = GUI.tooltip;
         }
     }
 }

This can be used for both: enter and leave. The if is only executed once when the tooltip changes. To detect the leave event, just check the oldToolTip like i showed in the example

edit I changed my example and added the repaint event check.
Btw the tooltip examples show exactly the same thing ;)

Comment
Add comment · Show 2 · 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 zakkar · Jun 08, 2012 at 01:03 AM 0
Share

Thank you, but i have tried with the old variable, it's the same thing, the sound play when i leave the OnGUI.Button :(

avatar image Bunny83 · Jun 15, 2012 at 10:14 AM 0
Share

@zakkar: Sorry, but this question got buried between the 46 other questions in my inbox...

$$anonymous$$y bad... GUI.tooltip is only valid in the repaint event so you have to check for it. $$anonymous$$eep in $$anonymous$$d that OnGUI is an event processing callback. There are many different events.

I will edit my answer ;)

avatar image
0

Answer by kolban · Jun 08, 2012 at 03:11 AM

How about we ignore the "tooltip" hack and instead have code that says:

 if (Event.current.type == EventType.Repaint &&
    Rect(...).Contains(Event.current.mousePosition))
 {
    if (!audio.isPlaying)
    {
       audio.Play()
    }
 }

The above is pseudo code but it seems to me that we don't need to fool around with tooltip handling for a single button.

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 Bunny83 · Jun 15, 2012 at 10:28 AM 0
Share

It's not really a "hack". The tooltip is ment to be used that way ;) It actually does the same thing. GUIStyle.Draw handles the tooltip information. It looks like this:

 // refactored by ILSpy:
 public void Draw(Rect position, GUIContent content, int controlID, bool on)
 {
     if (Event.current.type != EventType.Repaint)
     {
         Debug.LogError("Style.Draw may not be called if it is not a repaint event");
         return;
     }
     Event current = Event.current;
     bool flag = position.Contains(current.mousePosition);
     bool flag2 = flag && GUIClip.enabled;
     bool isHover = flag2 && (GUIUtility.hotControl == controlID || GUIUtility.hotControl == 0);
     if (flag2)
     {
         GUIUtility.mouseUsed = true;
     }
     bool flag3 = controlID == GUIUtility.hotControl && GUI.enabled && flag;
     bool flag4 = GUIUtility.keyboardControl == controlID && GUI.enabled && GUIStyle.show$$anonymous$$eyboardFocus;
     GUIStyle.Internal_Draw(this.m_Ptr, position, content.text, content.image, isHover, flag3, on, flag4);
     if (content.tooltip != null && content.tooltip != string.Empty && !flag3)
     {
         if (flag3 || GUIUtility.hotControl == controlID || (flag2 && GUIClip.visibleRect.Contains(Event.current.mousePosition)))
         {
             GUI.s_EditorTooltip = (GUI.s_$$anonymous$$ouseTooltip = content.tooltip);
             Vector2 vector = GUIUtility.GUIToScreenPoint(new Vector2(position.x, position.y));
             GUI.s_ToolTipRect = new Rect(vector.x, vector.y, position.width, position.height);
         }
         if (flag4)
         {
             GUI.s_EditorTooltip = (GUI.s_$$anonymous$$eyTooltip = content.tooltip);
         }
     }
 }

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

6 People are following this question.

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

Related Questions

Unity still receives OnMouseOver events while in background 1 Answer

MouseOver Clarification Please 2 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

When in game, sound is really strange. Help Please!? 2 Answers

How can i add a time limit to running? 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