- Home /
Display a Text/GUI on screen when triggerd with Fadein/Fadeout
Hey im new to unity scripting and i need help please.
i got this script
#pragma strict
var message : String = "Change Me";
var displayMessage : boolean = false;
var displayTime : float = 3.0;
function OnTriggerEnter ()
{
displayMessage = true;
yield WaitForSeconds (displayTime );
displayMessage = false;
}
function OnGUI ( )
{
if ( displayMessage )
{
GUI.Label(new Rect(Screen.width * 0.5f - 50f, Screen.height * 0.5f - 10f, 100f, 20f), message);
}
}
How can i add a Size for text in that? and fade in and fadeout feature. Thanks!
Two things, one, don't post comments as answers. Secondly, the community will respond if they know how to help you. $$anonymous$$aybe edit your question, and explain what documentation you have read, and what you need help with. It's unlikely that anyone is going to write your code for you. What have you managed to do for yourself in the last 4 hours?
Answer by TonyLi · May 29, 2013 at 03:10 PM
Unity GUI is a little complicated if you're new to scripting, so take your time learning it. It will eventually all make sense. :-)
For size, you'll probably want to use a GUIStyle, and set the fontSize.
See: http://docs.unity3d.com/Documentation/ScriptReference/GUIStyle.html
For fading, you'll want to set the text color's alpha value. For example, with a regular label, this is GUIStyle.normal.textColor.
The example below draws the text at font size 20, with 50% transparency.
var alpha : float = 0.5;
var style : GUIStyle;
style = GUI.skin.label;
style.fontSize = 20; // Set size of text to 20.
style.normal.textColor = new (1, 1, 1, alpha); // Set color to white w/alpha.
GUI.Label(new Rect(Screen.width * 0.5f - 50f, Screen.height * 0.5f - 10f,
100f, 20f), message, style);
(Warning: untested code above, plus my apologies in case I accidentally snuck some C# code in there out of habit.)
I recommend using a coroutine to handle fading in and out. A coroutine is a function that runs semi-independently from the main loop.
See: http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
When fading in, the coroutine can change the value of alpha from 0 (completely transparent) to 1 (completely solid). Then OnGUI() can draw the label using the current alpha value.
function FadeIn(duration : float) {
if (duration > 0) {
var startTime = Time.time;
var endTime = start + duration;
while (Time.time < endTime) {
var elapsed = Time.time - startTime;
alpha = (elapsed / duration);
yield;
}
}
alpha = 1;
}
If this answer helped, please accept it. It might help others, too.
Your answer
Follow this Question
Related Questions
Help me with Gui Text 2 Answers
[4.6 JS] How to show GUI text on a trigger enter and then take it away on exit? 1 Answer
Need Text to Display After 5 Seconds 1 Answer
Using a trigger to make a scrollview appear and then disappear? 1 Answer
Displaying a Text when near a item, then display a hud (Press 'E' to investiage) + Halo appear 0 Answers