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

43 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class FamilyNameArea : MonoBehaviour
{
public Text nameText;
public FamilyNameAreaData data;
public GameObject countyPrefab;
public Transform countysContainer;
public UnityEvent<FamilyNameCounty> onFamilyNameCountySelected = new UnityEvent<FamilyNameCounty>();
public void setData(FamilyNameAreaData familyNameAreaData)
{
this.data = familyNameAreaData;
nameText.text = data.areaName;
GameObject item = null;
foreach (var county in data.countys)
{
item = Instantiate(countyPrefab, countysContainer);
FamilyNameCounty familyNameCounty = item.GetComponent<FamilyNameCounty>();
familyNameCounty.setData(county);
familyNameCounty.button.onClick.AddListener(onFamilyNameCountyClick);
}
RectTransform lastItemTransForm = item.GetComponent<RectTransform>();
float height = lastItemTransForm.sizeDelta.y;
if (data.countys.Count > 6)
height += lastItemTransForm.sizeDelta.y + 10;
this.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
}
private void onFamilyNameCountyClick()
{
GameObject gameObject = EventSystem.current.currentSelectedGameObject;
FamilyNameCounty familyNameCounty = gameObject.GetComponent<FamilyNameCounty>();
onFamilyNameCountySelected.Invoke(familyNameCounty);
}
}