51Testing软件测试论坛
标题:
【Unity】【UI.Text】【Code】通用代码库——文字循环滚动+touch控制上下滚动
[打印本页]
作者:
橙子0012
时间:
2019-3-26 15:52
标题:
【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;
}
}
}
}
复制代码
作者:
Miss_love
时间:
2020-12-30 17:23
支持分享
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2