PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Unity3D教程 > 正文

    Unity 打字机(不用插件)

    作者:张志勇来源:原创浏览:时间:2020-09-30 00:07:50我要评论
    导读:using UnityEngine; using System.Collections; public class GUIPrintMachine : MonoBehaviour { //间隔时间pr...

    using UnityEngine;     

     using System.Collections;          

     public class GUIPrintMachine : MonoBehaviour      {       

     //间隔时间

    private float letterPause = 0.2f;              

    public AudioClip clip;                

    private AudioSource source;          

    private string word;          // 要显示的内容

    private string text = "text test";         

    void Start() {              

    source = GetComponent();              

    word = text;             

    text = "";              

    StartCoroutine(TypeText());         

     }                

    void OnGUI() {              

    GUI.Label(new Rect(100, 100, 200, 200), "我叫张志勇,很帅气!");              

    GUI.Label(new Rect(50, 50, 250, 250), text);         

     }        

    private IEnumerator TypeText() {

     foreach (char letter in word.ToCharArray()) {                 

    text += letter;             

    if(clip ){                     

    source.PlayOneShot(clip);                  

    }                 

     yield return new WaitForSeconds(letterPause);             

     }         

     }    

     }

    这是GUI做的,当然现在这么流行UGUI,可以直接在这里改成UGUI,

    1.Hierarchy中创建Text,可以修改一下Vertical Overflow用来测试

    2.引入UI,c#中public一个Text

    3.OnGUI中的闪出了,直接在Update中赋值Text.text = textString;

    别忘了运行时候把Text拖文件上

     

    如果是NGUI的话这样搞

    NGUI创建Label,添加组件Typewriter Effect

    Chars Per Second :  每秒显示多少字符

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-130-5977-1.html
    相关热词搜索: