随机长度效果
说话标识
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RandomLengthEffect : MonoBehaviour {
public Image[] images;
public Vector2[] tempSize;
public Vector2[] targetSize;
public float rate;
public float rangeMin;
public float rangeMax;
public float currRandomValue;
public float timer;
// Use this for initialization
void Start () {
if (images == null) return;
targetSize = new Vector2[images.Length];
tempSize = new Vector2[images.Length];
for (int i = 0; i < images.Length; i++)
{
targetSize[i] = images[i].rectTransform.sizeDelta;
tempSize[i] = images[i].rectTransform.sizeDelta;
}
}
// Update is called once per frame
void Update () {
if (targetSize == null) return;
timer += Time.deltaTime;
if (timer > rate) {
timer = 0;
for (int i = 0; i < targetSize.Length; i++) {
tempSize[i] = images[i].rectTransform.sizeDelta;
currRandomValue = Random.Range(rangeMin, rangeMax);
targetSize[i] = new Vector2(targetSize[i].x, currRandomValue);
}
}
for (int i = 0; i < images.Length; i++) {
images[i].rectTransform.sizeDelta = Vector2.Lerp(tempSize[i],targetSize[i], timer / rate);
//images[i].rectTransform.sizeDelta = new Vector2(targetSize[i].x, targetSize[i].y* (timer / rate));
}
}
}