- Home /
Situational GUI / GUI on mouse over?
I am trying to create a game and I want it so that whenever the cursor mouses over certain objects, it brings up a small GUI with information on the object. I have seen others use it. I want to know how to do this, and a basic script if you can please. Please help this is crucial to my game development.
EDIT: I am just trying to get something simple to copy/past and then modify it to my liking once it works.
EDIT: Also any script you provide, do I place it on my targeted object?
Just a heads up, Unity Answers is not meant to be treated as a forum. Asking specific questions is great, but requests for scripts are generally frowned upon.
Answer by karl_ · Oct 01, 2012 at 02:22 AM
Here's a quick example of one way to do this. Create a C# script and name it HoveringText. Then paste this code. Attach script to gameObject.
public class HoveringText : MonoBehaviour
{
void OnGUI()
{
Vector2 screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
Rect pos = new Rect(screenPoint.x, Screen.height - screenPoint.y, 256, 512);
GUI.Label(pos, "Hovering text here.");
}
}
Answer by BawsAnimations · Oct 01, 2012 at 03:22 AM
so i replace the "worldObject" with the gameobject that i want to target?
Answer by BawsAnimations · Oct 01, 2012 at 11:31 PM
This is not what i need, I need a pop up GUI, not a text appearing on the screen. A pop up which can be closed with a button or key press, and the GUI interface should only open up on clicking certain objects.
Answer by CgShady · Nov 12, 2012 at 08:39 PM
I would suggest using OnMouseDown () to turn a boolean value on, which when on would display a GUI, containing a button to turn the boolean value off again. You need a collider on the object for OnMouseDown to work, and it won't work on mobile (touchscreen) platforms. But that's a start. Also, if you want not be able to open more than one at a time, you'll need a singleton (not the drink :) or a manager of some sort to manage all instances of that script.
Hope this helps.
public class Popup : MonoBehaviour
{
private bool _isOn;
OnMouseDown ()
{
_isOn = true;
}
OnGUI ()
{
if GUI.Button ( new Rect ( 10, 10, 50, 20), "close" )
{
// your popup content here
_isOn = false;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
question with gui 2 Answers
Drawing GUI Text using Javascript 2 Answers
mouse displays GUI text when on objects 3 Answers
Does Unity Work With DAZ? 2 Answers