Recommended reading:
In the game, players perform a certain operation, often need to give the player a hint to tell the player operating result: success or failure prompts. Embodiment may be used to prompt the elastic block, the text may be fading. Display some information block is elastic, and with the OK / Cancel buttons. Fade text is display text on and gradually drift hidden, until it disappears. This process usually only takes about 1 second.
Today to share with you realize the writing prompt.
Implement (1) First create a new text, the text is used to control the animation:
The main idea:
the definition of a public variable color and text, easy to modify in the editor;
text of the statement component assigned text, color, position initialization;
declare a float variable for controlling character animation time.
Update on time - the judge <0? End text animation: reduce transparency, move up
/**********
*@author:shirln(博客地址:https://blog.csdn.net/shirln)
*@data:2019.7.29
*@fun:文本提示动画
*/
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FlyText : MonoBehaviour
{
public Color color;//文字的颜色
public string content;//文本内容
Text t;//Text组件
void Start()
{
t = transform.GetComponent<Text>();
t.text = content;//设置组件
t.color = color;
t.rectTransform.anchoredPosition = new Vector2(t.rectTransform.anchoredPosition.x, t.rectTransform.anchoredPosition.y + 30);
}
float fadeTimer = 1;
void Update()
{
fadeTimer -= Time.deltaTime;
t.color = new Color(color.r, color.g, color.b, fadeTimer);
t.rectTransform.anchoredPosition = new Vector2(t.rectTransform.anchoredPosition.x, t.rectTransform.anchoredPosition.y + (1 - fadeTimer));
if (fadeTimer < 0)
{
GameObject.Destroy(this.gameObject);
}
}
}
(2) create a game object, to add Text component, set according to demand, the addition of the FlyText script. Prefabs saved in the Resources folder / Text_FlyText.
(3) the use of prefabricated:
adding text prompt needs a script flyText method for instantiating preform, and converted to the GameObject, set the parent object, acquires the FlyText script component, the content of the text and assign colors.
/**********
*@author:shirln(博客地址:https://blog.csdn.net/shirln)
*@data:2019.7.29
*@fun:获取文字动画脚本,为其指定信息
*/
void flyText(Vector3 pos,Color color,string content)
{
GameObject go = Instantiate(Resources.Load<GameObject>("Prefabs/Text_FlyText"), pos, Quaternion.identity) as GameObject;
go.transform.SetParent(GameObject.Find("Canvas").transform);
FlyText ft = go.GetComponent<FlyText>();
ft.color = color;
ft.content = content;
}
(4) call flyText method:
flyText(new Vector3(0, 0, 0), new Color(1, 0, 0, 1), "文字提示内容");
For example: I need to start the game interface, click the Start button when the prompt text "into the game"
/**********
*@author:shirln(博客地址:https://blog.csdn.net/shirln)
*@data:2019.7.29
*@fun:具体调用方法
*/
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class StartScene : MonoBehaviour {
public void StartGame()
{
flyText(new Vector3(0, 0, 0), new Color(1, 0, 0, 1), "文字提示内容");
}
void flyText(Vector3 pos,Color color,string content)
{
GameObject go = Instantiate(Resources.Load<GameObject>("Prefabs/Text_FlyText"), pos, Quaternion.identity) as GameObject;
go.transform.SetParent(GameObject.Find("Canvas").transform);
FlyText ft = go.GetComponent<FlyText>();
ft.color = color;
ft.content = content;
}
}