2020-11-05 17:33:04 +11:00
|
|
|
|
using UnityEngine;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityEngine.UI;
|
2021-03-18 17:17:29 +11:00
|
|
|
|
using UnityExplorer.Core.Inspectors;
|
|
|
|
|
using UnityExplorer.UI.Main;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
2021-03-18 17:17:29 +11:00
|
|
|
|
using UnityExplorer.UI.Reusable;
|
|
|
|
|
using UnityExplorer.Core.Input;
|
2021-03-11 17:57:58 +11:00
|
|
|
|
using System;
|
2021-03-18 17:17:29 +11:00
|
|
|
|
using UnityExplorer.Core.Config;
|
|
|
|
|
using UnityExplorer.UI.Utility;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-03 20:59:13 +11:00
|
|
|
|
namespace UnityExplorer.UI
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
|
|
|
|
public static class UIManager
|
|
|
|
|
{
|
|
|
|
|
public static GameObject CanvasRoot { get; private set; }
|
|
|
|
|
public static EventSystem EventSys { get; private set; }
|
|
|
|
|
|
2020-11-10 20:18:14 +11:00
|
|
|
|
internal static Font ConsoleFont { get; private set; }
|
2020-11-08 21:04:41 +11:00
|
|
|
|
|
2021-03-26 19:49:53 +11:00
|
|
|
|
//internal static Sprite ResizeCursor { get; private set; }
|
2020-11-25 16:40:36 +11:00
|
|
|
|
internal static Shader BackupShader { get; private set; }
|
|
|
|
|
|
2021-03-11 17:57:58 +11:00
|
|
|
|
public static bool ShowMenu
|
|
|
|
|
{
|
|
|
|
|
get => s_showMenu;
|
|
|
|
|
set => SetShowMenu(value);
|
|
|
|
|
}
|
|
|
|
|
public static bool s_showMenu;
|
|
|
|
|
|
|
|
|
|
private static bool s_doneUIInit;
|
|
|
|
|
private static float s_timeSinceStartup;
|
|
|
|
|
|
|
|
|
|
internal static void CheckUIInit()
|
|
|
|
|
{
|
|
|
|
|
if (s_doneUIInit)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
s_timeSinceStartup += Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
if (s_timeSinceStartup > 0.1f)
|
|
|
|
|
{
|
|
|
|
|
s_doneUIInit = true;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
ExplorerCore.Log("Initialized UnityExplorer UI.");
|
|
|
|
|
|
|
|
|
|
if (ExplorerConfig.Instance.Hide_On_Startup)
|
|
|
|
|
ShowMenu = false;
|
|
|
|
|
|
|
|
|
|
// InspectorManager.Instance.Inspect(Tests.TestClass.Instance);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning($"Exception setting up UI: {e}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 01:50:33 +11:00
|
|
|
|
public static void Init()
|
|
|
|
|
{
|
2020-11-20 17:12:40 +11:00
|
|
|
|
LoadBundle();
|
2020-11-08 21:04:41 +11:00
|
|
|
|
|
2020-10-23 01:50:33 +11:00
|
|
|
|
// Create core UI Canvas and Event System handler
|
|
|
|
|
CreateRootCanvas();
|
|
|
|
|
|
|
|
|
|
// Create submodules
|
|
|
|
|
new MainMenu();
|
2021-03-18 17:17:29 +11:00
|
|
|
|
InspectUnderMouse.UI.ConstructUI();
|
2020-11-13 18:46:36 +11:00
|
|
|
|
PanelDragger.LoadCursorImage();
|
2020-11-06 20:42:16 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
// Force refresh of anchors
|
2020-10-24 20:18:42 +11:00
|
|
|
|
Canvas.ForceUpdateCanvases();
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 19:49:53 +11:00
|
|
|
|
private static GameObject CreateRootCanvas()
|
|
|
|
|
{
|
|
|
|
|
GameObject rootObj = new GameObject("ExplorerCanvas");
|
|
|
|
|
UnityEngine.Object.DontDestroyOnLoad(rootObj);
|
|
|
|
|
rootObj.layer = 5;
|
|
|
|
|
|
|
|
|
|
CanvasRoot = rootObj;
|
|
|
|
|
CanvasRoot.transform.position = new Vector3(0f, 0f, 1f);
|
|
|
|
|
|
|
|
|
|
EventSys = rootObj.AddComponent<EventSystem>();
|
|
|
|
|
InputManager.AddUIModule();
|
|
|
|
|
|
|
|
|
|
Canvas canvas = rootObj.AddComponent<Canvas>();
|
|
|
|
|
canvas.renderMode = RenderMode.ScreenSpaceCamera;
|
|
|
|
|
canvas.referencePixelsPerUnit = 100;
|
|
|
|
|
canvas.sortingOrder = 999;
|
|
|
|
|
//canvas.pixelPerfect = false;
|
|
|
|
|
|
|
|
|
|
CanvasScaler scaler = rootObj.AddComponent<CanvasScaler>();
|
|
|
|
|
scaler.referenceResolution = new Vector2(1920, 1080);
|
|
|
|
|
scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;
|
|
|
|
|
|
|
|
|
|
rootObj.AddComponent<GraphicRaycaster>();
|
|
|
|
|
|
|
|
|
|
return rootObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 17:57:58 +11:00
|
|
|
|
private static void SetShowMenu(bool show)
|
|
|
|
|
{
|
|
|
|
|
if (s_showMenu == show)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
s_showMenu = show;
|
|
|
|
|
|
|
|
|
|
if (CanvasRoot)
|
|
|
|
|
{
|
|
|
|
|
CanvasRoot.SetActive(show);
|
|
|
|
|
|
|
|
|
|
if (show)
|
2021-03-18 17:17:29 +11:00
|
|
|
|
CursorUnlocker.SetEventSystem();
|
2021-03-11 17:57:58 +11:00
|
|
|
|
else
|
2021-03-18 17:17:29 +11:00
|
|
|
|
CursorUnlocker.ReleaseEventSystem();
|
2021-03-11 17:57:58 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 17:17:29 +11:00
|
|
|
|
CursorUnlocker.UpdateCursorControl();
|
2021-03-11 17:57:58 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 01:50:33 +11:00
|
|
|
|
public static void Update()
|
|
|
|
|
{
|
2021-03-11 17:57:58 +11:00
|
|
|
|
if (InputManager.GetKeyDown(ExplorerConfig.Instance.Main_Menu_Toggle))
|
|
|
|
|
ShowMenu = !ShowMenu;
|
|
|
|
|
|
|
|
|
|
if (!ShowMenu || !s_doneUIInit || !CanvasRoot)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-10-25 20:57:34 +11:00
|
|
|
|
MainMenu.Instance?.Update();
|
|
|
|
|
|
2020-11-20 17:12:40 +11:00
|
|
|
|
if (EventSys)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
|
|
|
|
if (EventSystem.current != EventSys)
|
2021-03-18 17:17:29 +11:00
|
|
|
|
CursorUnlocker.SetEventSystem();
|
2020-10-27 00:54:08 +11:00
|
|
|
|
#if CPP
|
2020-12-16 14:28:54 +11:00
|
|
|
|
// Some IL2CPP games behave weird with multiple UI Input Systems, some fixes for them.
|
2020-11-20 17:12:40 +11:00
|
|
|
|
var evt = InputManager.InputPointerEvent;
|
|
|
|
|
if (evt != null)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-12-16 14:28:54 +11:00
|
|
|
|
if (!evt.eligibleForClick && evt.selectedObject)
|
|
|
|
|
evt.eligibleForClick = true;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
2020-10-27 00:54:08 +11:00
|
|
|
|
#endif
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PanelDragger.Instance != null)
|
|
|
|
|
PanelDragger.Instance.Update();
|
2020-11-06 20:42:16 +11:00
|
|
|
|
|
2020-11-10 20:18:14 +11:00
|
|
|
|
for (int i = 0; i < SliderScrollbar.Instances.Count; i++)
|
2020-11-06 20:42:16 +11:00
|
|
|
|
{
|
2020-11-10 20:18:14 +11:00
|
|
|
|
var slider = SliderScrollbar.Instances[i];
|
|
|
|
|
|
|
|
|
|
if (slider.CheckDestroyed())
|
|
|
|
|
i--;
|
|
|
|
|
else
|
2020-11-06 20:42:16 +11:00
|
|
|
|
slider.Update();
|
|
|
|
|
}
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < InputFieldScroller.Instances.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var input = InputFieldScroller.Instances[i];
|
|
|
|
|
|
2021-01-22 21:56:00 +11:00
|
|
|
|
if (input.CheckDestroyed())
|
2020-11-11 20:16:43 +11:00
|
|
|
|
i--;
|
|
|
|
|
else
|
|
|
|
|
input.Update();
|
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
private static void LoadBundle()
|
|
|
|
|
{
|
|
|
|
|
AssetBundle bundle = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bundle = LoadExplorerUi("modern");
|
2021-03-26 19:49:53 +11:00
|
|
|
|
if (bundle == null)
|
|
|
|
|
throw new Exception();
|
2021-01-02 19:38:01 +01:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2021-03-26 19:49:53 +11:00
|
|
|
|
ExplorerCore.Log("Failed to load Unity 2017 ExplorerUI Bundle, falling back to legacy");
|
2020-11-25 16:40:36 +11:00
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
try
|
2020-11-20 17:12:40 +11:00
|
|
|
|
{
|
2021-01-02 19:38:01 +01:00
|
|
|
|
bundle = LoadExplorerUi("legacy");
|
2020-11-20 17:12:40 +11:00
|
|
|
|
}
|
2021-01-02 19:38:01 +01:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
if (bundle == null)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning("Could not load the ExplorerUI Bundle!");
|
2021-03-26 19:49:53 +11:00
|
|
|
|
ConsoleFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
2021-01-02 19:38:01 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
BackupShader = bundle.LoadAsset<Shader>("DefaultUI");
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
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")
|
2020-11-20 17:12:40 +11:00
|
|
|
|
{
|
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;
|
2020-11-20 17:12:40 +11:00
|
|
|
|
}
|
2021-01-02 19:38:01 +01:00
|
|
|
|
|
2021-03-26 19:49:53 +11:00
|
|
|
|
//ResizeCursor = bundle.LoadAsset<Sprite>("cursor");
|
2021-01-02 19:38:01 +01:00
|
|
|
|
|
|
|
|
|
ConsoleFont = bundle.LoadAsset<Font>("CONSOLA");
|
|
|
|
|
|
|
|
|
|
ExplorerCore.Log("Loaded UI bundle");
|
2020-11-20 17:12:40 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 19:49:53 +11:00
|
|
|
|
private static AssetBundle LoadExplorerUi(string id)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2021-03-26 19:49:53 +11:00
|
|
|
|
var data = ReadFully(typeof(ExplorerCore).Assembly.GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle"));
|
|
|
|
|
return AssetBundle.LoadFromMemory(data);
|
|
|
|
|
}
|
2020-11-08 21:04:41 +11:00
|
|
|
|
|
2021-03-26 19:49:53 +11:00
|
|
|
|
private static byte[] ReadFully(this 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();
|
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|