如何使应用程序启动方法上的LoginController在以下情况下运行:
正在调用应用程序运行方法,或分派事件
应用程序启动事件。
EventManager
可以注册和取消注册事件的脚本。这些功能应该
ApplicationStartedEvent
作为参数。再添加一个可用于调用已订阅事件的函数。
然后,您可以订阅其他脚本中的事件,例如
LoginController
剧本订阅应在
OnEnable
和
OnDisable
作用
然后可以从
run()
应用程序脚本中的函数。
非常重要
:
请重命名您的
Application
脚本
Application2
或者别的什么。有一个名为
Application
如果您使用此类中的函数,则会遇到编译时错误。
我将使用
而不是
应用
在该解决方案中。
EventManager.cs
:
请将此附加到空游戏对象
public class EventManager : MonoBehaviour
{
private static EventManager localInstance;
public static EventManager Instance { get { return localInstance; } }
private void Awake()
{
if (localInstance != null && localInstance != this)
{
Destroy(this.gameObject);
}
else
{
localInstance = this;
}
}
public delegate void ApplicationStartedEvent(object source, EventArgs args);
private event ApplicationStartedEvent applicationStartedEvent;
public void dispatchEvent(object source, EventArgs args)
{
foreach (ApplicationStartedEvent runEvent in applicationStartedEvent.GetInvocationList())
{
try
{
runEvent.Invoke(source, args);
}
catch (Exception e)
{
Debug.LogError(string.Format("Exception while invoking" + runEvent.Method.Name + e.Message));
}
}
}
public void registerEvent(ApplicationStartedEvent callBackFunc)
{
applicationStartedEvent += callBackFunc;
}
public void unRegisterEvent(ApplicationStartedEvent callBackFunc)
{
applicationStartedEvent -= callBackFunc;
}
}
:
public class Application2 : IApplication
{
public void run()
{
OnApplicationStarted();
}
protected virtual void OnApplicationStarted()
{
EventManager.Instance.dispatchEvent(this, EventArgs.Empty);
}
}
LoginController.cs
:
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);
}
void OnEnable()
{
//Subscribe to event
EventManager.Instance.registerEvent(OnApplicationStarted);
}
void OnDisable()
{
//Un-Subscribe to event
EventManager.Instance.unRegisterEvent(OnApplicationStarted);
}
}