MyForm.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MyForm : MonoBehaviour {
  5. public KeyCode key = KeyCode.None;
  6. public bool defaultShow = false;
  7. private Vector2 originalPosition;
  8. public void Awake() {
  9. // 绑定显示按键(切换显示状态)
  10. if (key != KeyCode.None) {
  11. InputM.AddKeyboardEvent(key, () => SwapShowing());
  12. }
  13. // 保存初始位置
  14. originalPosition = GetComponent<RectTransform>().anchoredPosition;
  15. }
  16. // 设置或更改窗口的显示状态
  17. public void SwapShowing() {
  18. FormM.currentForm = (FormM.currentForm == this ? null : this);
  19. }
  20. public bool isShowing {
  21. get { return gameObject.activeSelf; }
  22. set {
  23. if (isShowing == value) return;
  24. if (value) {
  25. gameObject.SetActive(true);
  26. GetComponent<RectTransform>().anchoredPosition = originalPosition; // 还原到初始位置
  27. } else {
  28. gameObject.SetActive(false);
  29. }
  30. }
  31. }
  32. }