【Unity】【UI.Text】【Code】通用代码库(五)——文字循环滚动+touch控制上下滚动

来源:互联网 发布:php 敏感词过滤 编辑:程序博客网 时间:2024/04/30 05:50

通用代码库
基于Unity5.6.0f

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class TextRoll : MonoBehaviour{    public Transform txt;    public float Speed = 20;//滚动速度    public double num;//Screen.height的系数,    private Vector3 txtpos;    // Use this for initialization    void Start ()    {        txt.localPosition = new Vector3(txt.localPosition.x, -Screen.height* 0.25f, txt.localPosition.z);        txtpos = txt.localPosition;        Debug.Log("txtpos: " + txtpos);    }    // Update is called once per frame    void Update ()    {        //手指控制text上下滚动        if (1 == Input.touchCount)        {            var touch = Input.GetTouch(0);            if (touch.position.y > Screen.height * 0.0f &&                touch.position.y < Screen.height * 0.25f)//限制touch范围            {                if (txt.localPosition.y <= num * Screen.height ||                     txt.localPosition.y >= txtpos.y)//限制text高度                {                    Vector2 deltaPos = touch.deltaPosition;                    transform.Translate(new Vector3(0, 10*deltaPos.y * Time.deltaTime, 0), Space.World);                }            }        }        //文字自动上下循环滚动        if (Input.touchCount == 0)        {            if (txt.localPosition.y <= num * Screen.height)            {                float y = txt.localPosition.y + Speed * Time.deltaTime;                txt.localPosition = new Vector3(0, y, 0);                //Debug.Log(txt.position.y);            }            else            {                txt.localPosition = txtpos;            }        }    }}