/
GeekNerd
/
ServerModelingProject
Обзор
Документация
Войти
/
GeekNerd
/
ServerModelingProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/Plugins/Zenject/Source/Util/ZenAutoInjecter.cs
73 строки
2 KB
Gorney-Alex
Init
30 май 2026, 19:05
30 май 2026, 19:05
3cdcc49
Код
Авторство
О чём код?
using ModestTree; using UnityEngine; namespace Zenject { public class ZenAutoInjecter : MonoBehaviour { [SerializeField] ContainerSources _containerSource = ContainerSources.SearchHierarchy; bool _hasInjected; public ContainerSources ContainerSource { get { return _containerSource; } set { _containerSource = value; } } // Make sure they don't cause injection to happen twice [Inject] public void Construct() { if (!_hasInjected) { throw Assert.CreateException( "ZenAutoInjecter was injected! Do not use ZenAutoInjecter for objects that are instantiated through zenject or which exist in the initial scene hierarchy"); } } public void Awake() { _hasInjected = true; LookupContainer().InjectGameObject(gameObject); } DiContainer LookupContainer() { if (_containerSource == ContainerSources.ProjectContext) { return ProjectContext.Instance.Container; } if (_containerSource == ContainerSources.SceneContext) { return GetContainerForCurrentScene(); } Assert.IsEqual(_containerSource, ContainerSources.SearchHierarchy); var parentContext = transform.GetComponentInParent<Context>(); if (parentContext != null) { return parentContext.Container; } return GetContainerForCurrentScene(); } DiContainer GetContainerForCurrentScene() { return ProjectContext.Instance.Container.Resolve<SceneContextRegistry>() .GetContainerForScene(gameObject.scene); } public enum ContainerSources { SceneContext, ProjectContext, SearchHierarchy } } }