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.

UnityContext.cs 936B

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections;
  2. using Svelto.Context;
  3. using UnityEngine;
  4. public abstract class UnityContext:MonoBehaviour
  5. {
  6. protected abstract void OnAwake();
  7. void Awake()
  8. {
  9. OnAwake();
  10. }
  11. }
  12. public class UnityContext<T>: UnityContext where T:class, ICompositionRoot, new()
  13. {
  14. protected override void OnAwake()
  15. {
  16. _applicationRoot = new T();
  17. _applicationRoot.OnContextCreated(this);
  18. }
  19. void OnDestroy()
  20. {
  21. _applicationRoot.OnContextDestroyed();
  22. }
  23. void Start()
  24. {
  25. if (Application.isPlaying == true)
  26. StartCoroutine(WaitForFrameworkInitialization());
  27. }
  28. IEnumerator WaitForFrameworkInitialization()
  29. {
  30. //let's wait until the end of the frame, so we are sure that all the awake and starts are called
  31. yield return new WaitForEndOfFrame();
  32. _applicationRoot.OnContextInitialized();
  33. }
  34. T _applicationRoot;
  35. }