zoukankan      html  css  js  c++  java
  • unity3D中GUI标题内文字滚动效果

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class RollTxt : MonoBehaviour {
        //支持中文
        private string txt = "1234567werweerty74874651rtyrghdfgdgdfg891234wrew56789";
        public string showTxt;
        public int showLength = 8;
        public int txtLength;
        public float rollSpeed = 0.1f;
        private int indexMax;
        // Use this for initialization
        void Start () 
        {
            txtLength = txt.Length;
            showTxt = txt.Substring(0,showLength);
            indexMax = txtLength - showLength + 1;
        }
    
        // Update is called once per frame
        void Update () 
        {
            GetShowTxt();
        }
    
        void OnGUI()
        {
            GUI.Box(new Rect(200,200,150,20),showTxt);
        }
    
        void GetShowTxt() 
        {
            if(showLength>=txtLength)
            {
                showTxt = txt;
            }
            else if(showLength<txtLength)
            {
                int startIndex = 0;
                startIndex = (int)(Mathf.PingPong(Time.time * rollSpeed, 1) * indexMax);
                showTxt = txt.Substring(startIndex,showLength);
            }
        }
    }

    由于有的时候在GUI内输入一些文字标题的时候,标题的字符长度会超过我们排版要求的长度。有两种方法解决,第一种就是在超过的部分最后用“…”来代替,第二种就是让标题滚动起来。

    第一种不做讨论,第二种还有一种方法,就是给GUI打组,然后动态修改组内标题GUI的X或Y的坐标,但是比较麻烦一些。

    由于Mathf.PingPong(float t,float length);这个函数的来回动荡速度是和字符串的长度有关,所以要根据情况调整速度。

    注意:Mathf.PingPong(float t,float length);unity官方给出的这个函数并不是我们想的那么好用,当length过大的时候会出现不受控制的情况,不知道为什么。解决方法就是把length全部写成1,在10以内还是没什么问题的,然后再后面乘以自己想要控制的数值。

  • 相关阅读:
    HTML <input> 标签的 maxlength 属性
    HTTP 方法:GET 对比 POST
    怎么在html页面和js里判断是否是IE浏览器
    一行神奇的javascript代码
    c# 数据库批量插入数据SqlBulkCopy 示例
    c# 多线程调用窗体上的控件 示例
    sqlserver查找使用了某个字段的所有存储过程
    SQL Server 数据库性能优化
    SQL语句的执行过程
    Sql Server- 性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON
  • 原文地址:https://www.cnblogs.com/vital/p/3334533.html
Copyright © 2011-2022 走看看