Last active
October 16, 2024 17:13
-
-
Save abdelfattahradwan/8c8e6633d962429169602e7a6c1b58d9 to your computer and use it in GitHub Desktop.
The View Manager script from the my UI management system YouTube video tutorial (https://youtu.be/rdXC2om16lo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
public class ViewManager : MonoBehaviour | |
{ | |
private static ViewManager s_instance; | |
[SerializeField] private View _startingView; | |
[SerializeField] private View[] _views; | |
private View _currentView; | |
private readonly Stack<View> _history = new Stack<View>(); | |
public static T GetView<T>() where T : View | |
{ | |
for (int i = 0; i < s_instance._views.Length; i++) | |
{ | |
if (s_instance._views[i] is T tView) | |
{ | |
return tView; | |
} | |
} | |
return null; | |
} | |
public static void Show<T>(bool remember = true) where T : View | |
{ | |
for (int i = 0; i < s_instance._views.Length; i++) | |
{ | |
if (s_instance._views[i] is T) | |
{ | |
if (s_instance._currentView != null) | |
{ | |
if (remember) | |
{ | |
s_instance._history.Push(s_instance._currentView); | |
} | |
s_instance._currentView.Hide(); | |
} | |
s_instance._views[i].Show(); | |
s_instance._currentView = s_instance._views[i]; | |
} | |
} | |
} | |
public static void Show(View view, bool remember = true) | |
{ | |
if (s_instance._currentView != null) | |
{ | |
if (remember) | |
{ | |
s_instance._history.Push(s_instance._currentView); | |
} | |
s_instance._currentView.Hide(); | |
} | |
view.Show(); | |
s_instance._currentView = view; | |
} | |
public static void ShowLast() | |
{ | |
if (s_instance._history.Count != 0) | |
{ | |
Show(s_instance._history.Pop(), false); | |
} | |
} | |
private void Awake() => s_instance = this; | |
private void Start() | |
{ | |
for (int i = 0; i < _views.Length; i++) | |
{ | |
_views[i].Initialize(); | |
_views[i].Hide(); | |
} | |
if (_startingView != null) | |
{ | |
Show(_startingView, true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment