今天在复刻黑暗之魂制作游戏过程中,想创建一个篝火,来让人物有地方保存和进行其他的操作,这个时候我在网上查资料,看到了一个大神的做法,在这里分享一下。
首先,前面的配置已经完全制作完毕,这时候我创建了以下ui,如图所示:
我在下面的着张图中添加了一个canva ground来控制字体的透明度(Alpha),我将透明度改为0,此时文字应该是不可见的,然后在脚本中更改透明度使得文字能够逐渐显现。
脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SG
{
public class bonfireLitpopupUI : MonoBehaviour
{
public CanvasGroup canvas;
private void Awake()
{
canvas = GetComponent<CanvasGroup>();
}
public void displayBonfireLitpopup()
{
StartCoroutine(fadeinpopup());
}
//文字逐渐出现
IEnumerator fadeinpopup()
{
gameObject.SetActive(true);
for(float fade = 0.05f ;fade < 1;fade= fade + 0.05f)
{
canvas.alpha = fade;
if(fade > 0.9f)
{
StartCoroutine(fadeOutPopup());
}
yield return new WaitForSeconds(0.05f);
}
}
//文字逐渐消失
IEnumerator fadeOutPopup()
{
yield return new WaitForSeconds(2);
for(float fade = 1f;fade >0;fade = fade - 0.05f)
{
canvas.alpha = fade;
if(fade <= 0.05f)
{
gameObject.SetActive(false);
}
yield return new WaitForSeconds(0.05f);
}
}
}
}
然后在总的UImanager里面,我的UImanager脚本是挂载在bonfireLitpopupUI脚本的父节点上的
[Header("pop ups")]
public bonfireLitpopupUI bonfireLitpopupUI;
private void Awake()
{
bonfireLitpopupUI = GetComponentInChildren<bonfireLitpopupUI>();
}
public void activateBonfirepopup()
{
bonfireLitpopupUI.displayBonfireLitpopup();
}
然后在篝火所挂载里面的脚本调用这个activateBonfirepopup()函数,然后就可以正常使用,使得文字正常消失或者显现了。
下面是我的文字设置,这里我使用了外置的脚本,点击windows窗口,在Windows里面点击TextMeshPro,然后点击Font Asset Creator,把外置的通过unity工具然后实现了字体的转换。
最后效果:
整体效果是正常显现的,这里使用图片的形式进行展示