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.

177 lines
4.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
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. // private bool isShowing = false;
  30. void Awake ()
  31. {
  32. rectTransform = GetComponent<RectTransform>();
  33. canvasGroup = GetComponent<CanvasGroup>();
  34. if (canvasGroup == null)
  35. canvasGroup = gameObject.AddComponent<CanvasGroup>();
  36. if (overrideNotifyName == null || overrideNotifyName == "")
  37. {
  38. NotificationServer.register("show " + gameObject.name, showPanel);
  39. NotificationServer.register("hide " + gameObject.name, hidePanel);
  40. }
  41. else
  42. {
  43. NotificationServer.register("show " + overrideNotifyName, showPanel);
  44. NotificationServer.register("hide " + overrideNotifyName, hidePanel);
  45. }
  46. }
  47. void Start()
  48. {
  49. originalPos = rectTransform.anchoredPosition3D;
  50. if (showOnAwake)
  51. setShowState();
  52. else
  53. setHideState();
  54. pauseGame();
  55. }
  56. public void showPanel()
  57. {
  58. // if (isShowing)
  59. // return;
  60. float startAlpha = 0f;
  61. float endAlpha = 1f;
  62. Vector3 startPos = originalPos;
  63. Vector3 endPos = originalPos;
  64. Vector3 startScale = Vector3.one;
  65. Vector3 endScale = Vector3.one;
  66. if (showAnimType == UIAnimType.ZoomIn)
  67. startScale /= zoomAmount;
  68. else if (showAnimType == UIAnimType.ZoomOut)
  69. startScale *= zoomAmount;
  70. else if (showAnimType == UIAnimType.SlideRight)
  71. startPos += Vector3.left * slideAmount;
  72. else if (showAnimType == UIAnimType.SlideLeft)
  73. startPos += Vector3.right * slideAmount;
  74. else if (showAnimType == UIAnimType.SlideUp)
  75. startPos += Vector3.down * slideAmount;
  76. else if (showAnimType == UIAnimType.SlideDown)
  77. startPos += Vector3.up * slideAmount;
  78. LeanTween.cancel(gameObject, true);
  79. canvasGroup.interactable = false;
  80. canvasGroup.blocksRaycasts = true;
  81. canvasGroup.alpha = startAlpha;
  82. rectTransform.anchoredPosition3D = startPos;
  83. transform.localScale = startScale;
  84. LeanTween.value(gameObject, 0f, 1f, time).setOnUpdate((float val)=>{
  85. canvasGroup.alpha = startAlpha + (endAlpha-startAlpha)*fadeCurve.Evaluate(val);
  86. rectTransform.anchoredPosition3D = startPos + (endPos-startPos)*showCurve.Evaluate(val);
  87. transform.localScale = startScale + (endScale-startScale)*showCurve.Evaluate(val);
  88. }).setOnComplete(setShowState).setIgnoreTimeScale(true);
  89. }
  90. public void hidePanel()
  91. {
  92. // if (!isShowing)
  93. // return;
  94. float startAlpha = 1f;
  95. float endAlpha = 0f;
  96. Vector3 startPos = originalPos;
  97. Vector3 endPos = originalPos;
  98. Vector3 startScale = Vector3.one;
  99. Vector3 endScale = Vector3.one;
  100. if (hideAnimType == UIAnimType.ZoomIn)
  101. endScale *= zoomAmount;
  102. else if (hideAnimType == UIAnimType.ZoomOut)
  103. endScale /= zoomAmount;
  104. else if (hideAnimType == UIAnimType.SlideRight)
  105. endPos += Vector3.right * slideAmount;
  106. else if (hideAnimType == UIAnimType.SlideLeft)
  107. endPos += Vector3.left * slideAmount;
  108. else if (hideAnimType == UIAnimType.SlideUp)
  109. endPos += Vector3.up * slideAmount;
  110. else if (hideAnimType == UIAnimType.SlideDown)
  111. endPos += Vector3.down * slideAmount;
  112. LeanTween.cancel(gameObject, true);
  113. canvasGroup.interactable = false;
  114. canvasGroup.blocksRaycasts = true;
  115. canvasGroup.alpha = startAlpha;
  116. rectTransform.anchoredPosition3D = startPos;
  117. transform.localScale = startScale;
  118. LeanTween.value(gameObject, 0f, 1f, time).setOnUpdate((float val)=>{
  119. canvasGroup.alpha = startAlpha + (endAlpha-startAlpha)*fadeCurve.Evaluate(val);
  120. rectTransform.anchoredPosition3D = startPos + (endPos-startPos)*hideCurve.Evaluate(val);
  121. transform.localScale = startScale + (endScale-startScale)*hideCurve.Evaluate(val);
  122. }).setOnComplete(setHideState).setIgnoreTimeScale(true);
  123. }
  124. public void setShowState()
  125. {
  126. canvasGroup.interactable = true;
  127. canvasGroup.blocksRaycasts = true;
  128. canvasGroup.alpha = 1f;
  129. rectTransform.anchoredPosition3D = originalPos;
  130. transform.localScale = Vector3.one;
  131. // isShowing = true;
  132. }
  133. public void setHideState()
  134. {
  135. canvasGroup.interactable = false;
  136. canvasGroup.blocksRaycasts = false;
  137. canvasGroup.alpha = 0f;
  138. // isShowing = false;
  139. }
  140. public void quitApplication()
  141. {
  142. Application.Quit();
  143. }
  144. public void startGame()
  145. {
  146. NotificationServer.notify("show GameUI");
  147. unpauseGame();
  148. }
  149. public void pauseGame()
  150. {
  151. Time.timeScale = 0f;
  152. }
  153. public void unpauseGame()
  154. {
  155. Time.timeScale = 1f;
  156. }
  157. }