I have a stack to store all objects with base panel class
private Stack<BasePanel> stackPanels;
then I have different panels all inherited BasePanel. inside they all have a method call
public virtual void OnExit()
{
Debug.Log("UITypeName= "+UIType.Name);
UIManage.DestoryUI(UIType);
}
Then I trigger the Onexit method in the object using the first object in the stack.
public override void OnEnter()
{
UITool.GetOrAddComponentsInChildren<Button>("Next").onClick.AddListener(() =>
{
//Debug.Log($"Start create account");
PanelManage.Pop();
PanelManage.Push(new PrefectProfile());
});
}
The First panel is working fine and exit like except however the second panel didn't get exit.
public override void OnEnter()
{
UITool.GetOrAddComponentsInChildren<Button>("SelectFile").onClick.AddListener(()=>
{
Debug.Log("Upload");
PanelManage.Pop();
});
UITool.GetOrAddComponentsInChildren<Button>("UploadLater").onClick.AddListener(() =>
{
//Debug.Log($"UploadLater");
PanelManage.Pop();
PanelManage.Push(new IntroductionPanel());
// Debug.Log(PanelManage.panel.UIType.Path);
});
}
POP
public void Pop()
{
if (stackPanels.Count > 0)
{
stackPanels.Peek().OnExit();
Debug.Log(stackPanels.First());
//Debug.Log(stackPanels.First().UIManage);
stackPanels.Pop();
}
if (stackPanels.Count > 0)
stackPanels.Peek().OnResume();
}
So the logic is OnEnter -> so button will respone-> then pop-> pop trigger OnExit.
The part I don't get is, that the code is run line by line. And if you see the console where it debugs: create panel and perfect profile, these come from POP, if it did run the Debug meaning it run the code above as well, which is OnExit.
However, when you look at the console, OnExit only gets run in the create panel since it debugs Debug.Log("UITypeName= "+UIType.Name);
But it didn't run for the PrefectPanel. And they both have the same inherited class meaning the method are the same. Then how come it didn't get run on the second panel but it work on the first one? And if it didn't run the Exit method, how come it has the Debuged name? Since the Debug is below the Exit method, if there's any error it should pop up already. However, let's say it did run the Exit method then how come it didn't destroy the panel and no log for the exit on the second panel? I have been checking the code for hours and do not have any clues
Your overrides are not calling the base implementation => you only fully replace them by the new implementation!
Within the override
ones add calls to
base.OnEnter();
and accordingly
base.OnExit();
according to your needs.
See e.g. base
keyword examples
It basically can be anywhere but usually I would go with
public override void OnEnter()
{
base.OnEnter();
// additional custom implementation
}
public override void OnExit()
{
// additional custom implementation
base.OnExit();
}
in order to let the initialization of the base run first but when tearing down make it the last since e.g. in your case after destroying things the custom implementation might not be valid anymore since references might be already gone etc.