- Home /
How do I handle object selection and GUI response ingame?
How can I call up a GUI interface when i click an object?
I have some clues: Use on mouseover or onmousedown, or use collision triggers that make the GUI appear, I just cant make it to work.. Please help would be apreciated. Im new to this.
I really want to be able to controll when a GUI appears on my screen.
Answer by AngryAnt · Dec 03, 2009 at 10:14 AM
One way of doing it would be to have a selection manager class which keeps track of the currently selected object.
Each selectable object would then have to have a collider and a monobehaviour attached which on mouse down changes selection. By using MonoBehaviour.OnMouseDown, you don't need to worry about raycasting or GUI input clickthrough - that is taken care of for you out of the box.
To handle objects of a dynamic nature, we also need to make sure that if the currently selected object leaves the active scene by being disabled or destroyed, we tell the manager to deselect it.
Heres one example of a simple implementation:
using UnityEngine; using System.Collections;
public class SelectableObject : MonoBehaviour { private Rect m_SelectionWindowRect = new Rect (10.0f, 10.0f, 300.0f, 100.0f);
public void OnMouseDown ()
{
SelectionManager.Select (gameObject);
}
public void OnDisable ()
{
SelectionManager.Deselect (gameObject);
}
public void OnGUI ()
{
if (SelectionManager.IsSelected (gameObject))
{
m_SelectionWindowRect = GUI.Window (GetInstanceID (), m_SelectionWindowRect, SelectionWindow, gameObject.name);
}
}
void SelectionWindow (int id)
{
GUILayout.Box ("I am the selection and my name is " + gameObject.name);
GUI.DragWindow ();
}
}
public class SelectionManager { private static GameObject s_ActiveSelection;
public static GameObject ActiveSelection
{
get
{
return s_ActiveSelection;
}
set
{
s_ActiveSelection = value;
}
}
public static void Select (GameObject gameObject)
{
ActiveSelection = gameObject;
}
public static void Deselect (GameObject gameObject)
{
if (ActiveSelection == gameObject)
{
ActiveSelection = null;
}
}
public static bool IsSelected (GameObject gameObject)
{
return ActiveSelection == gameObject;
}
}
Answer by Cyclops · Mar 18, 2010 at 06:38 PM
This may be what you were looking for, if you're still here looking. :) Take this script file, drag/drop it onto an Object (say, a Cube). When you click the Cube in-game, a Button will appear with whatever text you assign it.
Note that you would have to attach this script to every Object you wanted to be clickable. This is a single-purpose solution, where some of the other Answers were showing you how to do a more general-purpose utility GUI function.
var wasClicked: boolean = false;
function OnMouseUp() { wasClicked = true; // turns on button. } function OnGUI() { if (wasClicked) { var someText = "Box Clicked"; if (GUI.Button(new Rect(20, 50, 100, 20), someText)) { // do other stuff when button clicked. wasClicked = false; // turns off button. } } }
Answer by Jaap Kreijkamp · Nov 29, 2009 at 10:34 PM
- give the clickable game object a trigger collider in special layer (clickable or whatever name you want to give it). As the object probably already uses layers for other purposes a way would be to add a new empty game object to the object, add the collider to this, and set this gameobject to the clickable layer
- make a script in camera that in update checks for mouseclicks, if clicked use mouse coordinates with Camera.main.ScreenPointToRay(Vector3(mousex, mousey, 0)) to get a ray from screen into the level.
- use this ray with Physics.Raycast limited to only the clickable layer to determine which object is clicked. When found, send message to parent (as we defined the collider in the childnode) that it's clicked.
- add a script to the objects that show the dialog or whatever on this message
Answer by Will · Nov 29, 2009 at 08:40 PM
Well do you want it to happen when you press a key or click a button
for a button the basic script would be
var window1 = false;
function OnGUI () {
if (GUI.Button (Rect(//leftRight position, //UpDown Position, //width, //length), "Your Text")){
window1 = true;
if (window1) {
//your GUI data
}
For a key press it would be like this
var window1 = false;
function OnGUI () { if (Input.GetButtonDown ("Button you want")) window1 = true;
if (window1) { //your GUI data }
That would be the basic script, you just have to modify it to your needs, hope this helps.
Thanks for the answer. Very kind of you. I love those people who care about responding. But these functions wont really satisfy my need though. What i really want is: a window to appear when I click on a game object with my left mouse button.
Just One click on a creature and then a window comes up with text.
I do not want to click on a GUI button on screen or a key to make this happen. I would really apreciate further help here. Thanks :}
Only one click on the game object.
Answer by Deadcrow · Jan 05, 2012 at 05:18 AM
Pour ma part jai tester ce donc vous parlez...
Jai mis un cube transparent avec collission Ontrigger sur le cube, et je lai mis sur le personnage ( moob)
Sur le cube transparent jai mic ce code JS
var Moob1 : GameObject; var Fenetre1 : GUITexture:
Function Start(){
Fenetre.enabled = false;
}
Function OnMouseUp(){
Fenetre.enabled = true;
}
Voila :P espérons que ca sera utile ^^
Your answer
Follow this Question
Related Questions
GUI Appear Help 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer