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-08 21:04:41 +11:00
using UnityExplorer.UI.PageModel ;
using System.IO ;
using TMPro ;
using System.Reflection ;
using UnityExplorer.Helpers ;
#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 ; }
public static StandaloneInputModule InputModule { get ; private set ; }
2020-11-08 21:04:41 +11:00
public static TMP_FontAsset ConsoleFont { get ; private set ; }
2020-10-23 01:50:33 +11:00
public static void Init ( )
{
2020-11-08 21:04:41 +11:00
var bundlePath = ExplorerCore . EXPLORER_FOLDER + @"\tmp.bundle" ;
if ( File . Exists ( bundlePath ) )
{
var bundle = AssetBundle . LoadFromFile ( bundlePath ) ;
bundle . LoadAllAssets ( ) ;
ExplorerCore . Log ( "Loaded TMP bundle" ) ;
if ( TMP_Settings . instance = = null )
{
var settings = bundle . LoadAsset < TMP_Settings > ( "TMP Settings" ) ;
#if MONO
typeof ( TMP_Settings )
. GetField ( "s_Instance" , ReflectionHelpers . CommonFlags )
. SetValue ( null , settings ) ;
#else
TMP_Settings . s_Instance = settings ;
#endif
}
ConsoleFont = bundle . LoadAsset < TMP_FontAsset > ( "CONSOLA SDF" ) ;
}
else if ( TMP_Settings . instance = = null )
{
ExplorerCore . LogWarning ( @"This game does not seem to have the TMP Resources package, and the TMP AssetBundle was not found at 'Mods\UnityExplorer\tmp.bundle\'!" ) ;
return ;
}
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-10-28 06:39:26 +11:00
// Force refresh of anchors
2020-10-24 20:18:42 +11:00
Canvas . ForceUpdateCanvases ( ) ;
CanvasRoot . SetActive ( false ) ;
CanvasRoot . SetActive ( true ) ;
2020-10-23 01:50:33 +11:00
}
public static void SetEventSystem ( )
{
EventSystem . current = EventSys ;
InputModule . ActivateModule ( ) ;
}
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-10-23 01:50:33 +11:00
if ( EventSys & & InputModule )
{
if ( EventSystem . current ! = EventSys )
{
ForceUnlockCursor . SetEventSystem ( ) ;
//ForceUnlockCursor.Unlock = true;
}
// Fix for games which override the InputModule pointer events (eg, VRChat)
2020-10-27 00:54:08 +11:00
#if CPP
2020-10-23 01:50:33 +11:00
if ( InputModule . m_InputPointerEvent ! = null )
{
2020-10-28 06:39:26 +11:00
PointerEventData evt = InputModule . m_InputPointerEvent ;
2020-10-23 01:50:33 +11:00
if ( ! evt . eligibleForClick & & evt . selectedObject )
{
evt . eligibleForClick = true ;
}
}
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
foreach ( var slider in SliderScrollbar . Instances )
{
if ( slider . m_slider . gameObject . activeInHierarchy )
{
slider . Update ( ) ;
}
}
2020-10-23 01:50:33 +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 > ( ) ;
InputModule = rootObj . AddComponent < StandaloneInputModule > ( ) ;
InputModule . ActivateModule ( ) ;
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 ;
2020-10-23 01:50:33 +11:00
}
2020-10-23 19:55:02 +11:00
public static Sprite CreateSprite ( Texture2D tex , Rect size = default )
2020-10-23 01:50:33 +11:00
{
#if CPP
2020-10-28 06:39:26 +11:00
Vector2 pivot = Vector2 . zero ;
Vector4 border = Vector4 . zero ;
2020-10-23 19:55:02 +11:00
if ( size = = default )
2020-10-28 06:39:26 +11:00
{
2020-10-23 19:55:02 +11:00
size = new Rect ( 0 , 0 , tex . width , tex . height ) ;
2020-10-28 06:39:26 +11:00
}
2020-10-23 19:55:02 +11:00
2020-10-23 01:50:33 +11:00
return Sprite . CreateSprite_Injected ( tex , ref size , ref pivot , 100f , 0 u , SpriteMeshType . Tight , ref border , false ) ;
#else
return Sprite . Create ( tex , size , Vector2 . zero ) ;
#endif
}
public static Texture2D MakeSolidTexture ( Color color , int width , int height )
{
2020-10-28 06:39:26 +11:00
Color [ ] pixels = new Color [ width * height ] ;
2020-10-23 01:50:33 +11:00
for ( int i = 0 ; i < pixels . Length ; i + + )
{
pixels [ i ] = color ;
}
2020-10-28 06:39:26 +11:00
Texture2D tex = new Texture2D ( width , height ) ;
2020-10-23 01:50:33 +11:00
tex . SetPixels ( pixels ) ;
tex . Apply ( ) ;
return tex ;
}
}
}