Advertisemen
Hello guys, Today we will learn to make a text fade away.
Open up Unity and create plane for background, if you want you can change the colour but I am going to leave it as it is and also add directional light so you can see what is happening.
Now, create guiText, GameObject > Create Other > GUI Text and then name it whatever you want and positioned it wherever you want.
After that create C# script, name it whatever you want then attached to GUI Text.
Now, type following in you script editor and save it.
This code is based on Unity 5x so if you are using Unity 4x please comment out the line saying For Unity 5x and uncomment the line saying For Unity 4x.
For C# :
using UnityEngine;
using System.Collections;
public class fadingText : MonoBehaviour {
const float period = 4.0f;
// Update is called once per frame
void Update () {
if(Time.time > period){
Destroy(gameObject);
}
//Color colorOfObject = guiText.material.color; // for Unity 4x
Color colorOfObject =
GetComponent<GUIText>().material.color; // for Unity 5x
float prop = (Time.time / period);
colorOfObject.a = Mathf.Lerp(1, 0, prop);
//guiText.material.color = colorOfObject; // for Unity 4x
using System.Collections;
public class fadingText : MonoBehaviour {
const float period = 4.0f;
// Update is called once per frame
void Update () {
if(Time.time > period){
Destroy(gameObject);
}
//Color colorOfObject = guiText.material.color; // for Unity 4x
Color colorOfObject =
GetComponent<GUIText>().material.color; // for Unity 5x
float prop = (Time.time / period);
colorOfObject.a = Mathf.Lerp(1, 0, prop);
//guiText.material.color = colorOfObject; // for Unity 4x
"color: #f7f7f1;"> GetComponent<GUIText>().material.color
= colorOfObject; // for Unity 5x
}
}
For Javascript :
Thats all, now you have fading away text.
= colorOfObject; // for Unity 5x
}
}
For Javascript :
#pragma strict
var period : float = 4.0;
// Update is called once per frame
function Update () {
if(Time.time > period){
Destroy(gameObject);
}
//var colorOfObject : Color = guiText.material.color; // for Unity 4x
var colorOfObject : Color =
GetComponent.<GUIText>().material.color; // for Unity 5x
var prop : float = (Time.time / period);
colorOfObject.a = Mathf.Lerp(1, 0, prop);
GetComponent.<GUIText>().material.color
= colorOfObject; // for unity 5x
//guiText.material.color = colorOfObject; // for Unity 4x
}
var period : float = 4.0;
// Update is called once per frame
function Update () {
if(Time.time > period){
Destroy(gameObject);
}
//var colorOfObject : Color = guiText.material.color; // for Unity 4x
var colorOfObject : Color =
GetComponent.<GUIText>().material.color; // for Unity 5x
var prop : float = (Time.time / period);
colorOfObject.a = Mathf.Lerp(1, 0, prop);
GetComponent.<GUIText>().material.color
= colorOfObject; // for unity 5x
//guiText.material.color = colorOfObject; // for Unity 4x
}
Thats all, now you have fading away text.
Advertisemen
Tidak ada komentar:
Posting Komentar