2020-11-05 17:33:04 +11:00
|
|
|
|
using UnityEngine;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityEngine.UI;
|
2020-11-05 17:33:04 +11:00
|
|
|
|
using UnityExplorer.Inspectors;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
using UnityExplorer.UI.Modules;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using UnityExplorer.Helpers;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
using UnityExplorer.UI.Shared;
|
2020-11-20 17:12:40 +11:00
|
|
|
|
using UnityExplorer.Input;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
#if CPP
|
2020-11-09 16:43:19 +11:00
|
|
|
|
using UnityExplorer.Unstrip;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
#endif
|
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
|
|
|
|
|
2020-11-25 16:40:36 +11:00
|
|
|
|
internal static Sprite ResizeCursor { get; private set; }
|
|
|
|
|
internal static Shader BackupShader { get; private set; }
|
|
|
|
|
|
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();
|
2020-11-06 20:42:16 +11:00
|
|
|
|
MouseInspector.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
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
public static void OnSceneChange()
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
SceneExplorer.Instance?.OnSceneChange();
|
|
|
|
|
SearchPage.Instance?.OnSceneChange();
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
public static void Update()
|
|
|
|
|
{
|
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)
|
|
|
|
|
ForceUnlockCursor.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-03 19:27:02 +11:00
|
|
|
|
private static AssetBundle LoadExplorerUi(string id)
|
|
|
|
|
{
|
|
|
|
|
return AssetBundle.LoadFromMemory(ReadFully(typeof(ExplorerCore).Assembly.GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle")));
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
private static byte[] ReadFully(this Stream input)
|
2020-11-20 17:12:40 +11:00
|
|
|
|
{
|
2021-01-02 19:38:01 +01:00
|
|
|
|
using (var ms = new MemoryStream())
|
2020-11-20 17:12:40 +11:00
|
|
|
|
{
|
2021-01-03 19:27:02 +11:00
|
|
|
|
byte[] buffer = new byte[81920];
|
|
|
|
|
int read;
|
|
|
|
|
while ((read = input.Read(buffer, 0, buffer.Length)) != 0)
|
|
|
|
|
ms.Write(buffer, 0, read);
|
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void LoadBundle()
|
|
|
|
|
{
|
|
|
|
|
AssetBundle bundle = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bundle = LoadExplorerUi("modern");
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.Log("Failed to load modern 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!");
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
ResizeCursor = bundle.LoadAsset<Sprite>("cursor");
|
|
|
|
|
|
|
|
|
|
ConsoleFont = bundle.LoadAsset<Font>("CONSOLA");
|
|
|
|
|
|
|
|
|
|
ExplorerCore.Log("Loaded UI bundle");
|
2020-11-20 17:12:40 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
private static GameObject CreateRootCanvas()
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
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>();
|
2020-11-20 17:12:40 +11:00
|
|
|
|
InputManager.AddUIModule();
|
2020-11-08 21:04:41 +11:00
|
|
|
|
|
|
|
|
|
Canvas canvas = rootObj.AddComponent<Canvas>();
|
|
|
|
|
canvas.renderMode = RenderMode.ScreenSpaceCamera;
|
|
|
|
|
canvas.referencePixelsPerUnit = 100;
|
|
|
|
|
canvas.sortingOrder = 999;
|
2021-01-14 17:46:32 +11:00
|
|
|
|
//canvas.pixelPerfect = false;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
|
|
|
|
|
CanvasScaler scaler = rootObj.AddComponent<CanvasScaler>();
|
|
|
|
|
scaler.referenceResolution = new Vector2(1920, 1080);
|
|
|
|
|
scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;
|
|
|
|
|
|
|
|
|
|
rootObj.AddComponent<GraphicRaycaster>();
|
|
|
|
|
|
|
|
|
|
return rootObj;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|