UnityExplorer/src/ExplorerBehaviour.cs

72 lines
1.6 KiB
C#
Raw Normal View History

2022-05-26 06:19:50 +10:00
using UnityExplorer.UI;
#if CPP
2022-06-23 06:03:58 +10:00
#if UNHOLLOWER
using UnhollowerRuntimeLib;
2022-06-23 06:03:58 +10:00
#else
using Il2CppInterop.Runtime.Injection;
#endif
#endif
namespace UnityExplorer
{
public class ExplorerBehaviour : MonoBehaviour
{
internal static ExplorerBehaviour Instance { get; private set; }
2022-04-09 18:58:56 +10:00
#if CPP
public ExplorerBehaviour(System.IntPtr ptr) : base(ptr) { }
2022-04-09 18:58:56 +10:00
#endif
internal static void Setup()
{
#if CPP
ClassInjector.RegisterTypeInIl2Cpp<ExplorerBehaviour>();
#endif
2022-04-09 18:58:56 +10:00
GameObject obj = new("ExplorerBehaviour");
DontDestroyOnLoad(obj);
obj.hideFlags = HideFlags.HideAndDontSave;
Instance = obj.AddComponent<ExplorerBehaviour>();
}
internal void Update()
{
ExplorerCore.Update();
}
2022-04-24 05:56:47 +10:00
// For editor, to clean up objects
internal void OnDestroy()
{
OnApplicationQuit();
}
internal bool quitting;
internal void OnApplicationQuit()
{
if (quitting) return;
quitting = true;
TryDestroy(UIManager.UIRoot?.transform.root.gameObject);
TryDestroy((typeof(Universe).Assembly.GetType("UniverseLib.UniversalBehaviour")
.GetProperty("Instance", BindingFlags.Static | BindingFlags.NonPublic)
.GetValue(null, null)
as Component).gameObject);
TryDestroy(this.gameObject);
}
internal void TryDestroy(GameObject obj)
{
try
{
if (obj)
Destroy(obj);
}
catch { }
}
}
}