ユニティツール:画面に表示テキスト

仕事のプロジェクトは、我々は多くの場合、画面上で見て理解するプレーヤーのために、より有利であることがショーの成功に間違ったキューを使用して、我々は、操作を必要とし、この時間は、我々が表示画面上に表示されるために必要なものを送って、次に実行することですテキストコンテンツ、下図のように:

我々はテキストを読み込んでテキストと表示の2つのスクリプトの内容を必要とするこれを実現:

次のようにugui上記成分のテキストは、次のとおりです。

 

次のスクリプトは、テキストが表示されるクリックの主な成果です。

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

public class ShowContentScript : MonoBehaviour {
    void Update () {
        if (Input.GetKeyDown(KeyCode.A))
        {
            PrompPanel.isStartShowText = true;
            PromptMsg.Instance.Change("小松真帅",Color.red);
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            PrompPanel.isStartShowText = true;
            PromptMsg.Instance.Change("大荣真丑", Color.black);
        }
    }
}

 

下面这个脚本主要实现的是文字内容的显示与消失以及颜色的设置:

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

public class PrompPanel : MonoBehaviour {

   
    private Text txt;
    private CanvasGroup cg;

    [SerializeField]
    [Range(0, 3)]
    private float showTime = 1f;

    private float timer = 0f;

    public static bool isStartShowText=false;

    void Start()
    {
        txt = transform.Find("Text").GetComponent<Text>();
        cg = transform.Find("Text").GetComponent<CanvasGroup>();

        cg.alpha = 0;
    }
    private void Update()
    {
        if (isStartShowText)
        {
            promptMessage(PromptMsg.Instance.Text, PromptMsg.Instance.Color);
            isStartShowText = false;
        }
    }
    /// <summary>
    /// 提示消息
    /// </summary>
    private void promptMessage(string text, Color color)
    {
        txt.text = text;
        txt.color = color;
        cg.alpha = 0;
        timer = 0;
        //做动画显示
        StartCoroutine(promptAnim());
    }

    /// <summary>
    /// 用来显示动画
    /// </summary>
    /// <returns></returns>
    IEnumerator promptAnim()
    {
        while (cg.alpha < 1f)
        {
            cg.alpha += Time.deltaTime * 2;
            yield return new WaitForEndOfFrame();
        }
        while (timer < showTime)
        {
            timer += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }
        while (cg.alpha > 0)
        {
            cg.alpha -= Time.deltaTime * 2;
            yield return new WaitForEndOfFrame();
        }
    }
}

  上面两端代码就能够较为轻松的实现 文本提示等内容的显示,这个东西我们可以设置我们的一个小工具类,当我们需要进行一些内容的显示的时候就可以用到上面这个方法了!!!!!!!!!!!!!!!!!!!!!!!!!!

おすすめ

転載: www.cnblogs.com/baosong/p/10958216.html