zoukankan      html  css  js  c++  java
  • NGUI 事件系统:UICamera

     
    UICamera事实上是对Unity原生的输入系统(UnityEngine.Input)的进一步封装,让开发人员能够直接处理点击、长按、拖动等事件,而不用去关注更底层的输入逻辑(例如鼠标位置、Raycast这些)。
    UICamera的代码看起来很长(将近3000行代码),但实际上做的事情挺单一的。它会在每一次帧更新(Update或lateUpdate)的时候,检测当前Input的各种输入情况,对屏幕做相应的投射(Raycast),筛选出需要接受事件的Collider(EventType),并最终将事件分发到其gameobject挂载的脚本上。
     
    NGUI框架下,如果你想让游戏里面的object接收OnPress、OnClick、OnDrag等这类事件,你需要把UICamera挂在你的主相机上,这就是NGUI的事件系统,包括UIButton,UISlider的实现都是基于UICamera。
     
    事实上,任何挂了BoxCollider的Mono对象都可以监听到OnPress/OnClick事件,那为什么是Mono对象呢?
    因为NGUI是通过GameObject.SendMessage通知对象的。
    关于GameObject.SendMessage可以参考文章:https://www.jianshu.com/p/d314dc7c7777
    static public void Notify (GameObject go, string funcName, object obj)
    {
       if (mNotifying > 10) return;
     
       // Automatically forward events to the currently open popup list
       if (currentScheme == ControlScheme.Controller && UIPopupList.isOpen &&
          UIPopupList.current.source == go && UIPopupList.isOpen)
             go = UIPopupList.current.gameObject;
     
       if (go && go.activeInHierarchy)
       {
          ++mNotifying;
          //if (currentScheme == ControlScheme.Controller)
          // Debug.Log((go != null ? "[" + go.name + "]." : "[global].") + funcName + "(" + obj + ");", go);
          go.SendMessage(funcName, obj, SendMessageOptions.DontRequireReceiver);
          if (mGenericHandler != null && mGenericHandler != go)
             mGenericHandler.SendMessage(funcName, obj, SendMessageOptions.DontRequireReceiver);
          --mNotifying;
       }
    }
    监听NGUI事件方法:挂载BoxCollider,然后在对象上绑定一个mono脚本,加上下面的方法,点击对象就可以打印出OnClick
    void OnClick() {
        GGDebug.Debug("OnClick");
    }
    事件触发流程图:
    RayCast:根据触摸点、EventType计算最终触摸的对象。
    ProcessTouch:UICamera实际关注三个事件输入:Input.GetMouseButton、Input.GetMouseButtonDown、Input.GetMouseButtonUp。这些事件发生时,UICamera会对每个输入事件(左右键、滚轮、多点触摸等)调用ProcessTouch。
    public void ProcessTouch (bool pressed, bool released)
    {
       if (released) mTooltipTime = 0f;
     
       // Whether we're using the mouse
       bool isMouse = (currentScheme == ControlScheme.Mouse);
       float drag   = isMouse ? mouseDragThreshold : touchDragThreshold;
       float click  = isMouse ? mouseClickThreshold : touchClickThreshold;
     
       // So we can use sqrMagnitude below
       drag *= drag;
       click *= click;
     
       if (currentTouch.pressed != null)
       {
          if (released) ProcessRelease(isMouse, drag);
          ProcessPress(pressed, click, drag);
     
          // Hold event = show tooltip
          if (tooltipDelay != 0f && currentTouch.deltaTime > tooltipDelay)
          {
             if (currentTouch.pressed == currentTouch.current && mTooltipTime != 0f && !currentTouch.dragStarted)
             {
                mTooltipTime = 0f;
                //currentTouch.clickNotification = ClickNotification.None;
                if (longPressTooltip) ShowTooltip(currentTouch.pressed);
                Notify(currentTouch.current, "OnLongPress", null);
             }
          }
       }
       else if (isMouse || pressed || released)
       {
          ProcessPress(pressed, click, drag);
          if (released) ProcessRelease(isMouse, drag);
       }
    }
    ProcessPress:处理鼠标(触摸屏)按下事件,代码很长,就不贴了。
    ProcessRelease:处理处理鼠标(触摸屏)抬起事件,代码很长,就不贴了。
  • 相关阅读:
    Java创建和解析Json对象
    Tyche 2191 WYF的递推式
    Tyche 2147 旅行
    Tyche 2317 Color
    洛谷 P1092 虫食算
    洛谷 P3951 小凯的疑惑
    BZOJ 1800 [Ahoi2009]fly 飞行棋
    BZOJ 1034 [ZJOI2008]泡泡堂BNB
    洛谷 P2151 [SDOI2009]HH去散步
    Android开发环境配置
  • 原文地址:https://www.cnblogs.com/wang-jin-fu/p/13509089.html
Copyright © 2011-2022 走看看