Skip to content

Instantly share code, notes, and snippets.

@JottoWorol
Last active July 2, 2022 13:57
Show Gist options
  • Save JottoWorol/92986268268abc1348c9ca811304c666 to your computer and use it in GitHub Desktop.
Save JottoWorol/92986268268abc1348c9ca811304c666 to your computer and use it in GitHub Desktop.
Lover Lab: Dynamic UI content creation (once per level, driven by config)
public class RequirementElement : MonoBehaviour
{
[SerializeField] private GameObject _tickImage;
[SerializeField] private TMP_Text _requirementNameText;
public void SetStatus(bool isSatisfied) => _tickImage.SetActive(isSatisfied);
public void SetName(string requirementName)
{
_requirementNameText.SetText(requirementName);
}
}
public class RequirementPanel : IUpdatable
{
private readonly RequirementElement _requirementElementPrefab;
private readonly List<RequirementElement> _requirementElementList = new List<RequirementElement>();
private StatRequirement[] _requirements;
private readonly Level _level;
private readonly RequirementChecker _requirementChecker;
private readonly Transform _elementsParent;
public RequirementPanel(SceneData sceneData, RequirementChecker requirementChecker, UiSceneData uiSceneData)
{
_level = sceneData.Level;
_requirementChecker = requirementChecker;
_elementsParent = uiSceneData.RequirementsParent;
_requirementElementPrefab = uiSceneData.RequirementElementPrefab;
}
public void Open()
{
ClearElements();
Init();
_elementsParent.gameObject.SetActive(true);
}
public void ClearElements()
{
if (_requirementElementList.Count > 0)
{
foreach (var requirementElement in _requirementElementList)
{
requirementElement.gameObject.SetActive(false);
}
_requirementElementList.Clear();
}
}
public void OnUpdate()
{
if (!_requirementChecker.IsInitialized)
return;
for (int i = 0; i < _requirementElementList.Count; i++)
{
_requirementElementList[i].SetStatus(_requirementChecker.RequirementSatisfied[i]);
}
}
private void Init()
{
_requirements = _level.StatRequirements;
foreach (var requirement in _requirements)
{
var element = Object.Instantiate(_requirementElementPrefab, _elementsParent);
element.SetName(requirement.Stat.ValuePresets[requirement.PresetIndex].Name);
_requirementElementList.Add(element);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment