在NGUI中按钮连按响应 是需要提前设置的,接下来我们就来看看NGUI中按钮连按响应的实现过程。
Unity对于按钮手势操作有第三方插件FingerGestures。但是考虑到我们项目本身就是用的NGUI,它里面已经封装了按钮响应的处理,虽然没有长按的处理,但似乎是用FingerGestures有点浪费,所以就自己写了以下代码,分享给大家,仅供参考。
这里面定义了两个按钮的长按事件,分别对鼠标和触摸进行了处理。代码中红色标注了以增加按钮为例的主要代码。
- private enum ButtonState
- {
- OnPress,
- OnRelease,
- OnLongPress,
- NONE,
- }
- private bool canLongPress = false;
- private float duration = 0.5f; //长按认证时间,当点击按钮超过此时间时,认为长按//
- private float changeTime = 0.2f; //长按后,数据改变时间间隔//
- private float startTime = 0; //记录开始点击的时间//
- private ButtonState addBtnState = ButtonState.NONE;
- /// <summary>
- /// Determines whether this instance is button hover the specified btn.判断现在是否经过此物体,辅助获取当前点击物体//
- /// </summary>
- /// <returns>
- /// <c>true</c> if this instance is button hover the specified btn; otherwise, <c>false</c>.
- /// </returns>
- /// <param name='btn'>
- /// If set to <c>true</c> button.
- /// </param>
- bool IsButtonHover(GameObject btn)
- {
- if(UICamera.hoveredObject != null && UICamera.hoveredObject.Equals(btn))
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// Raises the touch event. 触摸长按处理函数//
- /// </summary>
- void OnTouch(){
- if(Input.touchCount > 0)
- {//有触摸//
- Touch touch = Input.GetTouch(0); //单点触摸//
- if(touch.phase == TouchPhase.Began)
- { //按下//
- if(IsButtonHover(addBtn)){//点击增加按钮//
- addBtnState = ButtonState.OnPress;//按下状态//
- startTime = Time.time;//记录按下时间,用来判断是否长按//
- }
- else if(IsButtonHover(reduceBtn))
- { //点击减少按钮//
- reduceState = ButtonState.OnPress;
- startTime = Time.time;
- }
- else{
- }
- }
- else if(touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended){//抬起//
- addBtnState = ButtonState.NONE;
- reduceState = ButtonState.NONE;
- }
- else
- {//保持按下状态//
- if(IsButtonHover(addBtn))
- {//点击增加按钮//
- reduceState = ButtonState.NONE;
- if(addBtnState == ButtonState.OnPress)
- { //检测是否为长按事件//
- float deltaTime = Time.time - startTime;
- if(deltaTime >= duration)
- {//当按下的时间超过长按确认时间,表示为长按//
- addBtnState = ButtonState.OnLongPress;
- startTime = Time.time;
- }
- }
- else if(addBtnState == ButtonState.OnLongPress)
- { //长按事件的处理//
- float deltaTime = Time.time - startTime; //事件间隔//
- if(deltaTime >= changeTime)
- { //当按下时间到达处理的时间//
- int curNum = int.Parse(wnd.GetInputText());
- if(curNum < maxNum){
- preNum = curNum + 1;
- wnd.SetInputText(preNum.ToString());
- wnd.SetPriceLabelOnDialog(preNum * price);
- }
- startTime = Time.time;
- }
- }
- }
- else if(IsButtonHover(reduceBtn))
- { //点击减少按钮//
- addBtnState = ButtonState.NONE;
- if(reduceState == ButtonState.OnPress)
- {
- float deltaTime = Time.time - startTime;
- if(deltaTime >= duration){
- reduceState = ButtonState.OnLongPress;
- startTime = Time.time;
- }
- }
- else if(reduceState == ButtonState.OnLongPress)
- {
- float deltaTime = Time.time - startTime;
- if(deltaTime >= changeTime)
- {
- int curNum = int.Parse(wnd.GetInputText());
- if(curNum > 0)
- {
- preNum = curNum - 1;
- wnd.SetInputText(preNum.ToString());
- wnd.SetPriceLabelOnDialog(preNum * price);
- }
- startTime = Time.time;
- }
- }
- }
- else
- {
- addBtnState = ButtonState.NONE;
- reduceState = ButtonState.NONE;
- }
- }
- }
- }
- /// <summary>
- /// Raises the mouse event.鼠标点击长按函数//
- /// </summary>
- void OnMouse(){
- //鼠标//
- if(Input.GetMouseButtonDown(0))
- { //鼠标按下//
- if(IsButtonHover(addBtn))
- {//点击增加按钮//
- addBtnState = ButtonState.OnPress;
- startTime = Time.time;
- }
- else if(IsButtonHover(reduceBtn))
- { //点击减少按钮//
- reduceState = ButtonState.OnPress;
- startTime = Time.time;
- }
- else
- {
- }
- }
- else if(Input.GetMouseButtonUp(0))
- {
- addBtnState = ButtonState.NONE;
- reduceState = ButtonState.NONE;
- }
- else
- {
- if(IsButtonHover(addBtn))
- {//点击增加按钮//
- reduceState = ButtonState.NONE;
- if(addBtnState == ButtonState.OnPress)
- {
- float deltaTime = Time.time - startTime;
- if(deltaTime >= duration)
- {
- addBtnState = ButtonState.OnLongPress;
- startTime = Time.time;
- }
- }
- else if(addBtnState == ButtonState.OnLongPress)
- {
- float deltaTime = Time.time - startTime;
- if(deltaTime >= changeTime)
- {
- int curNum = int.Parse(wnd.GetInputText());
- if(curNum < maxNum)
- {
- preNum = curNum + 1;
- wnd.SetInputText(preNum.ToString());
- wnd.SetPriceLabelOnDialog(preNum * price);
- }
- startTime = Time.time;
- }
- }
- }
- else if(IsButtonHover(reduceBtn))
- { //点击减少按钮//
- addBtnState = ButtonState.NONE;
- if(reduceState == ButtonState.OnPress)
- {
- float deltaTime = Time.time - startTime;
- if(deltaTime >= duration)
- {
- reduceState = ButtonState.OnLongPress;
- startTime = Time.time;
- }
- }
- else if(reduceState == ButtonState.OnLongPress)
- {
- float deltaTime = Time.time - startTime;
- if(deltaTime >= changeTime){
- int curNum = int.Parse(wnd.GetInputText());
- if(curNum > 0){
- preNum = curNum - 1;
- wnd.SetInputText(preNum.ToString());
- wnd.SetPriceLabelOnDialog(preNum * price);
- }
- startTime = Time.time;
- }
- }
- }
- else
- {
- addBtnState = ButtonState.NONE;
- reduceState = ButtonState.NONE;
- }
- }
- }
- void Update()
- {
- //增加,减少按钮长按监听//
- if(canLongPress)
- {
- if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
- {
- OnTouch();
- }
- else
- {
- OnMouse();
- }
- }
- }