1
This commit is contained in:
118
Unity/Scripts/UX/Core/UXFramework.cs
Normal file
118
Unity/Scripts/UX/Core/UXFramework.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using UnityEngine.Events;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public class UXFramework : MonoBehaviour
|
||||
{
|
||||
public static Dictionary<string, UXPanel> panels = new();
|
||||
public static Stack<UXPanel> stacks = new();
|
||||
static UXFramework singleton;
|
||||
static UXPanel current;
|
||||
public static void Enter(string name)
|
||||
{
|
||||
if (panels.TryGetValue(name, out var panel))
|
||||
{
|
||||
Enter(panel);
|
||||
}
|
||||
}
|
||||
public static void Enter<T>()
|
||||
{
|
||||
Enter(typeof(T).Name);
|
||||
}
|
||||
public static void Enter(UXPanel panel)
|
||||
{
|
||||
if (current == panel) return;
|
||||
if (panel.isAdditive is false)
|
||||
{
|
||||
current?.Set(false);
|
||||
stacks.Push(panel);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
panel.Set(true);
|
||||
|
||||
current = panel;
|
||||
|
||||
singleton.history.Add(panel.name);
|
||||
|
||||
BITAppForUnity.AllowCursor.SetElements(singleton, panel.cursor);
|
||||
BITInputSystem.AllowInput.SetElements(singleton, panel.allowInput);
|
||||
}
|
||||
public static void Return()
|
||||
{
|
||||
if (stacks.TryPop(out var panel))
|
||||
{
|
||||
if (panel == current)
|
||||
{
|
||||
stacks.TryPop(out panel);
|
||||
}
|
||||
Enter(panel);
|
||||
}
|
||||
}
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void Reload()
|
||||
{
|
||||
panels.Clear();
|
||||
stacks.Clear();
|
||||
current = null;
|
||||
}
|
||||
[Header(Constant.Header.Settings)]
|
||||
public bool dontDestroyOnLoad;
|
||||
public UXPanel startPanel;
|
||||
public List<string> history = new();
|
||||
void Awake()
|
||||
{
|
||||
if (singleton is null)
|
||||
{
|
||||
singleton = this;
|
||||
if (dontDestroyOnLoad)
|
||||
{
|
||||
GameObject.DontDestroyOnLoad(gameObject);
|
||||
gameObject.transform.parent = BITAppForUnity.GameObject.transform;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
if (singleton == this)
|
||||
{
|
||||
singleton = null;
|
||||
foreach (var x in panels)
|
||||
{
|
||||
x.Value.OnDestroyComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
GetComponentsInChildren<UXPanel>(true).ForEach(x =>
|
||||
{
|
||||
var uxPanelType = typeof(UXPanel);
|
||||
var name = uxPanelType == x.GetType() ? x.gameObject.name : x.GetType().Name;
|
||||
panels.TryAdd(name, x);
|
||||
});
|
||||
foreach (var x in panels)
|
||||
{
|
||||
x.Value.OnStart();
|
||||
x.Value.Set(false);
|
||||
}
|
||||
//await System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(startDelay),gameObject.GetCancellationTokenOnDestroy());
|
||||
if (startPanel)
|
||||
{
|
||||
Enter(startPanel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user