H_SafeExperienceDrivingSystem/U3D_DrivingSystem/Assets/Script/SteeringWheelController.cs

102 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // 如果您使用的是UGUI
public class SteeringWheelController : MonoBehaviour
{
public Button[] buttons; // 您的UI按钮
private int selectedButtonIndex = 0; // 当前选中的按钮索引
private bool hasSelected = false; // 是否已经进行了选择
// 假设这是从硬件获取的方向盘角度
private float steeringWheelAngle;
// 方向盘选择角度阈值和中立阈值
public float selectionAngleThreshold = 30.0f; // 例如30度
public float neutralAngleThreshold = 5.0f; // 方向盘接近中立位置的阈值
public float testFloatV;
void Update()
{
// 更新方向盘角度
UpdateSteeringWheelAngle();
// 检查方向盘角度并更新选中的按钮
CheckSteeringWheelAngle();
}
public void SSSS()
{
testFloatV = 45;
}
public void AAAA()
{
testFloatV = 0;
}
public void BBB()
{
testFloatV = -45;
}
private void UpdateSteeringWheelAngle()
{
// 这里需要替换成获取硬件方向盘角度的代码
steeringWheelAngle = testFloatV;
}
private void CheckSteeringWheelAngle()
{
if (Mathf.Abs(steeringWheelAngle) < neutralAngleThreshold)
{
hasSelected = false;
return;
}
if (!hasSelected)
{
if (steeringWheelAngle > selectionAngleThreshold)
{
// 向右转动
SelectNextButton();
hasSelected = true;
}
else if (steeringWheelAngle < -selectionAngleThreshold)
{
// 向左转动
SelectPreviousButton();
hasSelected = true;
}
}
}
private void SelectNextButton()
{
selectedButtonIndex = (selectedButtonIndex + 1) % buttons.Length;
Debug .Log("前进"+selectedButtonIndex);
HighlightButton(selectedButtonIndex);
}
private void SelectPreviousButton()
{
if (selectedButtonIndex == 0)
selectedButtonIndex = buttons.Length - 1;
else
selectedButtonIndex--;
Debug .Log("后退"+selectedButtonIndex);
HighlightButton(selectedButtonIndex);
}
private void HighlightButton(int index)
{
// 根据您的UI系统来高亮选中的按钮
// 例如在UGUI中您可以改变按钮的颜色或添加一个高亮的边框
buttons[index].GetComponent<Button>().onClick.Invoke();
}
}