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.

89 lines
2.1 KiB

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