using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIPanel : MonoBehaviour { public enum UIAnimType { Fade, ZoomIn, ZoomOut, SlideRight, SlideLeft, SlideUp, SlideDown } public bool showOnAwake = false; public string overrideNotifyName; public UIAnimType showAnimType = UIAnimType.Fade; public UIAnimType hideAnimType = UIAnimType.Fade; public AnimationCurve fadeCurve; public AnimationCurve showCurve; public AnimationCurve hideCurve; public float time = 0.5f; public float zoomAmount = 1.2f; public float slideAmount = 800f; private RectTransform rectTransform; private CanvasGroup canvasGroup; private Vector3 originalPos; // private bool isShowing = false; void Awake () { rectTransform = GetComponent(); canvasGroup = GetComponent(); if (canvasGroup == null) canvasGroup = gameObject.AddComponent(); if (overrideNotifyName == null || overrideNotifyName == "") { NotificationServer.register("show " + gameObject.name, showPanel); NotificationServer.register("hide " + gameObject.name, hidePanel); } else { NotificationServer.register("show " + overrideNotifyName, showPanel); NotificationServer.register("hide " + overrideNotifyName, hidePanel); } } void Start() { originalPos = rectTransform.anchoredPosition3D; if (showOnAwake) setShowState(); else setHideState(); pauseGame(); } public void showPanel() { // if (isShowing) // return; float startAlpha = 0f; float endAlpha = 1f; Vector3 startPos = originalPos; Vector3 endPos = originalPos; Vector3 startScale = Vector3.one; Vector3 endScale = Vector3.one; if (showAnimType == UIAnimType.ZoomIn) startScale /= zoomAmount; else if (showAnimType == UIAnimType.ZoomOut) startScale *= zoomAmount; else if (showAnimType == UIAnimType.SlideRight) startPos += Vector3.left * slideAmount; else if (showAnimType == UIAnimType.SlideLeft) startPos += Vector3.right * slideAmount; else if (showAnimType == UIAnimType.SlideUp) startPos += Vector3.down * slideAmount; else if (showAnimType == UIAnimType.SlideDown) startPos += Vector3.up * slideAmount; LeanTween.cancel(gameObject, true); canvasGroup.interactable = false; canvasGroup.blocksRaycasts = true; canvasGroup.alpha = startAlpha; rectTransform.anchoredPosition3D = startPos; transform.localScale = startScale; LeanTween.value(gameObject, 0f, 1f, time).setOnUpdate((float val)=>{ canvasGroup.alpha = startAlpha + (endAlpha-startAlpha)*fadeCurve.Evaluate(val); rectTransform.anchoredPosition3D = startPos + (endPos-startPos)*showCurve.Evaluate(val); transform.localScale = startScale + (endScale-startScale)*showCurve.Evaluate(val); }).setOnComplete(setShowState).setIgnoreTimeScale(true); } public void hidePanel() { // if (!isShowing) // return; float startAlpha = 1f; float endAlpha = 0f; Vector3 startPos = originalPos; Vector3 endPos = originalPos; Vector3 startScale = Vector3.one; Vector3 endScale = Vector3.one; if (hideAnimType == UIAnimType.ZoomIn) endScale *= zoomAmount; else if (hideAnimType == UIAnimType.ZoomOut) endScale /= zoomAmount; else if (hideAnimType == UIAnimType.SlideRight) endPos += Vector3.right * slideAmount; else if (hideAnimType == UIAnimType.SlideLeft) endPos += Vector3.left * slideAmount; else if (hideAnimType == UIAnimType.SlideUp) endPos += Vector3.up * slideAmount; else if (hideAnimType == UIAnimType.SlideDown) endPos += Vector3.down * slideAmount; LeanTween.cancel(gameObject, true); canvasGroup.interactable = false; canvasGroup.blocksRaycasts = true; canvasGroup.alpha = startAlpha; rectTransform.anchoredPosition3D = startPos; transform.localScale = startScale; LeanTween.value(gameObject, 0f, 1f, time).setOnUpdate((float val)=>{ canvasGroup.alpha = startAlpha + (endAlpha-startAlpha)*fadeCurve.Evaluate(val); rectTransform.anchoredPosition3D = startPos + (endPos-startPos)*hideCurve.Evaluate(val); transform.localScale = startScale + (endScale-startScale)*hideCurve.Evaluate(val); }).setOnComplete(setHideState).setIgnoreTimeScale(true); } public void setShowState() { canvasGroup.interactable = true; canvasGroup.blocksRaycasts = true; canvasGroup.alpha = 1f; rectTransform.anchoredPosition3D = originalPos; transform.localScale = Vector3.one; // isShowing = true; } public void setHideState() { canvasGroup.interactable = false; canvasGroup.blocksRaycasts = false; canvasGroup.alpha = 0f; // isShowing = false; } public void quitApplication() { Application.Quit(); } public void startGame() { NotificationServer.notify("show GameUI"); NotificationServer.notify("play bgm", "tense:0.75"); unpauseGame(); } public void pauseGame() { Time.timeScale = 0f; } public void unpauseGame() { Time.timeScale = 1f; } }