Skip to content

Added transition time and sample scale menu #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Assets/Prefabs/Menus/OptionsMenu.prefab
Binary file not shown.
38 changes: 22 additions & 16 deletions Assets/Scripts/MenuSystem/Menu.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using UnityEngine;
using UnityEngine;

public abstract class Menu<T> : Menu where T : Menu<T>
{
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<T>();
MenuManager.Instance.CreateInstance<T>();
else
Instance.gameObject.SetActive(true);
Instance.gameObject.SetActive(true);

MenuManager.Instance.OpenMenu(Instance);
MenuManager.Instance.OpenMenu(Instance, Instance.animInTime);
}

protected static void Close()
Expand All @@ -32,7 +32,7 @@ protected static void Close()
return;
}

MenuManager.Instance.CloseMenu(Instance);
MenuManager.Instance.CloseMenu(Instance, Instance.animOutTime);
}

public override void OnBackPressed()
Expand All @@ -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();
}
}
111 changes: 65 additions & 46 deletions Assets/Scripts/MenuSystem/MenuManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

Expand All @@ -18,7 +20,7 @@ private void Awake()
{
Instance = this;

MainMenu.Show();
MainMenu.Show();
}

private void OnDestroy()
Expand All @@ -33,28 +35,36 @@ public void CreateInstance<T>() 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<Canvas>();
var previousCanvas = menuStack.Peek().GetComponent<Canvas>();
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<T>() where T : Menu
Expand All @@ -74,41 +84,50 @@ private T GetPrefab<T>() 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()
Expand Down
82 changes: 82 additions & 0 deletions Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using UnityEngine;

public abstract class SimpleMenuScaleInOut<T> : Menu<T> where T : SimpleMenuScaleInOut<T>
{
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);
}
}
12 changes: 12 additions & 0 deletions Assets/Scripts/MenuSystem/SimpleMenuScaleInOut.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Scripts/Menus/OptionsMenu.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEngine.UI;

public class OptionsMenu : SimpleMenu<OptionsMenu>
public class OptionsMenu : SimpleMenuScaleInOut<OptionsMenu>
{
public Slider Slider;

Expand Down