• Unity UGUI Button扩展


    由于UGUI中的Button组件只能添加Click一种事件有一定的局限性,如果我要做当按钮被按下时并且手指不离开屏幕让按钮缩小如果只用Click事件当手指滑动到其他地方后就没办法恢复到按钮原来的大小了。还比如我没还要检测手指在按钮上滑动的距离这时Button组件明显也不满足需求。所以我就根据UGUI中Button组件的源码写了拥有按下、抬起、离开按钮、开始滑动、停止滑动、滑动等事件的组件代码如下:

      1 using System;
      2 
      3 using UnityEngine;
      4 using UnityEngine.Events;
      5 using UnityEngine.EventSystems;
      6 using UnityEngine.Serialization;
      7 using UnityEngine.UI;
      8 
      9 [AddComponentMenu("UGUIExpend/UITouch", 30)]
     10 public class UITouch : Selectable, IBeginDragHandler, IDragHandler, IEndDragHandler
     11 {
     12     [Serializable]
     13     public class TouchEvent : UnityEvent
     14     {
     15         public PointerEventData  eventData = default;
     16     }
     17 
     18     [SerializeField]
     19     private TouchEvent m_OnTouchDown = new TouchEvent();
     20 
     21     [SerializeField]
     22     private TouchEvent m_OnTouchUp = new TouchEvent();
     23 
     24     [SerializeField]
     25     private TouchEvent m_OnTouchExit = new TouchEvent();
     26 
     27     [SerializeField]
     28     private TouchEvent m_OnBeginSlide = new TouchEvent();
     29 
     30     [SerializeField]
     31     private TouchEvent m_OnSlide = new TouchEvent();
     32 
     33     [SerializeField]
     34     private TouchEvent m_OnEndSlide = new TouchEvent();
     35 
     36     protected UITouch(){}
     37     public TouchEvent OnTouchDown
     38     {
     39         get { return this.m_OnTouchDown; }
     40         set { m_OnTouchDown = value; }
     41     }
     42 
     43     public TouchEvent OnTouchUp
     44     {
     45         get { return this.m_OnTouchUp; }
     46         set { this.m_OnTouchUp = value; }
     47     }
     48 
     49     public TouchEvent OnTouchExit
     50     {
     51         get { return m_OnTouchExit; }
     52         set { m_OnTouchExit = value; }
     53     }
     54 
     55     public TouchEvent OnBeginSlider
     56     {
     57         get { return m_OnBeginSlide; }
     58         set { m_OnBeginSlide = value; }
     59     }
     60 
     61     public TouchEvent OnSlider
     62     {
     63         get { return m_OnSlide; }
     64         set { m_OnSlide = value; }
     65     }
     66     public TouchEvent OnEndSlider
     67     {
     68         get { return m_OnEndSlide; }
     69         set { m_OnEndSlide = value; }
     70     }
     71 
     72     private void touchDownOption(PointerEventData eventData)
     73     {
     74         if(!IsActive() || !IsInteractable())
     75         {
     76             return;
     77         }
     78         UISystemProfilerApi.AddMarker("UITouch.OnTouchDown",this);
     79         m_OnTouchDown.eventData = eventData;
     80         m_OnTouchDown.Invoke();
     81     }
     82 
     83     private void touchExitOption(PointerEventData eventData)
     84     {
     85         if (!IsActive() || !IsInteractable())
     86         {
     87             return;
     88         }
     89         UISystemProfilerApi.AddMarker("UITouch.OnTouchExit", this);
     90         m_OnTouchExit.eventData = eventData;
     91         m_OnTouchExit.Invoke();
     92     }
     93 
     94     private void touchUpOption(PointerEventData eventData)
     95     {
     96         if (!IsActive() || !IsInteractable())
     97         {
     98             return;
     99         }
    100         UISystemProfilerApi.AddMarker("UITouch.OnTouchUp", this);
    101         m_OnTouchUp.eventData = eventData;
    102         m_OnTouchUp.Invoke();
    103     }
    104 
    105     private void slideOption(PointerEventData eventData)
    106     {
    107         if (!IsActive() || !IsInteractable())
    108         {
    109             return;
    110         }
    111         UISystemProfilerApi.AddMarker("UITouch.OnSlider", this);
    112         m_OnSlide.eventData = eventData;
    113         m_OnSlide.Invoke();
    114     }
    115     private void beginSlideOption(PointerEventData eventData)
    116     {
    117         if (!IsActive() || !IsInteractable())
    118         {
    119             return;
    120         }
    121         UISystemProfilerApi.AddMarker("UITouch.OnSlider", this);
    122         m_OnBeginSlide.eventData = eventData;
    123         m_OnBeginSlide.Invoke();
    124     }
    125     private void endSlideOption(PointerEventData eventData)
    126     {
    127         if (!IsActive() || !IsInteractable())
    128         {
    129             return;
    130         }
    131         UISystemProfilerApi.AddMarker("UITouch.OnSlider", this);
    132         m_OnEndSlide.eventData = eventData;
    133         m_OnEndSlide.Invoke();
    134     }
    135     public override void OnPointerDown(PointerEventData eventData)
    136     {
    137         if (eventData.button != PointerEventData.InputButton.Left)
    138         {
    139             return;
    140         }
    141         touchDownOption(eventData);
    142     }
    143     public override void OnPointerUp(PointerEventData eventData)
    144     {
    145         if(eventData.button != PointerEventData.InputButton.Left)
    146         {
    147             return;
    148         }
    149         touchUpOption(eventData);
    150     }
    151     public override void OnPointerExit(PointerEventData eventData)
    152     {
    153         if (eventData.button != PointerEventData.InputButton.Left)
    154         {
    155             return;
    156         }
    157         touchExitOption(eventData);
    158     }
    159 
    160     public void OnBeginDrag(PointerEventData eventData)
    161     {
    162         if (eventData.button != PointerEventData.InputButton.Left)
    163         {
    164             return;
    165         }
    166         beginSlideOption(eventData);
    167         //throw new NotImplementedException();
    168     }
    169 
    170     public void OnDrag(PointerEventData eventData)
    171     {
    172         if (eventData.button != PointerEventData.InputButton.Left)
    173         {
    174             return;
    175         }
    176         slideOption(eventData);
    177     }
    178 
    179     public void OnEndDrag(PointerEventData eventData)
    180     {
    181         if (eventData.button != PointerEventData.InputButton.Left)
    182         {
    183             return;
    184         }
    185         endSlideOption(eventData);
    186     }
    187 }

     

  • 相关阅读:
    第7.10节 Python类中的实例变量定义与使用
    第7.9节 案例详解:Python类封装
    第7.8节 Python中隐秘的类封装方法
    第7.7节 案例详解:Python类继承机制
    第7.6节 Python中类的继承机制详述
    第7.5节 揭开Python类中self的面纱
    第7.4节 Python中与众不同的类
    第7.3节 Python特色的面向对象设计:协议、多态及鸭子类型
    第7.2节 关于面向对象设计的一些思考
    (独孤九剑)--流程控制
  • 原文地址:https://www.cnblogs.com/sy-liu/p/12860034.html
Copyright © 2020-2023  润新知