zoukankan      html  css  js  c++  java
  • 循环移动Text (类公告)

    1.背景(Text移动区域)

    添加Mask组件

    2.添加Text

    设置锚点
    添加ContenSizeFitter组件:使Text长度随着text内容改变

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System;
    public class TextMove : MonoBehaviour
    {
        public Image imgMask; //移动区域组件
        public Text txt;
        //存放公告内容
        Dictionary<int, string> StrTxt = new Dictionary<int, string>();
    
        float maskWidth; // 遮罩的宽度  (text起始位置
        float txtWidth; // 文本宽度 
        float speed = 30; // 移动速度
        int txtid;
    
        private void Start()
        {
            maskWidth = imgMask.GetComponent<RectTransform>().rect.width;
            UnityEngine.Debug.Log("遮罩宽度:" + maskWidth);
    
            StrTxt.Add(0, "欢迎来到八点上班个发射");
            StrTxt.Add(1, "学习使我进步不熬夜不追剧");
            StrTxt.Add(2, "只有10086嘘寒问暖");
    
        }
        private void Update()
        {
            //未移动时给Text赋值  250开始位置
            if (txt.transform.localPosition.x >= 250)
            {
                showTxt(txtid);
            }
            else if (txt.transform.localPosition.x < -(txtWidth + maskWidth)) //移动到左边时复位
            {
                txtid++;
                txt.transform.localPosition = new Vector3(255, 0, 0);
                if (txtid > StrTxt.Count-1)
                {
                    txtid = 0;
                }
            }
    
            txtWidth = txt.GetComponent<RectTransform>().rect.width;
    
            if (txtWidth != 0)
            {
                txt.transform.Translate(Vector3.left * Time.deltaTime * speed);
            }
    
        }
    
        void showTxt(int id)
        {
            //通过key获取value
            string strvalue = "";
            if (StrTxt.TryGetValue(id, out strvalue) == false)
                throw new System.Exception("gradDic TryGetValue Failure! tmp:" + strvalue);
            txt.text = strvalue;
        }
    }
    
    
  • 相关阅读:
    PHP数组、函数
    PHP 基本内容
    Swift基础--tableview练习
    iOS 协议delegate分六步
    UI09_UITableView 使用 单例
    css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?一起来$('.float')
    CSS 如何使DIV层水平居中
    HTML转义字符大全
    jQuery选择器总结
    jQuery 学习笔记_01
  • 原文地址:https://www.cnblogs.com/Ms-Sake/p/10821443.html
Copyright © 2011-2022 走看看