UnityExplorer/src/UI/UIManager.cs

183 lines
5.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityExplorer.Core.Config;
using UnityExplorer.Core.Input;
using UnityExplorer.UI.Models;
using UnityExplorer.UI.Panels;
using UnityExplorer.UI.Utility;
using UnityExplorer.UI.Widgets;
namespace UnityExplorer.UI
{
public static class UIManager
{
public static GameObject CanvasRoot { get; private set; }
public static EventSystem EventSys { get; private set; }
// panels
public static SceneExplorer SceneExplorer { get; private set; }
public static InspectorTest Inspector { get; private set; }
// bundle assets
internal static Font ConsoleFont { get; private set; }
internal static Shader BackupShader { get; private set; }
2021-03-11 17:57:58 +11:00
public static bool ShowMenu
{
get => s_showMenu;
set
2021-03-11 17:57:58 +11:00
{
if (s_showMenu == value || !CanvasRoot)
return;
s_showMenu = value;
CanvasRoot.SetActive(value);
CursorUnlocker.UpdateCursorControl();
2021-03-11 17:57:58 +11:00
}
}
public static bool s_showMenu = true;
2021-03-11 17:57:58 +11:00
internal static void InitUI()
{
LoadBundle();
UIFactory.Init();
CreateRootCanvas();
SceneExplorer = new SceneExplorer();
SceneExplorer.ConstructUI(CanvasRoot);
Inspector = new InspectorTest();
Inspector.ConstructUI(CanvasRoot);
//MainMenu.Create();
//InspectUnderMouse.ConstructUI();
//PanelDragger.CreateCursorUI();
// Force refresh of anchors etc
Canvas.ForceUpdateCanvases();
ShowMenu = !ConfigManager.Hide_On_Startup.Value;
ExplorerCore.Log("UI initialized.");
}
public static void Update()
2021-03-11 17:57:58 +11:00
{
if (!CanvasRoot)
2021-03-11 17:57:58 +11:00
return;
//if (InspectUnderMouse.Inspecting)
//{
// InspectUnderMouse.UpdateInspect();
// return;
//}
2021-03-11 17:57:58 +11:00
if (InputManager.GetKeyDown(ConfigManager.Main_Menu_Toggle.Value))
2021-03-11 17:57:58 +11:00
ShowMenu = !ShowMenu;
if (!ShowMenu)
2021-03-11 17:57:58 +11:00
return;
if (InputManager.GetKeyDown(ConfigManager.Force_Unlock_Keybind.Value))
CursorUnlocker.Unlock = !CursorUnlocker.Unlock;
UIPanel.UpdateFocus();
UIBehaviourModel.UpdateInstances();
if (EventSystem.current != EventSys)
CursorUnlocker.SetEventSystem();
// TODO could make these UIBehaviourModels
PanelDragger.UpdateInstances();
SliderScrollbar.UpdateInstances();
InputFieldScroller.UpdateInstances();
}
private static void CreateRootCanvas()
{
CanvasRoot = new GameObject("ExplorerCanvas");
UnityEngine.Object.DontDestroyOnLoad(CanvasRoot);
CanvasRoot.hideFlags |= HideFlags.HideAndDontSave;
CanvasRoot.layer = 5;
CanvasRoot.transform.position = new Vector3(0f, 0f, 1f);
EventSys = CanvasRoot.AddComponent<EventSystem>();
InputManager.AddUIModule();
Canvas canvas = CanvasRoot.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceCamera;
canvas.referencePixelsPerUnit = 100;
canvas.sortingOrder = 999;
CanvasScaler scaler = CanvasRoot.AddComponent<CanvasScaler>();
scaler.referenceResolution = new Vector2(1920, 1080);
scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;
CanvasRoot.AddComponent<GraphicRaycaster>();
}
2021-01-02 19:38:01 +01:00
private static void LoadBundle()
{
AssetBundle bundle = null;
try
{
bundle = LoadBundle("modern");
if (bundle == null)
bundle = LoadBundle("legacy");
2021-01-02 19:38:01 +01:00
}
catch { }
2021-01-02 19:38:01 +01:00
if (bundle == null)
{
ExplorerCore.LogWarning("Could not load the ExplorerUI Bundle!");
ConsoleFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
2021-01-02 19:38:01 +01:00
return;
}
2021-01-02 19:38:01 +01:00
BackupShader = bundle.LoadAsset<Shader>("DefaultUI");
2021-01-02 19:38:01 +01:00
// Fix for games which don't ship with 'UI/Default' shader.
if (Graphic.defaultGraphicMaterial.shader?.name != "UI/Default")
{
2021-01-02 19:38:01 +01:00
ExplorerCore.Log("This game does not ship with the 'UI/Default' shader, using manual Default Shader...");
Graphic.defaultGraphicMaterial.shader = BackupShader;
}
2021-04-05 16:28:30 +10:00
else
BackupShader = Graphic.defaultGraphicMaterial.shader;
2021-01-02 19:38:01 +01:00
ConsoleFont = bundle.LoadAsset<Font>("CONSOLA");
ExplorerCore.Log("Loaded UI AssetBundle");
}
private static AssetBundle LoadBundle(string id)
{
var stream = typeof(ExplorerCore).Assembly
2021-04-05 16:28:30 +10:00
.GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle");
return AssetBundle.LoadFromMemory(ReadFully(stream));
}
private static byte[] ReadFully(Stream input)
{
using (var ms = new MemoryStream())
{
byte[] buffer = new byte[81920];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) != 0)
ms.Write(buffer, 0, read);
return ms.ToArray();
}
}
}
}