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以内还是没什么问题的,然后再后面乘以自己想要控制的数值。

 

转载于:https://www.cnblogs.com/vital/p/3334533.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class ZiMupanel extends JFrame { public ZiMupanel() { Container con = this.getContentPane(); // 设置窗口名称 this.setTitle("滚动字幕"); // 设置布局管理器为 null ,方便 面板定位,一般使用null 布局时,将窗口设置不可改变大小 this.setLayout(null); // 设置窗口位置和大小 this.setBounds(300, 300, 460, 330); // 设置窗口可见 this.setVisible(true); // 设置不可改变大小 this.setResizable(false); // 设置当点击窗口的关闭按钮时退出 // 设置此窗体关闭,滚动面板不关闭 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Panelfont1 p1 = new Panelfont1(); con.add(p1); p1.setBounds(15, 15, 175, 60); Panelfont2 p2 = new Panelfont2(); con.add(p2); p2.setBounds(195, 15, 250, 60); Panelneizi p3 = new Panelneizi(); con.add(p3); p3.setBounds(15, 80, 175, 80); Paneldirection p4 = new Paneldirection(); con.add(p4); p4.setBounds(195, 80, 250, 80); Panel5 p5 = new Panel5(); con.add(p5); p5.setBounds(15, 155, 430, 70); Panel6 p6 = new Panel6(); con.add(p6); p6.setBounds(15, 220, 430, 70); } } class Panelfont1 extends JPanel implements ActionListener{ public Panelfont1() { this.setBorder(BorderFactory.createTitledBorder("滚动字幕的文字大小控制"));//设置 面板边框 this.setLayout(new GridLayout(1, 2)); bigfont = new JRadioButton("大字体", false); smallfont = new JRadioButton("小字体", true); bigfont.addActionListener(this); smallfont.addActionListener(this); ButtonGroup sexRadioButtonGroup = new ButtonGroup();// 创建一个选按钮组 sexRadioButtonGroup.add(bigfont);// 将单选按钮对象添加到按钮组 sexRadioButtonGroup.add(smallfont);// 将单选按钮对象添加到按钮组 //Enumeration elements = sexRadioButtonGroup.getElements();// 遍历按钮组的所有按钮 this.add(bigfont); this.add(smallfont); } public void actionPerformed(ActionEvent e) { if(bigfont.isSelected()){ fontSize = 50; }else { fontSize = 36; } Font font = new Font(fontName,Font.BOLD,fontSize);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值