Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2KB

  1. #if UNITY_5 || UNITY_5_3_OR_NEWER
  2. using System.Collections;
  3. using Svelto.Context;
  4. using UnityEngine;
  5. public abstract class UnityContext:MonoBehaviour
  6. {
  7. protected abstract void OnAwake();
  8. void Awake()
  9. {
  10. OnAwake();
  11. }
  12. }
  13. //a Unity context is a platform specific context wrapper.
  14. //Unity will drive the ICompositionRoot interface.
  15. //OnContextCreated is called during the Awake of this MB
  16. //OnContextInitialized is called one frame after the MB started
  17. //OnContextDestroyed is called when the MB is destroyed
  18. public class UnityContext<T>: UnityContext where T:class, ICompositionRoot, new()
  19. {
  20. protected override void OnAwake()
  21. {
  22. _applicationRoot = new T();
  23. _applicationRoot.OnContextCreated(this);
  24. }
  25. void OnDestroy()
  26. {
  27. _applicationRoot.OnContextDestroyed();
  28. }
  29. void Start()
  30. {
  31. if (Application.isPlaying == true)
  32. StartCoroutine(WaitForFrameworkInitialization());
  33. }
  34. IEnumerator WaitForFrameworkInitialization()
  35. {
  36. //let's wait until the end of the frame, so we are sure that all the awake and starts are called
  37. yield return new WaitForEndOfFrame();
  38. _applicationRoot.OnContextInitialized();
  39. }
  40. T _applicationRoot;
  41. }
  42. #endif