游戏中超级实用的加血值、伤害值等提示显示的制作实战

游戏中必不可缺的信息提示类,比如攻击伤害值、药瓶加血值等显示,给大家分享一下制作过程。
效果如图:
伤害值显示
会显示一段时间,向上渐隐掉。
1.创作显示预制体prefab.
预制体结构
阴影背景
前端信息显示
建立空物体作为父物体HitReport;建立两个空物体作为子物体,每个子物体添加Text Mesh组件,作为显示信息。
2.为父物体创作脚本,如下:

using UnityEngine;
using System.Collections;

public class Hit : MonoBehaviour 
{
	public TextMesh yellow;
	public TextMesh black;
	float 			startTime;
	float 			currentTime;
	public float 	lifespan; //文字显示时间
	
	public float 	fadeTime;	//退隐时间
	public float 	fadeSpeed;	//退隐速度

	public float 	maxSize; //最大尺寸
	public float 	speed; // 自定义速度
	
	public string	text;
	// Use this for initialization
	void Start () 
	{
		startTime = Time.fixedTime;//获取记录文字显示的开始时间
	}
	
	// Update is called once per frame
	void Update () 
	{
		//destroy this hit report if we are past it's lifespan
		currentTime = Time.fixedTime - startTime;//当前游戏运行的时间
		if(currentTime > lifespan)//如果运行时间超过文字生命周期则销毁文字
		{
			Destroy(gameObject);
		}
		
		//fade out
		if(currentTime > fadeTime)//当时间超过渐隐设置的时间,开始渐隐
		{
			float fade = (fadeTime -(currentTime-fadeTime)) * (1/fadeSpeed);
			Color tempColor = yellow.color;
			tempColor.a = fade;//渐隐透明度
			yellow.color = tempColor;//设计文字显示黄色,当然你可以根据需要设置颜色,new color也可以
			
			tempColor = black.color;
			tempColor.a = fade;
			black.color = tempColor;//背景阴影的显示
		}
		
		//scale and lift accordingly
		if(transform.localScale.x < maxSize)
		{
			transform.localScale = new Vector3 (1.0f,1.0f,1.0f) * (0.01f + currentTime) * 3.0f;
		}//随着时间文字大小的变化实现
		transform.position += (Vector3.up * Time.deltaTime * speed);//沿着y轴垂直上升
		transform.LookAt(Camera.main.transform.position);
		//保持文字始终以最初的rect显示在摄像机视野
		yellow.text = text;
		black.text =text;
	}
}

随着时间流逝变化的物体都可以参考上面脚本,非常实用。

  1. 在需要用到效果的实例上使用此预制体,英雄、AI都可以使用,在他们的脚本里引用预制体,在攻击或者加血的函数里,生成文字提示显示预制体。如下:
	Transform tm = (Transform) Instantiate(hitReport , (game.player.position + new Vector3(0.0f,1.6f,0.0f)),Quaternion.identity);//在被攻击的英雄头上加1.6m的位置生成此预制体。
							tm.gameObject.SetActive(true);//显示预制体
							Hit tmHit = tm.GetComponent<Hit>();//获取预制体父物体的脚本
							tmHit.text = ((int)(Mathf.Abs(damage))).ToString();//设置显示信息,伤害值取绝对值整数

大功告成。
结论:
消息显示类都可以借用此案例,注意这种显示类是作为2D显示在屏幕上的,所以正面一定要始终朝向摄像机,要不然就诡异了。显示其他信息也一样,把内容变一下。

发布了50 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_23158477/article/details/105200518