1
0
mirror of https://github.com/originalnicodr/CinematicUnityExplorer.git synced 2026-10-10 13:03:15 +08:00

Refactored the pause button to work with UnityExplorer timescale widget.

This commit is contained in:
originalnicodr
2024-01-13 14:30:42 -03:00
parent aad2567b86
commit d70fc10ea5
4 changed files with 55 additions and 34 deletions
+14 -19
View File
@@ -8,6 +8,7 @@ using Il2CppInterop.Runtime.Injection;
#endif
#endif
using UnityExplorer.UI.Panels;
using UnityExplorer.UI.Widgets;
using System;
@@ -75,46 +76,40 @@ namespace UnityExplorer
// Cinematic stuff
public class TimeScaleController : MonoBehaviour
public class PauseListener : MonoBehaviour
{
internal static TimeScaleController Instance { get; private set; }
internal static PauseListener Instance { get; private set; }
#if CPP
public TimeScaleController(System.IntPtr ptr) : base(ptr) { }
public PauseListener(System.IntPtr ptr) : base(ptr) { }
#endif
public bool pause;
bool settingTimeScale;
public static TimeScaleWidget timeScaleWidget;
internal static void Setup()
{
#if CPP
ClassInjector.RegisterTypeInIl2Cpp<TimeScaleController>();
ClassInjector.RegisterTypeInIl2Cpp<PauseListener>();
#endif
GameObject obj = new("TimeScaleController");
GameObject obj = new("PauseListener");
DontDestroyOnLoad(obj);
obj.hideFlags = HideFlags.HideAndDontSave;
Instance = obj.AddComponent<TimeScaleController>();
Instance = obj.AddComponent<PauseListener>();
timeScaleWidget = UIManager.GetTimeScaleWidget();
}
public void Update()
{
if (InputManager.GetKeyDown(KeyCode.Pause))
{
pause = !pause;
if (pause)
SetTimeScale(0f);
else
SetTimeScale(1f);
timeScaleWidget.PauseToggle();
}
}
void SetTimeScale(float time)
{
settingTimeScale = true;
Time.timeScale = time;
settingTimeScale = false;
// Force pause no matter the game timescale changes.
if (timeScaleWidget.IsPaused() && Time.timeScale != 0) {
timeScaleWidget.SetTimeScale(0f);
}
}
}
+1 -1
View File
@@ -59,7 +59,7 @@ namespace UnityExplorer
UnityCrashPrevention.Init();
//Extra inits
TimeScaleController.Setup();
PauseListener.Setup();
}
// Do a delayed setup so that objects aren't destroyed instantly.
+2
View File
@@ -165,6 +165,8 @@ namespace UnityExplorer.UI
panel.SetActive(active);
}
public static TimeScaleWidget GetTimeScaleWidget() { return timeScaleWidget;}
// navbar
public static void SetNavBarAnchor()
+38 -14
View File
@@ -10,7 +10,7 @@ using IL2CPPUtils = Il2CppInterop.Common.Il2CppInteropUtils;
namespace UnityExplorer.UI.Widgets
{
internal class TimeScaleWidget
public class TimeScaleWidget
{
public TimeScaleWidget(GameObject parent)
{
@@ -28,22 +28,34 @@ namespace UnityExplorer.UI.Widgets
InputFieldRef timeInput;
float desiredTime;
bool settingTimeScale;
TimeScaleController timeScaleController = GameObject.Find("TimeScaleController").GetComponent<TimeScaleController>();
bool pause;
public void Update()
{
//We don't want Unity Explorer's native time scale control to interfere with ours.
if (!timeScaleController.pause){
// Fallback in case Time.timeScale patch failed for whatever reason
if (locked)
SetTimeScale(desiredTime);
// Fallback in case Time.timeScale patch failed for whatever reason
if (locked)
SetTimeScale(desiredTime);
if (!timeInput.Component.isFocused)
timeInput.Text = Time.timeScale.ToString("F2");
}
if (!timeInput.Component.isFocused)
timeInput.Text = Time.timeScale.ToString("F2");
}
void SetTimeScale(float time)
public void PauseToggle(){
pause = !pause;
if (!pause) {
SetTimeScale(1f); //or previous timescale
}
locked = pause;
desiredTime = pause ? 0 : 1;
UpdatePauseButton();
}
public bool IsPaused(){
return pause;
}
public void SetTimeScale(float time)
{
settingTimeScale = true;
Time.timeScale = time;
@@ -63,10 +75,22 @@ namespace UnityExplorer.UI.Widgets
void OnPauseButtonClicked()
{
OnTimeInputEndEdit(timeInput.Text);
if (pause){
pause = false;
desiredTime = 1;
SetTimeScale(desiredTime);
}
else {
OnTimeInputEndEdit(timeInput.Text);
}
locked = !locked;
UpdatePauseButton();
}
void UpdatePauseButton()
{
Color color = locked ? new Color(0.3f, 0.3f, 0.2f) : new Color(0.2f, 0.2f, 0.2f);
RuntimeHelper.SetColorBlock(lockBtn.Component, color, color * 1.2f, color * 0.7f);
lockBtn.ButtonText.text = locked ? "Unlock" : "Lock";
@@ -111,7 +135,7 @@ namespace UnityExplorer.UI.Widgets
static bool Prefix_Time_set_timeScale()
{
return Instance.timeScaleController.pause || !Instance.locked || Instance.settingTimeScale;
return !Instance.locked || Instance.settingTimeScale;
}
}
}