Last active
February 10, 2020 20:59
-
-
Save shayded-exe/e9342b8181fb48ece38b373e12276617 to your computer and use it in GitHub Desktop.
Dynamic Zenject enemy prefab factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class EnemyFacade : MonoBehaviour | |
{ | |
public class Factory : Factory<EnemyType, EnemyTunables, EnemyFacade> { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum EnemyType | |
{ | |
Red, | |
Green | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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