获取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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值