其实是绕了一圈,把数组里的元素放进数组列表里再读取它的下标
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
using UnityEngine.UI;
public class AddListener : MonoBehaviour {
public GameObject[] arrayButton;//存放按钮的数组
private ArrayList listButton;//存放按钮的数组列表
private GameObject temporaryBtn;//临时放存放单个按钮
private int buttonNum;
// Use this for initialization
void Start () {
listButton = new ArrayList(arrayButton);
foreach (GameObject btn in arrayButton)
{
btn.GetComponent<Button>().onClick.AddListener(delegate () { ShowNumber(); });
}
}
public void ShowNumber()
{
temporaryBtn = EventSystem.current.currentSelectedGameObject;//获取被点击的物体,注意要引用using UnityEngine.EventSystems;
buttonNum = listButton.Index