简体   繁体   English

unity3d中的聊天背景

[英]Chat background in unity3d

I want to ask a question. 我想问个问题。 How could I make a chat background similar like this link? 我怎样才能使像这样的链接聊天背景?

Link 链接

With the auto text (the text will automatically typed one by one words per time). 使用自动文本(文本每次会自动一一键入一个文字)。

The thing is, I already done the chat background similar to the image above, but with the transparent background, when I tried to fill the transparent background with the text in it, the text did not show. 问题是,我已经完成了与上图类似的聊天背景,但是使用透明背景,当我尝试在透明背景中填充文本时,文本没有显示。

Here is the image when it is on transparent background: 这是透明背景上的图像:

在此处输入图片说明

And here is the code that I have been using for this one (auto text): 这是我一直在使用的代码(自动文本):

using UnityEngine;
using System.Collections;

public class AutoText : MonoBehaviour 
{
    public float letterPause = 0.5f; // Define and set the pause for each letters

    public AudioClip sound = null; // For the sound

    public GUISkin customLabel = null; // For the label

    public static string message = "Help...! Help...!" + "\n" + "Somebody...! Please Help Me...!"; // The message

    private void Start()
    {
        StartCoroutine(TypedText());
    }

    private void OnGUI()
    {
        GUI.Label(new Rect((Screen.width / 2) - 250, (Screen.height / 2) - 200, 500, 500), message, customLabel.customStyles[0]);
    }

    IEnumerator TypedText()
    {
        // Loop through the message
        foreach (char letter in message.ToCharArray())
        {
            // Set the gui text to be same with message
            guiText.text += letter;

            // If sound available
            if (sound)
            {
                // Play it on each words
                audio.PlayOneShot(sound);

                // Go back to the if statement
                yield return 0;
            }

            // Each letters will be shown for how many seconds delay
            yield return new WaitForSeconds(letterPause);
        }
    }
}

For the text itself, I create a gui text and the gui skinin the unity editor and put the script on it, like the below image: 对于文本本身,我在统一编辑器中创建一个gui文本和gui外观并将脚本放在上面,如下图所示:

在此处输入图片说明

And also, when I tried to set the chat background to not be transparent, the text is on behind the chat background (so the text not been shown) 而且,当我尝试将聊天背景设置为不透明时,该文本位于聊天背景后面(因此该文本未显示)

Thank you very much sir for answering this question and interest with it 先生,非常感谢您回答这个问题并感兴趣。

Can't you just try and draw the Texture behind the Text :)? 您不能只是尝试在Text :)后面绘制纹理吗?

Start by adding the texture as a variable in the script 首先在脚本中将纹理添加为变量

public Texture2D Background;

Then change your OnGUI into 然后将您的OnGUI更改为

private void OnGUI()
{
    GUI.DrawTexture(new Rect((Screen.width / 2) - 250, (Screen.height / 2) - 200, 500, 500), Background);
    GUI.Label(new Rect((Screen.width / 2) - 250, (Screen.height / 2) - 200, 500, 500), message, customLabel.customStyles[0]);
}

Don't forget to assign the background texture in the editor first; 不要忘了先在编辑器中分配背景纹理。 Also on the imported texture. 还要放在导入的纹理上。 Set the Transparency to be from the alpha channel. 将透明度设置为来自Alpha通道。

Without testing this I can ALMOST guarantee that it will work :-) 如果没有测试,我可以保证它会工作:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM