From 7a1227d5888bf4a2e91c1dcc3e07156fea474d1b Mon Sep 17 00:00:00 2001 From: davud Date: Sun, 23 Aug 2026 22:59:26 +0200 Subject: [PATCH] refactor: pull out method pull out event to be able to unsubscribe from it when mod is unloaded --- IngameReveries/IngameReveries.cs | 70 ++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/IngameReveries/IngameReveries.cs b/IngameReveries/IngameReveries.cs index e8dd58e..0077aa0 100644 --- a/IngameReveries/IngameReveries.cs +++ b/IngameReveries/IngameReveries.cs @@ -2,6 +2,7 @@ using System.Linq; using UnityEngine; using UnityEngine.SceneManagement; +using UnityEngine.UI; namespace IngameReveries @@ -29,34 +30,46 @@ namespace IngameReveries { 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 + if (_ingameReveriesUI != null) { - // Check if UI already instantiated - if (_ingameReveriesUI != null) + return; + } + + string gameSceneDir = "Assets/Dew/Zones/"; + string relativeToScene = Path.GetRelativePath(gameSceneDir, scene.path); + + if (!relativeToScene.StartsWith("..")) + { + Debug.Log($"[IngameReveries] Switched to scene inside {gameSceneDir}"); + + GameObject menuViewGO = GameObject.Find("GameLogicPackage/UI/InGame/UI_Common_MenuView"); + if (menuViewGO == null) { + Debug.Log("Couldn't find menu view"); return; } - string gameSceneDir = "Assets/Dew/Zones/"; - string relativeToScene = Path.GetRelativePath(gameSceneDir, scene.path); + _ingameReveriesUI = Instantiate(_reveriesPrefab, menuViewGO.transform); + _ingameReveriesUI.SetActive(true); + _ingameReveriesUI.name = "IngameReveriesUI"; + _ingameReveriesUI.transform.SetSiblingIndex(1); - if (!relativeToScene.StartsWith("..")) - { - Debug.Log($"[IngameReveries] Switched to scene inside {gameSceneDir}"); - - GameObject menuViewGO = GameObject.Find("GameLogicPackage/UI/InGame/UI_Common_MenuView"); - if (menuViewGO == null) - { - Debug.Log("Couldn't find menu view"); - return; - } - - _ingameReveriesUI = Instantiate(_reveriesPrefab, menuViewGO.transform); - _ingameReveriesUI.SetActive(true); - _ingameReveriesUI.name = "IngameReveriesUI"; - _ingameReveriesUI.transform.SetAsFirstSibling(); - - var reveries = _ingameReveriesUI.GetComponent(); + var reveries = _ingameReveriesUI.GetComponent(); var rect = _ingameReveriesUI.GetComponent(); rect.anchorMin = new Vector2(0f, 1f); @@ -72,6 +85,19 @@ namespace IngameReveries // Make sure you clean up properly to support Live Reload. Debug.Log("Good bye! " + mod.metadata.id); harmony.UnpatchAll(); + // Show reveries window + var canvasGroup = _ingameReveriesUI.GetComponent(); + canvasGroup.alpha = 1f; + canvasGroup.interactable = true; + canvasGroup.blocksRaycasts = true; + + // Position reveries window top left + var rect = _ingameReveriesUI.GetComponent(); + 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()