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.

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;
    }
}

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.
        }
    }
}

  • 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

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.

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 :stuck_out_tongue: espérons que ca sera utile ^^