Last active
December 4, 2019 17:55
-
-
Save zachiPL/4ee5e7967ecdae31a42cb6f6df9e8464 to your computer and use it in GitHub Desktop.
Inject list elements to prefabs during instatiate
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
using UnityEngine; | |
using Zenject; | |
public class ChooseHeroPanel : MenuPanelBehaviour | |
{ | |
[Inject] private DiContainer _container; | |
[Inject] private ILocateConfig _config; | |
[SerializeField] private GameObject _prefabForSingleHeroView = null; | |
void Awake() | |
{ | |
/** There is list of available `HeroTemplate` in `_config.GetCorrectConfig().heroes` */ | |
foreach (var hero in _config.GetCorrectConfig().heroes) | |
{ | |
var c = _container.CreateSubContainer(); | |
c.BindInstance(hero); | |
c.InstantiatePrefab(_prefabForSingleHeroView, this.transform); | |
} | |
} | |
} | |
// ------------------------------------------ SingleHeroPanel.cs ----------------------------/ | |
/** This file is on prefab */ | |
using TMPro; | |
using UnityEngine; | |
using Zenject; | |
public class SingleHeroPanel : MonoBehaviour | |
{ | |
[Inject] private HeroTemplate _tempalte; // here is injected the HeroTemplate from list | |
[SerializeField] private TextMeshProUGUI _labelForName; | |
public void Awake() | |
{ | |
_labelForName.text = _tempalte.Name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment