From 72429bc695f226e200902f97f3651ee215e36760 Mon Sep 17 00:00:00 2001 From: Sylvain Dubrofsky Date: Thu, 31 Aug 2017 10:29:16 -0700 Subject: [PATCH] Added transition time and sample scale menu Adjusted MenuManager and Menu to consider transition in and out time Added a simple lerp scale in and out menu type Adjusted OptionsMenu prefab for scaling --- Assets/Prefabs/Menus/OptionsMenu.prefab | Bin 31484 -> 31980 bytes Assets/Scripts/MenuSystem/Menu.cs | 38 +++--- Assets/Scripts/MenuSystem/MenuManager.cs | 111 ++++++++++-------- .../MenuSystem/SimpleMenuScaleInOut.cs | 82 +++++++++++++ .../MenuSystem/SimpleMenuScaleInOut.cs.meta | 12 ++ Assets/Scripts/Menus/OptionsMenu.cs | 2 +- 6 files changed, 182 insertions(+), 63 deletions(-) create mode 100644 Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs create mode 100644 Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs.meta diff --git a/Assets/Prefabs/Menus/OptionsMenu.prefab b/Assets/Prefabs/Menus/OptionsMenu.prefab index d65f9689d42b0ff67a8cd5424136eae805d286f7..276ebf3d20dfc1b3f8a50a597e70984e20b8af8f 100644 GIT binary patch delta 1798 zcmY+EdrVVT7{I^N+hPT*#g?={TZRrfGF&bVGD00rz}rQNlf0l6rTJ2eiDi70fRrk{|A?Egc%$k$o}W| z__%LZd7INNl73b|I*8m0K!71B0A5be&>ur3S_OMSFifPmsi(U7>K8xjJfM2k{wVxk zdO~ecAvj)2B zsytku&6j(YYnL7Goo-H5oh*B_ZqhNh^!S??>JAE*I``3`#Yz`TWU#CA z-)97=K))$4*3J{(J|DSN2)(FYhT5Ff|Hr(E#6M+&}hmUb|U^M(*ooE$!5%NLG!_NKU z7!<)YGTxyrtD(rbGz^@gkX~y)8}Y0#3?}GT>t!pQVWFAC;Xh8coWdDomBH}y!q$A* zSlC9)vjC)6A65YG3Dote_UAAYNyBufBvXp4z~?{)W< zhAZvFJQpCDfFuCr10@#|;yTEFdIVGQMFAL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jzu!&3=iz$}!t?3;b?9((EE6&ujzC4jlW=dDEh0I*XH0#K5$7P>E%1El@!I!Z55^wB${L_j8&_n|i c+yH2eW?7{=Hc}{IeH9BEAw;$i?BQAE|A{@1WB>pF diff --git a/Assets/Scripts/MenuSystem/Menu.cs b/Assets/Scripts/MenuSystem/Menu.cs index 5a5272c..bc8fb1a 100644 --- a/Assets/Scripts/MenuSystem/Menu.cs +++ b/Assets/Scripts/MenuSystem/Menu.cs @@ -1,27 +1,27 @@ -using UnityEngine; +using UnityEngine; public abstract class Menu : Menu where T : Menu { - public static T Instance { get; private set; } + public static T Instance { get; private set; } - protected virtual void Awake() - { - Instance = (T)this; - } + protected virtual void Awake() + { + Instance = (T)this; + } - protected virtual void OnDestroy() - { - Instance = null; + protected virtual void OnDestroy() + { + Instance = null; } - protected static void Open() + protected static void Open() { if (Instance == null) - MenuManager.Instance.CreateInstance(); + MenuManager.Instance.CreateInstance(); else - Instance.gameObject.SetActive(true); + Instance.gameObject.SetActive(true); - MenuManager.Instance.OpenMenu(Instance); + MenuManager.Instance.OpenMenu(Instance, Instance.animInTime); } protected static void Close() @@ -32,7 +32,7 @@ protected static void Close() return; } - MenuManager.Instance.CloseMenu(Instance); + MenuManager.Instance.CloseMenu(Instance, Instance.animOutTime); } public override void OnBackPressed() @@ -47,7 +47,13 @@ public abstract class Menu : MonoBehaviour public bool DestroyWhenClosed = true; [Tooltip("Disable menus that are under this one in the stack")] - public bool DisableMenusUnderneath = true; + public bool DisableMenusUnderneath = true; + + [Tooltip("Time to animate this menu in")] + public float animInTime = 0f; + + [Tooltip("Time to animate this menu out")] + public float animOutTime = 0f; public abstract void OnBackPressed(); -} +} diff --git a/Assets/Scripts/MenuSystem/MenuManager.cs b/Assets/Scripts/MenuSystem/MenuManager.cs index fa54685..3e149fd 100644 --- a/Assets/Scripts/MenuSystem/MenuManager.cs +++ b/Assets/Scripts/MenuSystem/MenuManager.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections; +using System.Collections.Generic; using System.Reflection; using UnityEngine; @@ -18,7 +20,7 @@ private void Awake() { Instance = this; - MainMenu.Show(); + MainMenu.Show(); } private void OnDestroy() @@ -33,28 +35,36 @@ public void CreateInstance() where T : Menu Instantiate(prefab, transform); } - public void OpenMenu(Menu instance) + public void OpenMenu(Menu instance, float animInTime) { - // De-activate top menu if (menuStack.Count > 0) { - if (instance.DisableMenusUnderneath) - { - foreach (var menu in menuStack) - { - menu.gameObject.SetActive(false); - - if (menu.DisableMenusUnderneath) - break; - } - } - var topCanvas = instance.GetComponent(); var previousCanvas = menuStack.Peek().GetComponent(); - topCanvas.sortingOrder = previousCanvas.sortingOrder + 1; + topCanvas.sortingOrder = previousCanvas.sortingOrder + 1; } - menuStack.Push(instance); + StartCoroutine(DeactivateMenusUnder(instance, animInTime)); + } + + private IEnumerator DeactivateMenusUnder(Menu instance, float waitTime) + { + yield return new WaitForSeconds(waitTime); + + //De-activate previous top menu + if (instance.DisableMenusUnderneath) + { + foreach (var menu in menuStack) + { + if (menu == instance) + continue; + + menu.gameObject.SetActive(false); + + if (menu.DisableMenusUnderneath) + break; + } + } } private T GetPrefab() where T : Menu @@ -74,41 +84,50 @@ private T GetPrefab() where T : Menu throw new MissingReferenceException("Prefab not found for type " + typeof(T)); } - public void CloseMenu(Menu menu) - { - if (menuStack.Count == 0) - { - Debug.LogErrorFormat(menu, "{0} cannot be closed because menu stack is empty", menu.GetType()); - return; - } - - if (menuStack.Peek() != menu) - { - Debug.LogErrorFormat(menu, "{0} cannot be closed because it is not on top of stack", menu.GetType()); - return; - } - - CloseTopMenu(); - } + public void CloseMenu(Menu topMenu, float animOutTime) + { + if (menuStack.Count == 0) + { + Debug.LogErrorFormat(topMenu, "{0} cannot be closed because menu stack is empty", topMenu.GetType()); + return; + } + + if (menuStack.Peek() != topMenu) + { + Debug.LogErrorFormat(topMenu, "{0} cannot be closed because it is not on top of stack", topMenu.GetType()); + return; + } + + menuStack.Pop(); + + StartCoroutine(CloseTopMenu(topMenu, animOutTime)); - public void CloseTopMenu() + ReactivateOldMenu(); + } + + private IEnumerator CloseTopMenu(Menu instance, float waitTime) { - var instance = menuStack.Pop(); + yield return new WaitForSeconds(waitTime); - if (instance.DestroyWhenClosed) - Destroy(instance.gameObject); - else - instance.gameObject.SetActive(false); + if (instance == null) + yield break; + + if (instance.DestroyWhenClosed) + Destroy(instance.gameObject); + else + instance.gameObject.SetActive(false); + } - // Re-activate top menu - // If a re-activated menu is an overlay we need to activate the menu under it - foreach (var menu in menuStack) - { + private void ReactivateOldMenu() + { + // If a re-activated menu is an overlay we need to activate the menu under it + foreach (var menu in menuStack) + { menu.gameObject.SetActive(true); - if (menu.DisableMenusUnderneath) - break; - } + if (menu.DisableMenusUnderneath) + break; + } } private void Update() diff --git a/Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs b/Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs new file mode 100644 index 0000000..49c2841 --- /dev/null +++ b/Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs @@ -0,0 +1,82 @@ +using UnityEngine; + +public abstract class SimpleMenuScaleInOut : Menu where T : SimpleMenuScaleInOut +{ + readonly Vector3 START_SCALE = Vector3.one * .25f; + + private bool animatingIn = false; + private float currentAnimInTime = 0; + private bool animatingOut = false; + private float currentAnimOutTime = 0; + + // Prefab should have one child to scale + private Transform visualRoot; + + protected override void Awake() + { + base.Awake(); + + visualRoot = transform.GetChild(0); + visualRoot.localScale = START_SCALE; + } + + public static void Show() + { + Open(); + + if (Instance.animInTime > 0) + { + Instance.animatingIn = true; + Instance.currentAnimInTime = 0; + } + } + + public static void Hide() + { + Close(); + + if (Instance.animOutTime > 0) + { + Instance.animatingOut = true; + Instance.currentAnimOutTime = 0; + } + } + + public override void OnBackPressed() + { + Close(); + + if (Instance.animOutTime > 0) + { + Instance.animatingOut = true; + Instance.currentAnimOutTime = 0; + } + } + + private void Update() + { + if (animatingIn) + { + ScaleVisuals(ref Instance.animatingIn, ref currentAnimInTime, animInTime, START_SCALE, Vector3.one); + } + + if (animatingOut) + { + ScaleVisuals(ref Instance.animatingOut, ref currentAnimOutTime, animOutTime, Vector3.one, START_SCALE); + } + } + + private void ScaleVisuals(ref bool isAnimating, ref float curAnimTime, float totalAnimTime, Vector3 startScale, Vector3 endScale) + { + curAnimTime += Time.deltaTime; + if (curAnimTime >= totalAnimTime) + { + curAnimTime = totalAnimTime; + isAnimating = false; + } + + float percent = curAnimTime / totalAnimTime; + + visualRoot.localScale = Vector3.Lerp(startScale, endScale, percent); + } +} diff --git a/Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs.meta b/Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs.meta new file mode 100644 index 0000000..0e90d59 --- /dev/null +++ b/Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 2320601fd68b30244b9c8dea62ad7aab +timeCreated: 1504133741 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Menus/OptionsMenu.cs b/Assets/Scripts/Menus/OptionsMenu.cs index 9182e45..bab3249 100644 --- a/Assets/Scripts/Menus/OptionsMenu.cs +++ b/Assets/Scripts/Menus/OptionsMenu.cs @@ -1,6 +1,6 @@ using UnityEngine.UI; -public class OptionsMenu : SimpleMenu +public class OptionsMenu : SimpleMenuScaleInOut { public Slider Slider;