- Home /
Login form: Do I have to set custom delegates to null in OnDestroy?
I have a LoginForm that opens an AdminPanel. The latter has a delegate OnLoginRequested to send feedback, whether login was sucessful or not.
I can register and unregister to this event, but I wonder where and if I have to unregister all listeners manually in OnDestroy. Do I need to call AdminPanel.RemoveLoginCallback in every listeners OnDestroy or is it enough to just say OnLoginRequested = null in my AdminPanel destroy?
Does this look like I'm going in the right direction or should I try another approach? Everything works so far, but I'm opting for a "clean" solution for learning purposes.
Thank you very much!
using UnityEngine;
using System.Collections;
public class AdminPanel : MonoBehaviour, ILoginProtected
{
public event LoginRequest OnLoginRequested;
public void ILoginProtected.AddLoginCallback(LoginRequest listener)
{
OnLoginRequested += listener;
}
public void ILoginProtected.RemoveLoginCallback(LoginRequest listener)
{
OnLoginRequested -= listener;
}
public void ILoginProtected.RequestLogin(string username, string password)
{
bool correctUsername = username == Defines.Login.Username;
bool correctPassword = password == Defines.Login.Password;
if(OnLoginRequested != null)
{
OnLoginRequested(correctUsername, correctPassword);
}
this.Open();
}
void Open()
{
Debug.Log(name + ": Opening.", this);
}
void OnDestroy()
{
// Necessary? Useful? Or do I have to RemoveLoginCallback manually?
OnLoginRequested = null;
}
}
Comment
Your answer
Follow this Question
Related Questions
Sharing delegate types across scripts 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers