I am trying to figure out the best way to tell if a window got closed so that I can perform clean up responsibilities. For example any staged points should go back to unallocated. Or maybe I want to wipe the “cart” in a merchant shop if I close the window.
The below is the best way I found but it feels a bit hacky to rely on a component’s state to tell if the whole game object got deactivated. Is there something more reliable?
Steps
Put this on Traits Window (the child object of the UI game object)
using System;
using UnityEngine;
namespace RPG.Core
{
// Put this code on any gameobject where you want to receive notifications if it got deactivated
// or activated. The events fired will make the assumption that this Monobehavior will only ge
// enabled / disabled is the object is activated / deactivated, respectively.
// Warning: It will trigger false positives if just the component state is modified.
public class ObjectStateNotifier : MonoBehaviour
{
public Action GameObjectDeactivated;
public Action GameObjectActivated;
private void OnEnable()
{
GameObjectActivated?.Invoke();
}
private void OnDisable()
{
GameObjectDeactivated?.Invoke();
}
}
}
Modify TraitUI as below
using TMPro;
using RPG.Core;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace RPG.Stats
{
public class TraitUI : MonoBehaviour
{
[SerializeField] private ObjectStateNotifier traitsUIWindow;
private void OnEnable()
{
traitsUIWindow.GameObjectActivated += playerTraitStore.ClearUnassignedPoints;
}
private void OnDisable()
{
traitsUIWindow.GameObjectActivated -= playerTraitStore.ClearUnassignedPoints;
}
}
}
Add the following method to PlayerTraitStore
public void ClearUnassignedPoints()
{
stagedPoints.Clear();
}