refactor: pull out method

pull out event to be able to unsubscribe from it when mod is unloaded
This commit is contained in:
2026-08-23 22:59:26 +02:00
parent f53b8fa854
commit 7a1227d588
+28 -2
View File
@@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace IngameReveries namespace IngameReveries
@@ -29,7 +30,19 @@ namespace IngameReveries
{ {
InitializeAssets(); InitializeAssets();
SceneManager.sceneLoaded += (scene, mode) => SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDestroy()
{
// Make sure you clean up properly to support Live Reload.
Debug.Log("Good bye! " + mod.metadata.id);
harmony.UnpatchAll();
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{ {
// Check if UI already instantiated // Check if UI already instantiated
if (_ingameReveriesUI != null) if (_ingameReveriesUI != null)
@@ -54,7 +67,7 @@ namespace IngameReveries
_ingameReveriesUI = Instantiate(_reveriesPrefab, menuViewGO.transform); _ingameReveriesUI = Instantiate(_reveriesPrefab, menuViewGO.transform);
_ingameReveriesUI.SetActive(true); _ingameReveriesUI.SetActive(true);
_ingameReveriesUI.name = "IngameReveriesUI"; _ingameReveriesUI.name = "IngameReveriesUI";
_ingameReveriesUI.transform.SetAsFirstSibling(); _ingameReveriesUI.transform.SetSiblingIndex(1);
var reveries = _ingameReveriesUI.GetComponent<UI_Reveries>(); var reveries = _ingameReveriesUI.GetComponent<UI_Reveries>();
@@ -72,6 +85,19 @@ namespace IngameReveries
// Make sure you clean up properly to support Live Reload. // Make sure you clean up properly to support Live Reload.
Debug.Log("Good bye! " + mod.metadata.id); Debug.Log("Good bye! " + mod.metadata.id);
harmony.UnpatchAll(); harmony.UnpatchAll();
// Show reveries window
var canvasGroup = _ingameReveriesUI.GetComponent<CanvasGroup>();
canvasGroup.alpha = 1f;
canvasGroup.interactable = true;
canvasGroup.blocksRaycasts = true;
// Position reveries window top left
var rect = _ingameReveriesUI.GetComponent<RectTransform>();
rect.anchorMin = new Vector2(0f, 1f);
rect.anchorMax = new Vector2(0f, 1f);
rect.pivot = new Vector2(0f, 1f);
rect.anchoredPosition = new Vector2(+70f, -70f);
}
} }
private void InitializeAssets() private void InitializeAssets()