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

通用代码库
基于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;
            }
        }




    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值