- Home /
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?
Answer by aldonaletto · Sep 09, 2011 at 02:43 AM
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
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
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 }
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?
@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.
The First one Worked! $$anonymous$$ind 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??
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.
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. :)
from me.
Answer by sketchers1 · Sep 11, 2011 at 04:46 AM
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. :)
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.
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!!!
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.
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?
Your answer
Follow this Question
Related Questions
Detect a click outside a GUI/object 4 Answers
Gameobject shown as GUI 1 Answer
Many objects - optimization 1 Answer
Get texture from gui to gameobject 1 Answer
how can i detected wheter is clicked a children of gameobject? 0 Answers