unity UGUI点击事件

本文详细介绍了一个基于Unity的游戏UI交互实现,包括按钮点击、按下、弹起等事件的监听与响应,以及登录对话框的具体操作流程,如显示、隐藏、输入账号密码并发送到数据库验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///

///处理button点击事件

///

public class EventTrriggerListener : UnityEngine.EventSystems.EventTrigger

{

   public delegate void VoidDelegate(GameObject go);
    public VoidDelegate OnClick;

    public VoidDelegate OnDown;
    public VoidDelegate OnEnter;
    public VoidDelegate OnExit;
    public VoidDelegate OnUp;

   static public EventTrriggerListener Get(GameObject go) { 
    
        EventTrriggerListener listener = go.GetComponent<EventTrriggerListener>();
        if (listener == null) { listener = go.AddComponent<EventTrriggerListener>(); }
        return listener;

    }

    public override void OnPointerClick(UnityEngine.EventSystems.PointerEventData eventData)
    {
        if (OnClick != null) { OnClick(gameObject); }
    }

    public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData)
    {
        if (OnDown != null) { OnDown(gameObject); }
    }

    public override void OnPointerEnter(UnityEngine.EventSystems.PointerEventData eventData)
    {
        if (OnEnter != null) { OnEnter(gameObject); }
    }

    public override void OnPointerExit(UnityEngine.EventSystems.PointerEventData eventData)
    {
        if (OnExit != null)
        {
            OnExit(gameObject);
         }
    }

    public override void OnPointerUp(UnityEngine.EventSystems.PointerEventData eventData)
    {
        if(OnUp!=null){
            OnUp(gameObject);
        }
    }
 

}

 

 

public class MenuBasic:MonoBehaviour

{

    /// <summary>
    /// 点击对象
    /// </summary>

   public GameObject[] Butttons;

 

 protected virtual void Awake() {
        RigesterButtonListenner();
    }

    // 注册按钮事件
    private void RigesterButtonListenner(){
        if(Butttons!=null){
        foreach( GameObject go in Butttons ){
          EventTrriggerListener listener = EventTrriggerListener.Get(go);
          listener.OnClick += this.OnButtonClick;
                listener.OnDown += this.OnButtonDown;
                listener.OnUp += this.OnButtonUp;
        }
        }
    }

 /// <summary>
    /// 点击
    /// </summary>
    /// <param name="gameObject"></param>
    public virtual void OnButtonClick(GameObject gameObject) {}
    /// <summary>
    /// 按下
    /// </summary>
    /// <param name="gameObject"></param>
    public virtual void OnButtonDown(GameObject gameObject) { }
    /// <summary>
    /// 谈起
    /// </summary>
    /// <param name="gameObject"></param>
    public virtual void OnButtonUp(GameObject gameObject) { }

    /// <summary>
    /// 显示
    /// </summary>
    public virtual void Show() { gameObject.SetActive(true); }
    /// <summary>
    /// 隐藏
    /// </summary>
    public virtual void Hide() { gameObject.SetActive(false);  }

}

public class DialogLog :MenuBasic {

    public InputField account, password;

    public GameObject [] accountAndpasswordText;

   public override void OnButtonClick(GameObject gameObject)
    {
        base.OnButtonClick(gameObject);

      

 switch (gameObject.name) {

            case "Btn_Colse":  //UI button按钮名称
                Hide();
                break;
            case "Btn_Cancel": //UI button名称
                log.Show();
                Hide();
                break;
            case "Btn_Yes":

            //发给数据库进行识别
             // HttpFGMySQL._instance.SendMessageMySQL("NameP"+ account.text + "," + password.text+UserMessage.DataNot );
              StartCoroutine(Login(0.1f));
                break;
            case "Btn_ForgetPasswod":
                findpassword.Show();
                break;

 }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值