x


GUI on click

So, I have made a map for my game. There are 36 different territories. Each Territory is a different game object. In this map scene, I want it so that when I click one of the territories, i have it so that information pops up on the side. Then when I click another, information pops up for that. I am almost entirely new to scripting. Is there a way to do this without using tags?

more ▼

asked Sep 09, 2011 at 01:38 AM

sketchers1 gravatar image

sketchers1
229 56 56 62

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can use OnMouseDown to detect when the object is clicked, and show the info in a GUIText or GUI.Label, like this:

var information: String; // define the text here
private var guiOn = false;
private var rect: Rect;

function OnMouseDown(){
    guiOn = true; // enable gui and define position at point clicked
    rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
    yield WaitForSeconds(5);
    guiOn = false;
}

function OnGUI(){
    if (guiOn){
        GUI.Label(rect, information);
    }
}

NOTES:
1- Attach this script to each object.
2- OnMouseDown doesn't work in the iPhone.
3- All objects must have a collider or be a GUI element.

EDITED:
If you already have some common script attached to all blocks, you can just edit this script and add the code above - code changes made to a script affect all instances (except changes in initial values of public variables - these are kept by the Inspector).

Anyway, if you want to have only one script (maybe attached to the camera) you can do a raycast and find the object "under" the mouse pointer:

function Update(){

    if (Input.GetMouseButtonDown(0)){
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var hit: RaycastHit;
        if (Physics.Raycast(ray, hit) && hit.transform.tag=="Block"){
            var clickedObj = hit.transform;
            // do whatever you want with clickedObj
        }
     }
}

The problem here is: how to know which information show for each block? To do that, you should have the information stored in each block, or some kind of id number to use as an index and retrieve the information from an array. Both have the same problem: you must attach a script to each block, and write the information or id number for each one in the Inspector.
Finally: you can use the object name or tag to store information - but I don't know which are the limitations (name size, tag size, how much tags you can register etc.).

EDITED 2: Since you need only one territory name showing at one time, you should use a single separated script to show the name (attach it to the camera or any other object), and set variables in it when some territory is clicked. This also helps to set the message Box characteristics in the Inspector with the variable style. That's the separated script - let's call it ShowName.js:

static var information: String; // the territory defines this string...
static var rect: Rect;          // the message position...
static var endTime: float = 0;  // and the time it will appear
var style: GUIStyle; // you can set color, background etc. at the Inspector

function OnGUI(){
  if (Time.time 

= endTime){ // show message if there's a new end time... GUI.Box(rect, information, style); // Box may be better to show the message } } The territory script reduced a little, since the OnGUI part was moved to the script above. It now only needs to define the message, the rect occupied and the time to turn the message off:

var information: String; // define the text here, as before

function OnMouseDown(){
    ShowName.information = information; // define the name and the position (below)
    ShowName.rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
    ShowName.endTime = Time.time + 5; // name will appear for 5 seconds
}

EDITED 3: Now there's the version with a static right aligned box and with variable height:

static var information: String; // the territory defines this string...
static var endTime: float = 0;  // and the time it will appear
var style: GUIStyle; // you can set color, background etc. at the Inspector
var w: float = 300;  // box width

function OnGUI(){
  if (Time.time 

= endTime){ // show message if there's a new end time... var h = style.CalcHeight(GUIContent(information), w); // find the necessary height GUI.Box(Rect(Screen.width-w,10,w,h), information, style); } } The territory script, now more reduced yet:

var information: String; // define the text here, as before

function OnMouseDown(){
    ShowName.information = information; // define the name
    ShowName.endTime = Time.time + 5; // name will appear for 5 seconds
}
more ▼

answered Sep 09, 2011 at 02:43 AM

aldonaletto gravatar image

aldonaletto
57.2k 31 79 292

aldonaletto , your concept is absolutely correct. but,there is 36 objects in his game if we attach into every object the process will be slow. so, we can tag every objects in a single script. from that we can find object name or object tag in run time. correct?

Sep 09, 2011 at 03:43 AM sriram90

@sriram90: this is possible, but you will have to define the information for each block anyway - I edited my answer to show the approach you've suggested.
On the other hand, if you already have any common script attached to all blocks, you can just edit this script and add the code above - all instances use the same source script, so they will get updated automatically.

Sep 09, 2011 at 11:05 AM aldonaletto

The First one Worked! Kind of.... but my only problem is that, I need a box, because the text kind of fades in into the background... Where and how would I put that? and if I want to switch the territory information I am looking at... What changes would I need. Because, I have territory one selected. When I click it, it says bob. When I click the next one, the bob stays, but fred also pops up for the other one. I want the bob to disappear. Does that make sense??

Sep 10, 2011 at 03:32 AM sketchers1

In this case, maybe the second script could be more indicated. But you can still use the first one, with some changes: you would need a separated script to show the message (it could be attached to the camera or to a empty object) and use some static variables to communicate which is the currently selected territory. I've edited my answer to show this idea.

Sep 10, 2011 at 04:44 PM aldonaletto

This is a great, exhaustive answer. If it helped you, be sure to award @aldonaletto an upvote by hitting the thumbs-up button and to mark the answer as correct. :)

+1 from me.

Sep 12, 2011 at 11:50 AM CHPedersen
(comments are locked)
10|3000 characters needed characters left

Thanks So much! that helped alot. of course, i still have one more problem. The Box and Information shows up below the mouse... I want it to show up in the same place every time. (right corner of the screen) how would I apply this? Gui Box instead of rectangle? or... how would I do that. Thanks. :)

more ▼

answered Sep 11, 2011 at 04:46 AM

sketchers1 gravatar image

sketchers1
229 56 56 62

Just define the rect yourself in GUI.Box (use Screen.width to calculate the left coordinate):

GUI.Box(Rect(Screen.width-300,10,300,100), information, style);

You can remove all the rect stuff from the other lines in both scripts.

Sep 12, 2011 at 11:13 AM aldonaletto

Thanks!!! It Works Now! although i still have one problem... my descriptions go out of the box.... is there a way to set a limit so that the automatically skip to the next line? Once again, Thanks!!!

Sep 12, 2011 at 08:20 PM sketchers1

You must set Word Wrap in the style variable at the Inspector - this will break the lines. Case the text is too big, it will overflow down the box. You can use a GUI function to calculate the height based on a given width and text:

    var h = style.CalcHeight(GUIContent(information), 300);
    GUI.Box(Rect(Screen.width-300,10,300,h), information, style);

I edited the answer to show these last modifications - including a w variable to set the box width.

Sep 14, 2011 at 05:11 PM aldonaletto

oh... i have come upon another problem. I have a lot of information typed in word.. I wanted to copy it and paste it into the Information option but it wont let me... is there a way to fix that?

Oct 07, 2011 at 06:29 PM sketchers1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5736
x3532
x1732
x345

asked: Sep 09, 2011 at 01:38 AM

Seen: 9622 times

Last Updated: Feb 07, 2013 at 01:21 AM