51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1902|回复: 1

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

[复制链接]

该用户从未签到

发表于 2019-3-26 15:52:59 | 显示全部楼层 |阅读模式
通用代码库
基于Unity5.6.0f
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;

  5. public class TextRoll : MonoBehaviour
  6. {
  7.     public Transform txt;
  8.     public float Speed = 20;//滚动速度
  9.     public double num;//Screen.height的系数,


  10.     private Vector3 txtpos;

  11.     // Use this for initialization
  12.     void Start ()
  13.     {
  14.         txt.localPosition = new Vector3(txt.localPosition.x, -Screen.height* 0.25f, txt.localPosition.z);
  15.         txtpos = txt.localPosition;
  16.         Debug.Log("txtpos: " + txtpos);



  17.     }

  18.     // Update is called once per frame
  19.     void Update ()
  20.     {

  21.         //手指控制text上下滚动
  22.         if (1 == Input.touchCount)
  23.         {
  24.             var touch = Input.GetTouch(0);
  25.             if (touch.position.y > Screen.height * 0.0f &&
  26.                 touch.position.y < Screen.height * 0.25f)//限制touch范围
  27.             {
  28.                 if (txt.localPosition.y <= num * Screen.height ||
  29.                     txt.localPosition.y >= txtpos.y)//限制text高度
  30.                 {
  31.                     Vector2 deltaPos = touch.deltaPosition;
  32.                     transform.Translate(new Vector3(0, 10*deltaPos.y * Time.deltaTime, 0), Space.World);
  33.                 }

  34.             }
  35.         }

  36.         //文字自动上下循环滚动
  37.         if (Input.touchCount == 0)
  38.         {
  39.             if (txt.localPosition.y <= num * Screen.height)
  40.             {
  41.                 float y = txt.localPosition.y + Speed * Time.deltaTime;
  42.                 txt.localPosition = new Vector3(0, y, 0);
  43.                 //Debug.Log(txt.position.y);
  44.             }
  45.             else
  46.             {

  47.                 txt.localPosition = txtpos;
  48.             }
  49.         }




  50.     }
  51. }
复制代码


回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-3-29 22:30 , Processed in 0.064694 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表