You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中C#实现TextView条件显示及5秒后隐藏循环功能

Hey there! Let's tackle your two Unity text visibility questions— I’m assuming you’re working with UGUI’s Text component (if you’re using TextMeshPro, the changes are super minor). Here’s how to implement both features:

1. Show Text When a Specific Condition is Met

First, make sure your Text component starts hidden (you can uncheck its GameObject’s "Active" box in the Inspector, or set it inactive via code on startup). Then, check your condition in a regular update loop (or event callback) and activate the text when the condition is satisfied.

Here’s a sample script:

using UnityEngine;
using UnityEngine.UI;

public class TextVisibilityController : MonoBehaviour
{
    [SerializeField] private Text targetText; // Drag your Text component here in the Inspector
    // Replace this with your actual condition (e.g., player score >= 100, collectible picked up)
    private bool IsConditionMet()
    {
        // Example: Condition is met when the Spacebar is pressed
        return Input.GetKeyDown(KeyCode.Space);
    }

    private void Start()
    {
        // Hide the text on game start
        targetText.gameObject.SetActive(false);
    }

    private void Update()
    {
        if (IsConditionMet())
        {
            // Show the text when condition is satisfied
            targetText.gameObject.SetActive(true);
        }

        // Optional: Uncomment below to hide the text when the condition is no longer met
        // else
        // {
        //     targetText.gameObject.SetActive(false);
        // }
    }
}
  • Attach this script to any GameObject in your scene.
  • Drag your Text component into the targetText field in the Inspector.
  • Replace the IsConditionMet() logic with your own (e.g., checking player stats, collision events, etc.).
2. Show Text for 5 Seconds When Condition is Met (Repeatable)

For this, we’ll use a Coroutine to handle the 5-second delay, and we’ll track the active coroutine to reset the timer if the condition is triggered again while the text is still visible.

Here’s the implementation:

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

public class TimedTextController : MonoBehaviour
{
    [SerializeField] private Text targetText;
    [SerializeField] private float displayDuration = 5f; // Set to 5 seconds as requested
    private Coroutine activeHideCoroutine; // Tracks the current delay coroutine

    private void Start()
    {
        targetText.gameObject.SetActive(false);
    }

    private void Update()
    {
        if (IsConditionMet())
        {
            // Show the text immediately
            targetText.gameObject.SetActive(true);

            // If there's an active delay coroutine, stop it to reset the timer
            if (activeHideCoroutine != null)
            {
                StopCoroutine(activeHideCoroutine);
            }

            // Start a new coroutine to hide the text after the duration
            activeHideCoroutine = StartCoroutine(HideTextAfterDelay(displayDuration));
        }
    }

    private bool IsConditionMet()
    {
        // Example: Condition is met when the Enter key is pressed
        return Input.GetKeyDown(KeyCode.Return);
    }

    // Coroutine to handle delayed hiding
    private IEnumerator HideTextAfterDelay(float delay)
    {
        yield return new WaitForSeconds(delay);
        targetText.gameObject.SetActive(false);
        activeHideCoroutine = null; // Clear the reference once done
    }
}

Key Notes:

  • If the condition is triggered again while the text is visible, the timer resets—so the text will stay visible for another 5 seconds from the new trigger.
  • For TextMeshPro users: Replace Text with TextMeshProUGUI and add using TMPro; at the top of the script.
  • Instead of checking in Update(), you can trigger the text display from event callbacks (e.g., OnTriggerEnter, button clicks) for better performance if your condition doesn’t need to be checked every frame.

内容的提问来源于stack exchange,提问作者Sumit Pal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:43:52