VoiceController.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. public class VoiceController : MonoBehaviour
  6. {
  7. // Start is called once before the first execution of Update after the MonoBehaviour is created
  8. private VisualElement waveForm;
  9. private Button voiceBtn;
  10. private Coroutine waveCoroutine;
  11. void Start()
  12. {
  13. var root = GetComponent<UIDocument>().rootVisualElement;
  14. var voiceArea = root.Q<VisualElement>("voiceArea");
  15. waveForm = voiceArea.Q<VisualElement>("waveForm");
  16. voiceBtn = voiceArea.Q<Button>("voice");
  17. // root加载完成后,所有element算出位置后再进行计算
  18. root.RegisterCallback<GeometryChangedEvent>(e => OnMainMenuClickGeometryChanged(e));
  19. }
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. }
  24. IEnumerator waveFormAnimation()
  25. {
  26. while (true)
  27. {
  28. waveForm.style.height = UnityEngine.Random.Range(15f, 30f);
  29. float alpha = UnityEngine.Random.Range(0.6f, 1f);
  30. waveForm.style.unityBackgroundImageTintColor = new Color(1f, 1f, 1f, alpha);
  31. yield return new WaitForSeconds(0.1f);
  32. }
  33. }
  34. void OnMainMenuClickGeometryChanged(GeometryChangedEvent evt)
  35. {
  36. if (waveCoroutine == null)
  37. {
  38. waveCoroutine = StartCoroutine(waveFormAnimation());
  39. }
  40. }
  41. }