簡體   English   中英

啟用它為false時如何淡出ui文本?

[英]How can I fade out the ui text when enable it false?

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

public class SaveGameMessage : MonoBehaviour
{
    public Text text;
    public float speed;
    public bool startFading = false;
    public bool enableText = false;

    private Color textColor;

    private void Start()
    {
        text = GetComponent<Text>();
        textColor = text.color;
    }

    private void Update()
    {
        if (startFading == true)
        {
            if (enableText == true)
            {
                text.enabled = true;
            }

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

並使用它:

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

public class PlayingInGameScenesController : MonoBehaviour
{
    public CinemachineFreeLook[] freeLookcameras;
    public LockController lockController;
    public GameObject uiSceneText;
    public GameObject player;
    public float transitionSpeed = 5f;
    public float thresHold = 0.1f;
    public bool talkingProcess = true;
    public SaveGameMessage saveGameMessage;

    private bool newGame = true;

    private void Start()
    {
        
    }

    public void PlayingSceneInGame()
    {
        PlayingSceneStatesControls(true);
        StartCoroutine(ScenePlayingTime());
    }

    private void Update()
    {
        if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true)
        {
            PlayingSceneInGame();
            newGame = false;
        }
    }

    private void LateUpdate()
    {
       
    }

    private void PlayingSceneStatesControls(bool LockState)
    {
        lockController.LockControl(LockState);

        if (LockState == true)
        {
            uiSceneText.SetActive(true);
        }
        else
        {
            talkingProcess = false;
            uiSceneText.SetActive(false);
        }
    }

    IEnumerator ScenePlayingTime()
    {
        yield return new WaitForSeconds(10);

        PlayingSceneStatesControls(false);

        freeLookcameras[0].enabled = false;
        freeLookcameras[1].enabled = true;

        var brain = Camera.main.GetComponent<CinemachineBrain>().m_DefaultBlend.m_Time;

        saveGameMessage.enableText = true;
        saveGameMessage.startFading = true;

        player.GetComponent<Player>().SavePlayer();

        StartCoroutine(FakeSave());
    }

    IEnumerator FakeSave()
    {
        yield return new WaitForSeconds(7);

        saveGameMessage.startFading = false;
    }
}

在底部,我偽造了 7 秒,問題是當偽造完成時,ui 文本立即啟用 false。 在底部的 SaveGameMessage 腳本中,我正在做:

text.enabled = false;

但這很丑陋,它只是立即停止淡入淡出效果。 我想以某種方式啟用它 false 但在此之前完成當前的淡入淡出,這樣啟用 false 的文本將平滑淡出,而不是突然停止淡入淡出效果。

在禁用它之前運行動畫以使其淡出。 應該先制作這個動畫,或者你可以在網上找到一個。

然后在將其設置為 false 之前運行動畫。

暫無
暫無

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

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