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.

37 lines
1013B

  1. #region
  2. using System;
  3. using Svelto.DataStructures;
  4. using UnityEngine;
  5. #endregion
  6. namespace Svelto.Context.Legacy
  7. {
  8. public class MonoBehaviourFactory : Factories.IMonoBehaviourFactory
  9. {
  10. public MonoBehaviourFactory(IUnityContextHierarchyChangedListener unityContext)
  11. {
  12. _unityContext = new WeakReference<IUnityContextHierarchyChangedListener>(unityContext);
  13. }
  14. public M Build<M>(Func<M> constructor) where M : MonoBehaviour
  15. {
  16. DesignByContract.Check.Require(_unityContext.IsAlive == true, "Context is used, but not alive");
  17. var mb = constructor();
  18. _unityContext.Target.OnMonobehaviourAdded(mb);
  19. GameObject go = mb.gameObject;
  20. if (go.GetComponent<NotifyComponentsRemoved>() == null)
  21. go.AddComponent<NotifyComponentsRemoved>().unityContext = _unityContext;
  22. return mb;
  23. }
  24. WeakReference<IUnityContextHierarchyChangedListener> _unityContext;
  25. }
  26. }