82 lines
2.5 KiB
C#
82 lines
2.5 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using TMPro;
|
||
|
|
using UnityEngine;
|
||
|
|
using XCharts.Runtime;
|
||
|
|
using Random = UnityEngine.Random;
|
||
|
|
|
||
|
|
public class StretchRectangle : MonoBehaviour
|
||
|
|
{
|
||
|
|
public TextMeshProUGUI text;
|
||
|
|
private RectTransform rectTransform;
|
||
|
|
private Vector2 initialMousePosition;
|
||
|
|
private float initialHeight;
|
||
|
|
private bool isDragging;
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
// 获取 RectTransform 组件
|
||
|
|
|
||
|
|
// 设置 pivot 在下边 (0.5f, 0f) -> 水平居中,垂直在底部
|
||
|
|
/*rectTransform.pivot = new Vector2(0.5f, 0f);
|
||
|
|
|
||
|
|
// 设置 anchorMin 和 anchorMax 固定在下边
|
||
|
|
rectTransform.anchorMin = new Vector2(rectTransform.anchorMin.x, 0f); // 下锚点
|
||
|
|
rectTransform.anchorMax = new Vector2(rectTransform.anchorMax.x, 0f); // 下锚点*/
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnEnable()
|
||
|
|
{
|
||
|
|
rectTransform = GetComponent<RectTransform>();
|
||
|
|
|
||
|
|
StartCoroutine(Zoom());
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator Zoom()
|
||
|
|
{
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
float y = rectTransform.sizeDelta.y;
|
||
|
|
if (Random.Range(0, 10) < 2 && y > 40) y=y-4.4f;
|
||
|
|
if (Random.Range(0, 10) > 7 && y < 245) y=y+4.4f;
|
||
|
|
float i = (y - 32) / 4.4f;
|
||
|
|
text.text = Mathf.Floor(i).ToString();
|
||
|
|
rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, y);
|
||
|
|
int time = Random.Range(2, 5);
|
||
|
|
yield return new WaitForSeconds(time/2); // 等待两秒
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
/*// 鼠标左键按下时开始拖动
|
||
|
|
if (Input.GetMouseButtonDown(0))
|
||
|
|
{
|
||
|
|
/*initialMousePosition = Input.mousePosition;
|
||
|
|
initialHeight = rectTransform.rect.height;#1#
|
||
|
|
isDragging = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 鼠标拖动时更新高度
|
||
|
|
if (Input.GetMouseButton(0) && isDragging)
|
||
|
|
{
|
||
|
|
/*Vector2 mouseDelta = (Vector2)Input.mousePosition - initialMousePosition;
|
||
|
|
float newHeight = Mathf.Max(initialHeight - mouseDelta.y, 0); // 拉伸上边,确保高度不为负#1#
|
||
|
|
|
||
|
|
// 更新矩形的高度
|
||
|
|
float y = rectTransform.sizeDelta.y;
|
||
|
|
if (Random.Range(0, 10) < 2 && y > 40) y--;
|
||
|
|
if (Random.Range(0, 10) < 7 && y < 250) y++;
|
||
|
|
|
||
|
|
rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, y);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 鼠标松开时结束拖动
|
||
|
|
if (Input.GetMouseButtonUp(0))
|
||
|
|
{
|
||
|
|
isDragging = false;
|
||
|
|
}*/
|
||
|
|
}
|
||
|
|
}
|