1
0
mirror of https://github.com/xiaopeng12138/MaiDXR.git synced 2024-12-14 10:32:50 +01:00
MaiDXR/Assets/Scripts/Configs/PlayerSettingManager.cs

198 lines
6.9 KiB
C#
Raw Normal View History

2022-08-18 00:50:50 +02:00
using UnityEngine.UI;
using UnityEngine;
2022-08-22 04:21:51 +02:00
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
2022-08-18 00:50:50 +02:00
public class PlayerSettingManager : MonoBehaviour
{
2022-08-22 04:21:51 +02:00
private Transform LHandTransform = null;
private Transform RHandTransform = null;
private Transform PlayerTransform = null;
private ControllerHapticManager[] HapticManagers = null;
public ValueManager PlayerHeightManager;
public float HandSize = 8;
public float HandPositionX = 0;
public float HandPositionY = 0;
public float HandPositionZ = 0;
public List<Slider> Sliders;
2022-08-18 00:50:50 +02:00
void Start()
{
2022-08-22 04:21:51 +02:00
SetTarget(gameObject);
SetSliders();
}
public void SetTarget(GameObject XRObj)
{
LHandTransform = XRObj.transform.Find("Camera Offset").Find("LeftHand Controller").Find("LHand");
RHandTransform = XRObj.transform.Find("Camera Offset").Find("RightHand Controller").Find("RHand");
PlayerTransform = XRObj.transform;
HapticManagers = GetComponentsInChildren<ControllerHapticManager>();
GetSetConfigs();
}
private void GetSetConfigs()
{
GetPlayerHeight();
GetHandSize();
GetHandPositionX();
GetHandPositionY();
GetHandPositionZ();
GetHapticDuration();
GetHapticAmplitude();
}
private void SetSliders()
{
foreach (Slider slider in Sliders)
2022-08-18 00:50:50 +02:00
{
2022-08-22 04:21:51 +02:00
switch (slider.gameObject.name)
{
case "HandS":
slider.value = HandSize;
break;
case "HandX":
slider.value = HandPositionX;
break;
case "HandY":
slider.value = HandPositionY;
break;
case "HandZ":
slider.value = HandPositionZ;
break;
case "HpDuration":
slider.value = HapticManagers[0].duration;
break;
case "HpAmplitude":
slider.value = HapticManagers[0].amplitude;
break;
}
2022-08-18 00:50:50 +02:00
}
}
2022-08-22 04:21:51 +02:00
private void GetPlayerHeight()
2022-08-18 00:50:50 +02:00
{
if (PlayerConfig.HasKey("PlayerHeight"))
PlayerHeightManager.Value = (float)PlayerConfig.GetDouble("PlayerHeight");
2022-08-18 00:50:50 +02:00
SetPlayerHeight();
}
2022-08-22 04:21:51 +02:00
private void GetHandSize()
2022-08-18 00:50:50 +02:00
{
if (PlayerConfig.HasKey("HandSize"))
HandSize = (float)PlayerConfig.GetDouble("HandSize");
2022-08-22 04:21:51 +02:00
SetHandSize(HandSize);
2022-08-18 00:50:50 +02:00
}
2022-08-22 04:21:51 +02:00
private void GetHandPositionX()
2022-08-18 00:50:50 +02:00
{
if (PlayerConfig.HasKey("HandPositionX"))
HandPositionX = (float)PlayerConfig.GetDouble("HandPositionX");
2022-08-22 04:21:51 +02:00
SetHandPositionX(HandPositionX);
2022-08-18 00:50:50 +02:00
}
2022-08-22 04:21:51 +02:00
private void GetHandPositionY()
2022-08-18 00:50:50 +02:00
{
if (PlayerConfig.HasKey("HandPositionY"))
HandPositionY = (float)PlayerConfig.GetDouble("HandPositionY");
2022-08-22 04:21:51 +02:00
SetHandPositionY(HandPositionY);
2022-08-18 00:50:50 +02:00
}
2022-08-22 04:21:51 +02:00
private void GetHandPositionZ()
2022-08-18 00:50:50 +02:00
{
if (PlayerConfig.HasKey("HandPositionZ"))
HandPositionZ = (float)PlayerConfig.GetDouble("HandPositionZ");
2022-08-22 04:21:51 +02:00
SetHandPositionZ(HandPositionZ);
2022-08-18 00:50:50 +02:00
}
2022-08-22 04:21:51 +02:00
void GetHapticDuration()
{
if (PlayerConfig.HasKey("HapticDuration"))
HapticManagers[0].duration = (float)PlayerConfig.GetDouble("HapticDuration");
2022-08-22 04:21:51 +02:00
SetHapticDuration(HapticManagers[0].duration);
}
void GetHapticAmplitude()
{
if (PlayerConfig.HasKey("HapticAmplitude"))
HapticManagers[0].amplitude = (float)PlayerConfig.GetDouble("HapticAmplitude") * 10;
2022-08-22 04:21:51 +02:00
SetHapticAmplitude(HapticManagers[0].amplitude);
}
2022-08-18 00:50:50 +02:00
public void SetPlayerHeight()
{
2022-08-22 04:21:51 +02:00
PlayerTransform.position = new Vector3(PlayerTransform.position.x, PlayerHeightManager.Value, PlayerTransform.position.z);
PlayerConfig.SetDouble("PlayerHeight", PlayerHeightManager.Value);
2022-08-18 00:50:50 +02:00
}
public void SetHandSize(float value)
{
PlayerConfig.SetDouble("HandSize", value);
2022-08-18 00:50:50 +02:00
value = value / 100;
LHandTransform.localScale = new Vector3(value, value, value);
RHandTransform.localScale = new Vector3(value, value, value);
}
public void SetHandPositionX(float value)
{
PlayerConfig.SetDouble("HandPositionX", value);
2022-08-18 00:50:50 +02:00
value = value / 100;
LHandTransform.localPosition = new Vector3(value, LHandTransform.localPosition.y, LHandTransform.localPosition.z);
RHandTransform.localPosition = new Vector3(-value, RHandTransform.localPosition.y, RHandTransform.localPosition.z);
}
public void SetHandPositionY(float value)
{
PlayerConfig.SetDouble("HandPositionY", value);
2022-08-18 00:50:50 +02:00
value = value / 100;
LHandTransform.localPosition = new Vector3(LHandTransform.localPosition.x, value, LHandTransform.localPosition.z);
RHandTransform.localPosition = new Vector3(RHandTransform.localPosition.x, value, RHandTransform.localPosition.z);
}
public void SetHandPositionZ(float value)
{
PlayerConfig.SetDouble("HandPositionZ", value);
2022-08-18 00:50:50 +02:00
value = value / 100;
LHandTransform.localPosition = new Vector3(LHandTransform.localPosition.x, LHandTransform.localPosition.y, value);
RHandTransform.localPosition = new Vector3(RHandTransform.localPosition.x, RHandTransform.localPosition.y, value);
}
2022-08-22 04:21:51 +02:00
public void SetHapticDuration(float duration)
{
foreach (var controller in HapticManagers)
{
controller.duration = duration;
}
PlayerConfig.SetDouble("HapticDuration", duration);
2022-08-22 04:21:51 +02:00
}
public void SetHapticAmplitude(float amplitude)
{
amplitude /= 10;
foreach (var controller in HapticManagers)
{
controller.amplitude = amplitude;
}
PlayerConfig.SetDouble("HapticAmplitude", amplitude);
}
private static class PlayerConfig
{
static JObject playerConfig;
public static bool hasInitialized = false;
private static void ensureInitialization() {
if (hasInitialized)
return;
GetPlayerConfig();
hasInitialized = true;
}
public static bool HasKey(string key) {
ensureInitialization();
return playerConfig.ContainsKey(key);
}
public static void SetDouble(string key, double number) {
ensureInitialization();
playerConfig[key] = number;
SetPlayerConfig();
}
public static double GetDouble(string key) {
ensureInitialization();
return playerConfig.Value<double>(key);
}
public static void SetPlayerConfig() {
JsonConfig.SetJObject("PlayerConfig", playerConfig);
}
public static void GetPlayerConfig() {
if (JsonConfig.HasKey("PlayerConfig"))
playerConfig = JsonConfig.GetJObject("PlayerConfig");
else
playerConfig = new JObject();
}
2022-08-22 04:21:51 +02:00
}
2022-08-18 00:50:50 +02:00
}