You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

  1. using System.Collections;
  2. using UnityEngine;
  3. public abstract class PlayerUIManager : MonoBehaviour
  4. {
  5. [SerializeField]
  6. protected InventoryUI Inventory;
  7. [SerializeField]
  8. private float AnimationSpeed = 0.5f;
  9. [SerializeField]
  10. protected LogicTrayUI TrayUI;
  11. protected Vector3 ShowPosition;
  12. private Vector3 HidePosition;
  13. private RectTransform inventoryRect { get { return Inventory.transform as RectTransform; } }
  14. protected virtual void Awake()
  15. {
  16. ShowPosition = new Vector3(0, -inventoryRect.rect.height / 2, 0);
  17. HidePosition = new Vector3(0, inventoryRect.rect.height/2, 0);
  18. inventoryRect.anchoredPosition = HidePosition;
  19. }
  20. [ContextMenu("Show")]
  21. public void OnClick_Show()
  22. {
  23. StopAllCoroutines();
  24. Inventory.UpdateUI();
  25. StartCoroutine( LerpPosition(inventoryRect, ShowPosition, AnimationSpeed));
  26. }
  27. [ContextMenu("Hide")]
  28. public void OnClick_Hide()
  29. {
  30. StopAllCoroutines();
  31. StartCoroutine( LerpPosition(inventoryRect, HidePosition, AnimationSpeed));
  32. }
  33. public void OnClick_Toggle()
  34. {
  35. if (Vector3.Distance(inventoryRect.anchoredPosition,ShowPosition) < Vector3.Distance(inventoryRect.anchoredPosition, HidePosition))
  36. {
  37. Debug.Log("Hiding Menu");
  38. OnClick_Hide();
  39. }
  40. else
  41. {
  42. Debug.Log("Showing Menu");
  43. OnClick_Show();
  44. }
  45. }
  46. public abstract void OnClick_Play();
  47. private IEnumerator LerpPosition(RectTransform rt, Vector3 endPosition, float time)
  48. {
  49. float elapsedTime = 0;
  50. Vector3 startPos = rt.anchoredPosition;
  51. while (elapsedTime < time)
  52. {
  53. rt.anchoredPosition = Vector3.Slerp(startPos, endPosition, (elapsedTime / time));
  54. elapsedTime += Time.deltaTime;
  55. yield return new WaitForEndOfFrame();
  56. }
  57. rt.anchoredPosition = endPosition;
  58. }
  59. }