- Home /
How to draw GUI Text from code
Hey guys,
I was wondering, how do you draw some GUI text (or any GUI object for that matter) on the screen from Javascript. I know how to use the Time.time method to display it after a number of seconds but I want to be able to remove it after a number of seconds as well....
And also, it would be good if I was able to choose the font that this GUI text would use!!!!! Thanks
-Grady
Answer by Jesus_Freak · Jun 28, 2011 at 07:35 AM
I recommend using an array of strings for your subtitles.
var waitTime : float;
var seconds : int;
var timer : float;
var f : Font;
var rectplayer1 : Rect[];
var rectplayer2 : Rect[]; //just keep making new arrays for every amount of subtitles you want.
var s1 : String[];
var s2 : String[]; //same here
var cur1 : int;
var cur2 : int; //and here
function Update() {
waitTime = (Time.deltaTime * seconds);
while(timer < waitTime) {
timer += Time.deltaTime;
}
if(timer >= waitTime) {
cur2++;
timer = 0;
}
}
//vars and function for gui code here in the same script:
function OnGUI() {
GUI.skin.label.font = f;
GUI.Label(rectplayer1[cur1],s1[cur2]); //change cur1 between two or three rects and continuously increment cur2 to get an illusion of dialogue.
}
See? make one GUI.Label for each character's subtitle you want, with its own string array, and it's own original cur value and after that character is "done talking," increment cur to go to the next subtitle. And in case you don't know already, to use an array, set the size to however many elements (vars) you want, and then set those values to whatever you want, via the inspector, or code.
If you have any more questions, leave another comment. :) I hope I helped!
O$$anonymous$$, thanks very much for the examples!!!! :D i've managed to get the first one to work, but am having trouble with the second. although, as far as i understand, you would attach the second example to a GUI object. With my game, I am trying to make it have lots of different text come up on the screen at different times, as it will be subtitles for other characters speech in the game. I was thinking, that probably the most efficient way to achieve this would be to use the first method, but to set it so that it stops drawing the GUI.... would this be possible
-Grady
And to answer, yes you'd attach the original posted second script to a GUI object, preferably a GUI Text object. I elaborated on the first because JavaScript in unity can make it's own GUI Elements and edit the from a code, which can do a lot more than a builtin GUI Object.
when i just put in the first script, under rect it has no x or y, it just has "Size 0"???
yeah, the [] after Rect and String makes them an array. so set the size to however many subtitles each character has. like if it has 4, set it to 4. you should then get 4 Elements with a down arrow, to open up as 4 editable Rects. and Strings.
oh, stupid me, you have to put a number in there first!!!!!! :P so in the rect size element, would i put the number of different subtitles that will appear????
i managed to get one string to display, but i can't get it to display two, or have one, and then change to the next????
Your answer
Follow this Question
Related Questions
Scrolling Text 1 Answer
Making a script for all gameobject GUITEXT. 1 Answer
Gui text script - help 1 Answer