E_ElecCompetition/Electrical_inspectionCompet.../Assets/Script/MyFrameworkPure/Tool/GestureTool.cs

102 lines
2.8 KiB
C#

using UnityEngine;
using UnityEngine.Events;
//using Pvr_UnitySDKAPI;
namespace MyFrameworkPure
{
public class UnityEventFloat : UnityEvent<float>
{
}
public class GestureTool : CSingletonMono<GestureTool>
{
public float HThreshold { get; set; }//水平阈值
public float VThreshold { get; set; }//垂直阈值
public UnityEvent<float> onHorizontalMove = new UnityEventFloat();
public UnityEvent<float> onVerticalMove = new UnityEventFloat();
public float mouseSpeed = 20;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
#if UNITY_EDITOR || UNITY_STANDALONE
if (!Input.GetMouseButton(0))
return;
float deltaX = Input.GetAxis("Mouse X") * mouseSpeed;
float deltaY = Input.GetAxis("Mouse Y") * mouseSpeed;
if (Input.touchCount > 0)
{
deltaX = Input.touches[0].deltaPosition.x;
deltaY = Input.touches[0].deltaPosition.y;
}
Vector2 mousePos = Input.mousePosition;
if (mousePos.x <= 0)
{
deltaX = -mouseSpeed * 0.5f;
}
else if (mousePos.x >= Screen.width)
{
deltaX = mouseSpeed * 0.5f;
}
if (mousePos.y <= 0)
{
deltaY = -mouseSpeed * 0.5f;
}
else if (mousePos.y >= Screen.height)
{
deltaY = mouseSpeed * 0.5f;
}
if (Mathf.Abs(deltaX) > HThreshold)
{
onHorizontalMove?.Invoke(deltaX);
}
if (Mathf.Abs(deltaY) > VThreshold)
{
onVerticalMove?.Invoke(deltaY);
}
#endif
#if PICO
int mainHandNess = Pvr_UnitySDKAPI.Controller.UPvr_GetMainHandNess();
if (Controller.UPvr_GetControllerState(mainHandNess) != ControllerState.Connected)
return;
bool pressTouchpad = Controller.UPvr_GetKey(mainHandNess, Pvr_KeyCode.TOUCHPAD);
if (!pressTouchpad)
return;
Vector2 touchPos = Controller.UPvr_GetAxis2D(mainHandNess);
if (Mathf.Abs(touchPos.x) > Mathf.Abs(touchPos.y))
{
touchPos.y = 0;
touchPos.x = Mathf.Sign(touchPos.x)*5;
onHorizontalMove?.Invoke(touchPos.x);
}
else if (Mathf.Abs(touchPos.x) < Mathf.Abs(touchPos.y))
{
touchPos.y = Mathf.Sign(touchPos.y)*5;
touchPos.x = 0;
onVerticalMove?.Invoke(touchPos.y);
}
#endif
}
}
}