I want a GUI.Button to vibrate when the mouse is over it. I have the animation part down. I just need the code for detecting when the mouse is over a given control.
my mythod is using GUISkin~while which seem to be not very good~!
You can test if the mouse is in the control’s rectangle using Rect.Contains. I don’t think there is any direct way to do this via the GUI.Button call.
Andeeee, I considered that approach. Might still go with it. Another alternative I’m having success with is creating a game object with GUI scripting attached to it and using OnMouseOver. If I have something like 50 buttons that need to be testing for mouse over, which method is going to be more efficient in terms of resources used?
I found this somewhere in here… I don’t remember where so I put the code…
I’m using it and it’s pretty easy like this…
it looks like it updates every frame so it doesn’t work very well for sounds but it should work with an animation, I think.
I hope it helps…
var hover : String;
var sound : AudioClip;
function OnGUI(){
GUI.Button (Rect (10,10,100,20), GUIContent ("Button 1", "Button 1"));
GUI.Button (Rect (110,10,100,20), GUIContent ("Button 2", "Button 2"));
hover = GUI.tooltip;
if(hover=="Button 1")
audio.PlayOneShot(sound);
}
function Update () {
if(hover=="Button 2")
Debug.Log("Hovering Button 2");
}
The tooltip idea is excellent - never thought of that before.
Actually, it is working fairly well using Rect.Contains method discussed above. I thought about the tooltip functionality, but doesn’t hover have a second or so delay before it registers as a hover?
well… I don’t really know about the delay…
I’m using it and it starts right after you put your mouse over it and finishes when you take it out…
It doesn’t work for sounds as I said so Imma try the Rect.contains for that…
I love coding and I love arrays… Sue me, why don’t ya…?
How about:
class shivverButton extends object {
var area : Rect;
var directionV : int = 1;
var directionH : int = 1;
var maxOffsetV : float = 5;
var maxOffsteH : float = 5;
var offsetV : float = 0.0;
var offsetH: float = 0.0;
var maxOffset : float = 5.0;
var text : String;
function draw(pos : Vector2) : boolean
{
if (pos.x > area.x pos.x < area.x + area.width
pos.y > area.y pos.y < area.y + area.height)
{
offsetV = Math.Slerp(offsetV, MaxOffsetV * directionV, Random.Range(0,0.3));
offsetH = Math.Slerp(offsetH, MaxOffsetH * directionH, Random.Range(0,0.3));
} else
{
offsetV = Math.Slerp(MaxOffsetV * directionV, 0, Random.Range(0,0.5));
offsetH = Math.Slerp(MaxOffset H* directionH, 0, Random.Range(0,0.3));
}
if (offsetV >= MaxOffset || offsetV =< 0) directionV *= -1;
if (offsetH >= MaxOffset || offsetH =< 0) directionH *= -1;
return GUI.Button(Rect(area.x + offsetH, area.y.offsetV, area.width, area.height), text);
}
}
var MyGUIButtons : shivverButton[];
function OnGUI()
{
var e : Input.MousePosition;
e.y = Screen.height - e.y;
for (x = 0; x < MyGUIButtons.length; x++)
{
if ( MyGUIButtons[x].Draw(e) )
switch(x)
{
case 0: CAllOpenFunc(); break;
case 1: CAllCloseFunc(); break;
case 2: CAllStartGameFunc(); break;
case 3: CAllEndGameFunc(); break;
case 4: CAllMeFunc(); break;
}
}
}
Okay so this is obviously untested and will undoubtedly need some cleanup but you get the basic idea… Also, this has the addd advantage of you being able to define your button size, position and text in the inspector. Graphically. Add in a ExicuteInEditor command in there and you have yourself a mini GuiX interface right there
I just made two conditional sentences and made it work for sounds… and other things…
The variables…
public AudioClip soundButton;
public bool overButton = false;
public bool playedSound = false;
public string hoverButton = "";
On the Update function… (OnUpdate)
if(overButton)
{
if(!playedSound)
{
audio.PlayOneShot(soundButton);
playedSound = true;
}
}
else
{
playedSound = false;
}
Example button… (For each button)
if(GUI.Button(enterButtonRect, new GUIContent ("", "enterButton")))
//No text or "" because is a texture button
On the GUI update function… (OnGUI)
if(hoverButton != "")
overButton = true;
else
overButton = false;
You can put “” on the second parameter of the GUIContent, for buttons that you don’t want to play the audiclip and that’s it…
Thanks for the help!
OMG GUIContent has just saved me sooooo much hassle! I’m a total noob to this scripting malarkey and thought I was going to have to something like get different tooltips displaying based on the mouse pointer’s screen position.
I think this will solve a few other problems I’ve been looking into aswell so thanks very much for this sn4k3!
Here ya go…
who can help me or say to do:
I spend my pointer for button1, which has a text, then the text button1 button2 can instruct someone to appear?
is that I work with over event (onmouseover), but not how to create the code