Skip to content

Instantly share code, notes, and snippets.

@shayded-exe
Last active February 10, 2020 20:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shayded-exe/e9342b8181fb48ece38b373e12276617 to your computer and use it in GitHub Desktop.
Save shayded-exe/e9342b8181fb48ece38b373e12276617 to your computer and use it in GitHub Desktop.
Dynamic Zenject enemy prefab factory
public partial class EnemyFacade : MonoBehaviour
{
public class Factory : Factory<EnemyType, EnemyTunables, EnemyFacade> { }
}
public enum EnemyType
{
Red,
Green
}
public class GameInstaller : MonoInstaller
{
[SerializeField] private Settings settings = null;
public override void InstallBindings()
{
// Other bindings and stuff...
// Original factory for reference
//Container
// .BindFactory<EnemyTunables, EnemyFacade, EnemyFacade.Factory>()
// .FromSubContainerResolve()
// .ByPrefab<EnemyInstaller>(this.settings.EnemyFacadePrefab)
// .UnderGameObjectGroup("Enemies");
// New factory
Container
.BindFactory<EnemyType, EnemyTunables, EnemyFacade, EnemyFacade.Factory>()
.FromSubContainerResolve()
.ByMethod(CreateEnemy);
}
private void CreateEnemy(DiContainer subContainer, EnemyType type, EnemyTunables tunables)
{
subContainer
.Bind<EnemyFacade>()
.FromPrefab(this.settings.EnemyPrefabs.Single(p => p.Type == type).Prefab)
.UnderGameObjectGroup("Enemies");
subContainer.BindInstance(tunables, true).WhenInjectedInto<EnemyInstaller>();
}
[Serializable]
public class Settings
{
public List<EnemyPrefabMapping> EnemyPrefabs;
}
[Serializable]
public class EnemyPrefabMapping
{
public EnemyType Type;
public GameObject Prefab;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment