簡體   English   中英

如何使 ui 文本閃爍淡入淡出?

[英]How can I make a ui text to blink fade in out?

首先,我嘗試獲取組件材質,但 UI text沒有材質,因此我嘗試將組件直接獲取到顏色組件,但出現以下錯誤:

ArgumentException: GetComponent 要求請求的組件“Color”派生自 MonoBehaviour 或 Component 或者是一個接口。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveGameMessage : MonoBehaviour
{
    public float speed;
    public Text text;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        var textColor = text.GetComponent<Color>();
        textColor.a = (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
    }
}

更新后的代碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveGameMessage : MonoBehaviour
{
    public float speed;
    public Text text;

    private Color textColor;

    // Start is called before the first frame update
    void Start()
    {
        textColor = text.color;
    }

    // Update is called once per frame
    void Update()
    {
        textColor.a = (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
    }
}

我也嘗試過PingPong

textColor.a = Mathf.PingPong(Time.time * speed, 1.0f);

但它從不閃爍淡入淡出。

有一個Canvas設置為 Screen Space - Overlay 並且Text是它的子項:
如果我在編輯器中更改文本的 alpha 顏色,它將使其透明並返回,但腳本不起作用。 該腳本附加到Text

用戶界面文字

var textColor = text.GetComponent<Color>();

有錯誤不存在組件顏色。

您需要以這種方式訪問​​屬性 Color

var textColor = text.color;

void Update() 
{ 
   textColor.a = (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
   text.color = textColor; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM