refactor: pull out method
pull out event to be able to unsubscribe from it when mod is unloaded
This commit is contained in:
@@ -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<UI_Reveries>();
|
||||
var reveries = _ingameReveriesUI.GetComponent<UI_Reveries>();
|
||||
|
||||
var rect = _ingameReveriesUI.GetComponent<RectTransform>();
|
||||
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>();
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user