SideBar.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using DG.Tweening;
  6. public class SideBar : MonoBehaviour
  7. {
  8. [SerializeField] Image switchButtonImage;
  9. [SerializeField] Sprite leftArrow;
  10. [SerializeField] Sprite rightArrow;
  11. [SerializeField] float defaultPos_x;
  12. [SerializeField] float shrinkPos_x;
  13. [SerializeField] GameObject cover;
  14. [SerializeField] GameObject popup;
  15. [SerializeField] GameObject parent;
  16. bool isShrink { get; set; } = false;
  17. public void Init()
  18. {
  19. OffSideBar();
  20. parent.gameObject.SetActive(true);
  21. cover.SetActive(false);
  22. }
  23. public void OffSideBar(bool returnDefaultPos = true)
  24. {
  25. if (popup != null)
  26. {
  27. popup.SetActive(false);
  28. }
  29. if (returnDefaultPos)
  30. {
  31. SetIsShrink(false);
  32. }
  33. }
  34. public void SetUpSideBar()
  35. {
  36. if (popup != null)
  37. {
  38. popup.SetActive(true);
  39. }
  40. SetIsShrink(false);
  41. }
  42. public void SetIsShrink(bool isShrink)
  43. {
  44. this.isShrink = isShrink;
  45. if (!isShrink)
  46. {
  47. parent.transform.localPosition = new Vector2(defaultPos_x, parent.transform.localPosition.y);
  48. switchButtonImage.sprite = leftArrow;
  49. }
  50. else
  51. {
  52. parent.transform.localPosition = new Vector2(shrinkPos_x, parent.transform.localPosition.y);
  53. switchButtonImage.sprite = rightArrow;
  54. }
  55. }
  56. public void OnClickSwitchButton()
  57. {
  58. ContinuousController.instance.StartCoroutine(movePopup());
  59. }
  60. IEnumerator movePopup()
  61. {
  62. bool end = false;
  63. float anjmTime = 0.16f;
  64. float targetPos_x = isShrink ? defaultPos_x : shrinkPos_x;
  65. cover.SetActive(true);
  66. var sequence = DOTween.Sequence();
  67. sequence.Append(parent.transform.DOLocalMoveX(targetPos_x, anjmTime))
  68. .AppendCallback(() => end = true);
  69. yield return new WaitWhile(() => !end);
  70. end = false;
  71. SetIsShrink(!isShrink);
  72. cover.SetActive(false);
  73. }
  74. }