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.

46 lines
976B

  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. public class UnityContext<T>: UnityContext where T:class, ICompositionRoot, new()
  14. {
  15. protected override void OnAwake()
  16. {
  17. _applicationRoot = new T();
  18. _applicationRoot.OnContextCreated(this);
  19. }
  20. void OnDestroy()
  21. {
  22. _applicationRoot.OnContextDestroyed();
  23. }
  24. void Start()
  25. {
  26. if (Application.isPlaying == true)
  27. StartCoroutine(WaitForFrameworkInitialization());
  28. }
  29. IEnumerator WaitForFrameworkInitialization()
  30. {
  31. //let's wait until the end of the frame, so we are sure that all the awake and starts are called
  32. yield return new WaitForEndOfFrame();
  33. _applicationRoot.OnContextInitialized();
  34. }
  35. T _applicationRoot;
  36. }
  37. #endif