Let's assume I have a class which does not inherit from MonoBehavior that looks like this:
public interface IApplication
{
void run();
}
public class Application : IApplication
{
public static IServiceManager serviceManager;
public delegate void ApplicationStartedEvent(object source, EventArgs args);
public event ApplicationStartedEvent applicationStartedEvent;
public void run()
{
}
protected virtual void OnApplicationStarted()
{
if (applicationStartedEvent != null)
{
applicationStartedEvent(this, EventArgs.Empty);
}
}
}
And a Unity controller which is a MonoBehavior like this:
public class LoginController : MonoBehaviour
{
void Start()
{
Hide();
}
public void OnApplicationStarted(object source, EventArgs e)
{
Show();
}
public virtual void Show()
{
gameObject.SetActive(true);
}
public virtual void Hide()
{
gameObject.SetActive(false);
}
}
How do I connect those two?
Let's assume I have the following Bootstrap code:
public class Setup : MonoBehaviour
{
Application application;
void Awake()
{
application = new Application();
application.run();
}
}
How do I make my LoginController
OnApplicationStarted
method run when Application
run
method is being invoked or Dispatch an event that is ApplicationStartedEvent
.
An example of how do I think EventManager should look like:
addEventListener(string eventName, Callback callback)
dispatchEvent(Event event)
Here's how ull use it in the LoginController:
EventManager.addEventListener("ApplicationStarted", this.OnApplicationStarted)
And here's how ull use it in the Application:
public void run()
{
EventManager.dispatchEvent(new ApplicationStartedEvent());
}
Obviously it's just an idea and not have to be the same syntax.
EDIT: I think I have found something that is almost similar to what I am looking for: http://wiki.unity3d.com/index.php/Advanced_CSharp_Messenger