[英]Does a UI Button in Unity have a location?
這很容易做到
首先讓我們編寫一個腳本GridController.cs
using UnityEngine.UI;
在頂部添加對UI名稱空間的引用using UnityEngine.UI;
現在將所有網格項放置在父容器中(可以是一個空的游戲對象)
現在開始編碼
//Reference parent gameobject
[SerializeField]
private GameObject gridContainer;
//List of all child elements
private List<Button> gridObjects = new List<Button>();
private void Start(){
//Loop through all children
for(int i = 0; i < gridContainer.transform.childCount; i++){
//Add button to list
gridObjects.Add(gridContainer.transform.getChild(i).gameObject.GetComponent<Button>());
}
//Set listeners for buttons
for(int i = 0; i < gridObjects.Count;i++){
gridObjects[i].onClick.AddListener(delegate{
DoSomething();
});
}
}
private void DoSomething(){
//Do Something
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.