1.在固定位置显示不同内容的提示信息MessageManager:
在想要显示提示的位置创建:messageLabel
public class MessageManager : MonoBehaviour {
public static MessageManager _instance;
private UILabel messageLabel;
private TweenAlpha tween_alpha;
private bool isTweenFinished = false;
private void Awake()
{
_instance = this;
messageLabel = transform.Find("Label").GetComponent<UILabel>();
tween_alpha = this.GetComponent<TweenAlpha>();
this.gameObject.SetActive(false);
}
/// <summary>
/// 对外显示提示信息.
/// </summary>
public void ShowMessage(string message,float time)
{
this.gameObject.SetActive(true);
StartCoroutine(Show(message, time));
}
/// <summary>
/// 方法一:纯IEnumerator实现方法.
/// </summary>
IEnumerator Show(string message,float time=1)
{
tween_alpha.PlayForward();
messageLabel.text = message;
yield return new WaitForSeconds(time);
tween_alpha.PlayReverse();
yield return new WaitForSeconds(1);
this.gameObject.SetActive(false);
}
}
2.战斗力数值的跳动显示
在想要显示战斗力跳动的位置创建numLabel
public class PowerShow : MonoBehaviour {
private UILabel numLabel;//UI上显示的Label
private TweenAlpha tweenAlpha;
private float startValue = 0;//初始值.
private int endValue = 1000;//结束值.
private bool isStart = false;//是否开始显示数值的变化.
private bool isUp = true;//是否是数值的上升.
private int speed = 500;//数值变化速度.
private void Awake()
{
numLabel = transform.Find("Label").GetComponent<UILabel>();
tweenAlpha = transform.GetComponent<TweenAlpha>();
this.transform.gameObject.SetActive(false);//默认不显示.
}
private void Update()
{
if(isStart)
{
if (isUp)
{
startValue += speed * Time.deltaTime; //每秒数值变动幅度为500.
if (startValue>endValue)
{//当初始值大于结束值时结束.
isStart = false;
startValue = endValue;
StartCoroutine(HidePowerChangePanel());//隐藏显示面板.
}
}
else
{
startValue -= speed * Time.deltaTime;
if (startValue < endValue)
{
isStart = false;
startValue = endValue;
StartCoroutine(HidePowerChangePanel());
}
}
numLabel.text = (int)startValue + "";
}
}
/// <summary>
/// 显示战斗力的数值变化.
/// </summary>
public void ShowPowerChange(float startValue,int endValue)
{
isStart = true;
gameObject.SetActive(true);
tweenAlpha.PlayForward();
this.startValue = startValue;
this.endValue = endValue;
if (startValue<endValue)
{
isUp = true;
}
else
{
isUp = false;
}
}
/// <summary>
/// 隐藏战斗力数值变化面板.
/// </summary>
IEnumerator HidePowerChangePanel()
{
tweenAlpha.PlayReverse();
yield return new WaitForSeconds(1);
gameObject.SetActive(false);
}
}