Files
TaiWan/Assets/ZX/ZX6/Scripts/FamilyNameIndex.cs
2025-10-31 15:20:38 +08:00

44 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Events;
public class FamilyNameIndex : MonoBehaviour
{
public Text indexText;
public GameObject familyNamePrefab;
public Transform familyNamesContainer;
private FamilyNameIndexData data;
public UnityEvent<FamilyName> onFamilyNameSelected = new UnityEvent<FamilyName>();
public void setData(FamilyNameIndexData familyNameIndexData)
{
this.data = familyNameIndexData;
indexText.text = data.index;
GameObject familyNameItem = null;
foreach (var name in data.names)
{
familyNameItem = Instantiate(familyNamePrefab, familyNamesContainer);
FamilyName familyName = familyNameItem.GetComponent<FamilyName>();
familyName.nameText.text = name;
familyName.button.onClick.AddListener(onFamilyNameClick);
}
RectTransform lastItemTransForm = familyNameItem.GetComponent<RectTransform>();
float height = lastItemTransForm.sizeDelta.y;
if (data.names.Count > 10)
height += lastItemTransForm.sizeDelta.y + 10;
this.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
}
private void onFamilyNameClick()
{
GameObject gameObject = EventSystem.current.currentSelectedGameObject;
FamilyName familyName = gameObject.GetComponent<FamilyName>();
onFamilyNameSelected.Invoke(familyName);
}
}