平面检测部分
3-箱体矫正-下
问题出现
经过测试我们发现,当我们点击按钮时并不能将柜子旋转一定的角度,因此我们决定并不使用点击按钮的逻辑触发物体旋转,而选择使用长按按钮的逻辑来不断调用旋转方法进而达到旋转的目的
按钮部分
首先我们找到旋转按钮的UI物体
插入我们在前面的博客中提到的脚本click_event
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class click_event : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
public float pressDurationTime = 1;
public bool responseOnceByPress = false;
public float doubleClickIntervalTime = 0.5f;
public UnityEvent onDoubleClick;
public UnityEvent onPress;
public UnityEvent onClick;
private bool isDown = false;
private bool isPress = false;
private float downTime = 0;
private float clickIntervalTime = 0;
private int clickTimes = 0;
void Update()
{
if (isDown)
{
if (responseOnceByPress && isPress)
{
return;
}
downTime += Time.deltaTime;
if (downTime > pressDurationTime)
{
isPress = true;
onPress.Invoke();
}
}
if (clickTimes >= 1)
{
clickIntervalTime += Time.deltaTime;
if (clickIntervalTime >= doubleClickIntervalTime)
{
if (clickTimes >= 2)
{
onDoubleClick.Invoke();
}
else
{
onClick.Invoke();
}
clickTimes = 0;
clickIntervalTime = 0;
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
isDown = true;
downTime = 0;
}
public void OnPointerUp(PointerEventData eventData)
{
isDown = false;
}
public void OnPointerExit(PointerEventData eventData)
{
isDown = false;
isPress = false;
}
public void OnPointerClick(PointerEventData eventData)
{
if (!isPress)
{
//onClick.Invoke();
clickTimes += 1;
}
else
isPress = false;
}
}
回到Unity界面可以看到,在按钮本来拥有的点击事件下方多出了一个组件,拥有长按、双击以及单机的方法
我们这里只使用长按的方法,将Button_click_manager中的click_control脚本的fastRotate方法放入长按事件中
同时注意取消勾选只触发一次的选项