Add persistent global mouse sensitivity setting with slider
MouseSensitivity is a static, PlayerPrefs-backed multiplier (default 1, clamped 0.1-4.0) that scales the mouse-look delta in the FreeCam, FirstPerson and ThirdPerson controllers. Writes flush immediately so the value survives a hard build exit. MouseSensitivitySlider binds the Settings-panel slider to it (range read from the inspector) and mirrors the value into its label, syncing on panel open via OnEnable. Main.unity now contains the session-manager canvas directly (the Online Session Manager prefab was unpacked) and wires the slider binder. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
0df37b8e4a
commit
2a6fc163b1
10 files changed
+3097
-2543
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -1,7 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 940219f29a5971947be1767af2ec5a71
|
||||
PrefabImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+3007
-332
File diff suppressed because it is too large.
Load diff
@@ -113,7 +113,7 @@ public class FirstPersonController : MonoBehaviour
|
||||
|
||||
void LookTick()
|
||||
{
|
||||
Vector2 delta = ReadMouseDelta();
|
||||
Vector2 delta = ReadMouseDelta() * MouseSensitivity.Multiplier;
|
||||
_yaw += delta.x * horizontalSensitivity;
|
||||
float vs = verticalSensitivity * (invertY ? 1f : -1f);
|
||||
_pitch = Mathf.Clamp(_pitch + delta.y * vs, -maxPitch, maxPitch);
|
||||
|
||||
@@ -109,7 +109,7 @@ public class FreeCamController : MonoBehaviour
|
||||
|
||||
void LookTick()
|
||||
{
|
||||
Vector2 delta = ReadMouseDelta();
|
||||
Vector2 delta = ReadMouseDelta() * MouseSensitivity.Multiplier;
|
||||
_yaw += delta.x * horizontalSensitivity;
|
||||
float vs = verticalSensitivity * (invertY ? 1f : -1f);
|
||||
_pitch = Mathf.Clamp(_pitch + delta.y * vs, -maxPitch, maxPitch);
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
/// Global mouse look-around sensitivity multiplier, shared by every camera
|
||||
/// controller (FreeCam, FirstPerson, ThirdPersonOrbit). 1 = unchanged baseline.
|
||||
/// Persisted per-machine via PlayerPrefs so the setting survives restarts.
|
||||
public static class MouseSensitivity
|
||||
{
|
||||
public const float Default = 1f;
|
||||
public const float Min = 0.1f;
|
||||
public const float Max = 4f;
|
||||
|
||||
const string PrefKey = "Settings.MouseSensitivity";
|
||||
|
||||
static bool _loaded;
|
||||
static float _multiplier = Default;
|
||||
|
||||
/// Fires whenever the multiplier changes (UI may run before the avatar spawns).
|
||||
public static event Action<float> Changed;
|
||||
|
||||
public static float Multiplier
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_loaded)
|
||||
{
|
||||
_loaded = true;
|
||||
_multiplier = Mathf.Clamp(PlayerPrefs.GetFloat(PrefKey, Default), Min, Max);
|
||||
}
|
||||
return _multiplier;
|
||||
}
|
||||
set
|
||||
{
|
||||
float v = Mathf.Clamp(value, Min, Max);
|
||||
_loaded = true;
|
||||
if (Mathf.Approximately(v, _multiplier)) return;
|
||||
_multiplier = v;
|
||||
PlayerPrefs.SetFloat(PrefKey, v);
|
||||
// Flush to disk now so the setting survives a hard kill (build), not
|
||||
// only a clean quit. Settings changes are rare user actions, so the
|
||||
// extra write is negligible.
|
||||
PlayerPrefs.Save();
|
||||
Changed?.Invoke(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5dc578039be8b9e48a2260d640ffcfc1
|
||||
@@ -0,0 +1,37 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
/// Binds a UI Slider (and optional value label) to the global MouseSensitivity
|
||||
/// multiplier. The slider's range is authored in the inspector (0.1 - 4.0);
|
||||
/// this only reads/writes the value so scene-side range edits are respected.
|
||||
public class MouseSensitivitySlider : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Slider slider;
|
||||
[Tooltip("Optional TMP label showing the current multiplier (e.g. \"1,00\").")]
|
||||
[SerializeField] private TMP_Text valueLabel;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (!slider) return;
|
||||
slider.SetValueWithoutNotify(MouseSensitivity.Multiplier);
|
||||
UpdateLabel(MouseSensitivity.Multiplier);
|
||||
slider.onValueChanged.AddListener(OnSliderChanged);
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if (slider) slider.onValueChanged.RemoveListener(OnSliderChanged);
|
||||
}
|
||||
|
||||
void OnSliderChanged(float value)
|
||||
{
|
||||
MouseSensitivity.Multiplier = value;
|
||||
UpdateLabel(value);
|
||||
}
|
||||
|
||||
void UpdateLabel(float value)
|
||||
{
|
||||
if (valueLabel) valueLabel.text = value.ToString("0.00");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc6da376d2cfebe47beeb9b6012e76dd
|
||||
@@ -103,7 +103,7 @@ public class ThirdPersonOrbitInput : MonoBehaviour
|
||||
|
||||
if (modeWantsLook && !LookSuppressed)
|
||||
{
|
||||
Vector2 delta = ReadMouseDelta();
|
||||
Vector2 delta = ReadMouseDelta() * MouseSensitivity.Multiplier;
|
||||
Vector2 raw = new Vector2(
|
||||
delta.x * horizontalSensitivity,
|
||||
delta.y * verticalSensitivity * (invertY ? 1f : -1f));
|
||||
|
||||
Reference in new issue
Block a user