【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;
}
}
}
}
支持分享
页:
[1]