1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UIElements;
- public class VoiceController : MonoBehaviour
- {
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- private VisualElement waveForm;
- private Button voiceBtn;
- private Coroutine waveCoroutine;
- void Start()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- var voiceArea = root.Q<VisualElement>("voiceArea");
- waveForm = voiceArea.Q<VisualElement>("waveForm");
- voiceBtn = voiceArea.Q<Button>("voice");
- // root加载完成后,所有element算出位置后再进行计算
- root.RegisterCallback<GeometryChangedEvent>(e => OnMainMenuClickGeometryChanged(e));
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- IEnumerator waveFormAnimation()
- {
- while (true)
- {
- waveForm.style.height = UnityEngine.Random.Range(15f, 30f);
- float alpha = UnityEngine.Random.Range(0.6f, 1f);
- waveForm.style.unityBackgroundImageTintColor = new Color(1f, 1f, 1f, alpha);
- yield return new WaitForSeconds(0.1f);
- }
- }
- void OnMainMenuClickGeometryChanged(GeometryChangedEvent evt)
- {
- if (waveCoroutine == null)
- {
- waveCoroutine = StartCoroutine(waveFormAnimation());
- }
- }
- }
|