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.

158 lines
4.4 KiB

7 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class UIPanel : MonoBehaviour
  5. {
  6. public enum UIAnimType
  7. {
  8. Fade,
  9. ZoomIn,
  10. ZoomOut,
  11. SlideRight,
  12. SlideLeft,
  13. SlideUp,
  14. SlideDown
  15. }
  16. public bool showOnAwake = false;
  17. public string overrideNotifyName;
  18. public UIAnimType showAnimType = UIAnimType.Fade;
  19. public UIAnimType hideAnimType = UIAnimType.Fade;
  20. public AnimationCurve fadeCurve;
  21. public AnimationCurve showCurve;
  22. public AnimationCurve hideCurve;
  23. public float time = 0.5f;
  24. public float zoomAmount = 1.2f;
  25. public float slideAmount = 800f;
  26. private RectTransform rectTransform;
  27. private CanvasGroup canvasGroup;
  28. private Vector3 originalPos;
  29. void Awake ()
  30. {
  31. rectTransform = GetComponent<RectTransform>();
  32. canvasGroup = GetComponent<CanvasGroup>();
  33. if (canvasGroup == null)
  34. canvasGroup = gameObject.AddComponent<CanvasGroup>();
  35. if (overrideNotifyName == null || overrideNotifyName == "")
  36. {
  37. NotificationServer.register("show " + gameObject.name, showPanel);
  38. NotificationServer.register("hide " + gameObject.name, hidePanel);
  39. }
  40. else
  41. {
  42. NotificationServer.register("show " + overrideNotifyName, showPanel);
  43. NotificationServer.register("hide " + overrideNotifyName, hidePanel);
  44. }
  45. }
  46. void Start()
  47. {
  48. originalPos = rectTransform.anchoredPosition3D;
  49. if (showOnAwake)
  50. setShowState();
  51. else
  52. setHideState();
  53. }
  54. public void showPanel()
  55. {
  56. float startAlpha = 0f;
  57. float endAlpha = 1f;
  58. Vector3 startPos = originalPos;
  59. Vector3 endPos = originalPos;
  60. Vector3 startScale = Vector3.one;
  61. Vector3 endScale = Vector3.one;
  62. if (showAnimType == UIAnimType.ZoomIn)
  63. startScale /= zoomAmount;
  64. else if (showAnimType == UIAnimType.ZoomOut)
  65. startScale *= zoomAmount;
  66. else if (showAnimType == UIAnimType.SlideRight)
  67. startPos += Vector3.left * slideAmount;
  68. else if (showAnimType == UIAnimType.SlideLeft)
  69. startPos += Vector3.right * slideAmount;
  70. else if (showAnimType == UIAnimType.SlideUp)
  71. startPos += Vector3.down * slideAmount;
  72. else if (showAnimType == UIAnimType.SlideDown)
  73. startPos += Vector3.up * slideAmount;
  74. LeanTween.cancel(gameObject, true);
  75. canvasGroup.interactable = false;
  76. canvasGroup.blocksRaycasts = true;
  77. canvasGroup.alpha = startAlpha;
  78. rectTransform.anchoredPosition3D = startPos;
  79. transform.localScale = startScale;
  80. LeanTween.value(gameObject, 0f, 1f, time).setOnUpdate((float val)=>{
  81. canvasGroup.alpha = startAlpha + (endAlpha-startAlpha)*fadeCurve.Evaluate(val);
  82. rectTransform.anchoredPosition3D = startPos + (endPos-startPos)*showCurve.Evaluate(val);
  83. transform.localScale = startScale + (endScale-startScale)*showCurve.Evaluate(val);
  84. }).setOnComplete(setShowState);
  85. }
  86. public void hidePanel()
  87. {
  88. float startAlpha = 1f;
  89. float endAlpha = 0f;
  90. Vector3 startPos = originalPos;
  91. Vector3 endPos = originalPos;
  92. Vector3 startScale = Vector3.one;
  93. Vector3 endScale = Vector3.one;
  94. if (hideAnimType == UIAnimType.ZoomIn)
  95. endScale *= zoomAmount;
  96. else if (hideAnimType == UIAnimType.ZoomOut)
  97. endScale /= zoomAmount;
  98. else if (hideAnimType == UIAnimType.SlideRight)
  99. endPos += Vector3.right * slideAmount;
  100. else if (hideAnimType == UIAnimType.SlideLeft)
  101. endPos += Vector3.left * slideAmount;
  102. else if (hideAnimType == UIAnimType.SlideUp)
  103. endPos += Vector3.up * slideAmount;
  104. else if (hideAnimType == UIAnimType.SlideDown)
  105. endPos += Vector3.down * slideAmount;
  106. LeanTween.cancel(gameObject, true);
  107. canvasGroup.interactable = false;
  108. canvasGroup.blocksRaycasts = true;
  109. canvasGroup.alpha = startAlpha;
  110. rectTransform.anchoredPosition3D = startPos;
  111. transform.localScale = startScale;
  112. LeanTween.value(gameObject, 0f, 1f, time).setOnUpdate((float val)=>{
  113. canvasGroup.alpha = startAlpha + (endAlpha-startAlpha)*fadeCurve.Evaluate(val);
  114. rectTransform.anchoredPosition3D = startPos + (endPos-startPos)*hideCurve.Evaluate(val);
  115. transform.localScale = startScale + (endScale-startScale)*hideCurve.Evaluate(val);
  116. }).setOnComplete(setHideState);
  117. }
  118. public void setShowState()
  119. {
  120. canvasGroup.interactable = true;
  121. canvasGroup.blocksRaycasts = true;
  122. canvasGroup.alpha = 1f;
  123. rectTransform.anchoredPosition3D = originalPos;
  124. transform.localScale = Vector3.one;
  125. }
  126. public void setHideState()
  127. {
  128. canvasGroup.interactable = false;
  129. canvasGroup.blocksRaycasts = false;
  130. canvasGroup.alpha = 0f;
  131. }
  132. public void quitApplication()
  133. {
  134. Application.Quit();
  135. }
  136. public void startGame()
  137. {
  138. NotificationServer.notify("show GameUI");
  139. }
  140. }