- Home /
Tooltip when mousing over a game object?
I need to have like a tooltip that appears over some objects in my scene when they hover the mouse over them. Is this possible? I see the tuts for doing GUI elements, but these are 3D things. Help!
Answer by DaveA · Jan 27, 2011 at 05:34 AM
This is what I use:
var toolTipText = ""; // set this in the Inspector
private var currentToolTipText = ""; private var guiStyleFore : GUIStyle; private var guiStyleBack : GUIStyle;
function Start() { guiStyleFore = new GUIStyle(); guiStyleFore.normal.textColor = Color.white;
guiStyleFore.alignment = TextAnchor.UpperCenter ; guiStyleFore.wordWrap = true; guiStyleBack = new GUIStyle(); guiStyleBack.normal.textColor = Color.black;
guiStyleBack.alignment = TextAnchor.UpperCenter ; guiStyleBack.wordWrap = true; }
function OnMouseEnter () {
currentToolTipText = toolTipText;
}
function OnMouseExit () { currentToolTipText = ""; }
function OnGUI() { if (currentToolTipText != "") { var x = Event.current.mousePosition.x; var y = Event.current.mousePosition.y; GUI.Label (Rect (x-149,y+21,300,60), currentToolTipText, guiStyleBack); GUI.Label (Rect (x-150,y+20,300,60), currentToolTipText, guiStyleFore); } }
Make sure you have a collider on your object and IsTrigger is set. This will make a white-with-black-dropshadow label, no background, that floats around with your mouse pointer.
Cool thanks. Can I have a solid background, maybe semi-transparent?
Thanks, and, to "Highlight" the object on mouse over, use renderer.material.color or something similar, then change it back on mouse exit. Conversely you can "fade" all the ~other~ objects by iterating through them and setting alpha part of Color to less than 1.