Skip to content

Instantly share code, notes, and snippets.

@zachiPL
Last active December 4, 2019 17:55
Show Gist options
  • Save zachiPL/4ee5e7967ecdae31a42cb6f6df9e8464 to your computer and use it in GitHub Desktop.
Save zachiPL/4ee5e7967ecdae31a42cb6f6df9e8464 to your computer and use it in GitHub Desktop.
Inject list elements to prefabs during instatiate
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