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 onFamilyNameCountySelected = new UnityEvent(); 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.setData(county); familyNameCounty.button.onClick.AddListener(onFamilyNameCountyClick); } RectTransform lastItemTransForm = item.GetComponent(); float height = lastItemTransForm.sizeDelta.y; if (data.countys.Count > 6) height += lastItemTransForm.sizeDelta.y + 10; this.gameObject.GetComponent().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); } private void onFamilyNameCountyClick() { GameObject gameObject = EventSystem.current.currentSelectedGameObject; FamilyNameCounty familyNameCounty = gameObject.GetComponent(); onFamilyNameCountySelected.Invoke(familyNameCounty); } }