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.

35 lines
721B

  1. #region
  2. using System;
  3. using UnityEngine;
  4. #endregion
  5. namespace Svelto.Context
  6. {
  7. public class MonoBehaviourFactory: Factories.IMonoBehaviourFactory
  8. {
  9. IUnityContextHierarchyChangedListener _unityContext;
  10. public MonoBehaviourFactory(IUnityContextHierarchyChangedListener unityContext)
  11. {
  12. _unityContext = unityContext;
  13. }
  14. public M Build<M>(Func<M> constructor) where M:MonoBehaviour
  15. {
  16. var mb = constructor();
  17. _unityContext.OnMonobehaviourAdded(mb);
  18. GameObject go = mb.gameObject;
  19. if (go.GetComponent<NotifyComponentsRemoved>() == null)
  20. go.AddComponent<NotifyComponentsRemoved>().unityContext = _unityContext;
  21. return mb;
  22. }
  23. }
  24. }