Unity tool 1: display text content to screen

Keywords: PHP

In the work project, we often use the error prompt to display the successful prompt on the screen, which is more conducive to the viewing and understanding of players. At this time, we need to send the content we need to display to the display screen, and then realize the display of text content, as shown in the following figure:

To realize this function, we need two script contents and one Text display Text content as follows:

The components above Text in ugui are as follows:

 

The following script mainly implements the display of text content by clicking:

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("Komatsu is really handsome.",Color.red);
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            PrompPanel.isStartShowText = true;
            PromptMsg.Instance.Change("Great glory is ugly.", Color.black);
        }
    }
}

 

The following script mainly implements the display and disappearance of text content and the setting of color:

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>
    ///Prompt message
    /// </summary>
    private void promptMessage(string text, Color color)
    {
        txt.text = text;
        txt.color = color;
        cg.alpha = 0;
        timer = 0;
        //Animate
        StartCoroutine(promptAnim());
    }

    /// <summary>
    ///Used to display animation
    /// </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();
        }
    }
}

The codes at both ends of the above can easily display the text prompt and other contents. We can set up a small tool class for this thing. When we need to display some contents, we can use the above method!!!!!!!!!!!!!!!!!!!!!!!!!!

Posted by d3vilr3d on Sat, 02 Nov 2019 00:16:51 -0700