获取Button的名字 并在按下时持续打印

//方法一:利用泛型集合 获取Button名字 实现持续打印

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;

public class PrintInfo : MonoBehaviour
{
  public List<GameObject> printAddInfo = new List<GameObject>();//增加按钮
  public List<GameObject> PrintSubInfo = new List<GameObject>();//减小按钮
  bool flag = false;
  GameObject go;
  void Start()
  {
    for (int i = 0; i < printAddInfo.Count; i++)
    {
      int j = i;
      EventTriggerListener.Get(printAddInfo[j]).onDown += MouseOnDown;
      EventTriggerListener.Get(printAddInfo[j]).onUp += MouseOnUp;
    }
    for (int i = 0; i <PrintSubInfo.Count; i++)
    {
      int j = i;
      EventTriggerListener.Get(PrintSubInfo[j]).onDown += MouseOnDown;
      EventTriggerListener.Get(PrintSubInfo[j]).onUp += MouseOnUp;
    }
  }
  void MouseOnDown(GameObject btn)
  {
    go = btn;
    flag = true;
  }
  void MouseOnUp(GameObject btn)
  {
    go = null;
    flag = false;
  }
  void Update()
  {
    if (go != null)
    {
      Debug.Log(go.name);
    }
  }
}

 

//方法二:读取字符串 获取名字 在属性面板绑定字符串

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class PrintNewInfo : MonoBehaviour,IPointerDownHandler,IPointerUpHandler

{
  public string Info;
  bool flag = false;
  void Update () 
  {
    if(flag)
    {
      Debug.Log(Info);
    }
  }
  public void OnPointerDown(PointerEventData eventData)
  {
    flag = true;
  }
  public void OnPointerUp(PointerEventData eventData)
  {
    flag = false;
  }

}
原文地址:https://www.cnblogs.com/Cocomo/p/5711162.html