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.

77 lines
2.0 KiB

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