Refactor: dedupe input reads, material-property guards, button hooks
Extract byte-identical duplicated code into shared helpers (behaviour-neutral): - New InputUtil.cs: single source for mouse-delta/scroll/cursor-lock, modifier and mouse-button reads, previously copied verbatim across camera controllers, MeshPainter, FirstPersonMode and PlayerCharacterController. - GetMatFloat/SetMatFloat replace the 12x material-instance + HasProperty guard pattern for the camo/height float properties. - Route two inline button hooks through the existing Hook() helper. Bespoke variants deliberately kept local (FreeCam SprintHeld = LeftShift only, MeshPainter.TryGetMouse with out-params). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
626dcc1bdf
commit
882d30797e
11 files changed
+158
-222
No files matched your search
@@ -0,0 +1,107 @@
|
||||
using UnityEngine;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Shared low-level input reads that were previously copy-pasted verbatim across the camera rigs
|
||||
/// (<see cref="FirstPersonController"/>, <see cref="FreeCamController"/>,
|
||||
/// <see cref="ThirdPersonOrbitInput"/>), the paint controller and the first-person shoot mode. Each
|
||||
/// method preserves its original per-backend behaviour (new Input System when present, legacy Input
|
||||
/// Manager otherwise). Sensitivity scaling stays at the call sites.
|
||||
/// </summary>
|
||||
public static class InputUtil
|
||||
{
|
||||
/// <summary>Raw mouse delta this frame (pixels); the legacy path scales GetAxisRaw by 10 to match.</summary>
|
||||
public static Vector2 ReadMouseDelta()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
return Mouse.current != null ? Mouse.current.delta.ReadValue() : Vector2.zero;
|
||||
#else
|
||||
return new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * 10f;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>Raw mouse-wheel delta this frame (usually only its sign is used).</summary>
|
||||
public static float ReadScroll()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
if (Mouse.current != null) return Mouse.current.scroll.ReadValue().y;
|
||||
#endif
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
return Input.GetAxis("Mouse ScrollWheel");
|
||||
#else
|
||||
return 0f;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>Locks+hides or frees+shows the hardware cursor.</summary>
|
||||
public static void SetCursorLocked(bool locked)
|
||||
{
|
||||
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
|
||||
Cursor.visible = !locked;
|
||||
}
|
||||
|
||||
public static bool ShiftHeld()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
var kb = Keyboard.current;
|
||||
if (kb != null) return kb.leftShiftKey.isPressed || kb.rightShiftKey.isPressed;
|
||||
#endif
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool CtrlHeld()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
var kb = Keyboard.current;
|
||||
if (kb != null) return kb.leftCtrlKey.isPressed || kb.rightCtrlKey.isPressed;
|
||||
#endif
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool LeftMouseHeld()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
return Mouse.current != null && Mouse.current.leftButton.isPressed;
|
||||
#else
|
||||
return Input.GetMouseButton(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool RightMouseHeld()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
return Mouse.current != null && Mouse.current.rightButton.isPressed;
|
||||
#else
|
||||
return Input.GetMouseButton(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool MiddleMouseHeld()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
return Mouse.current != null && Mouse.current.middleButton.isPressed;
|
||||
#else
|
||||
return Input.GetMouseButton(2);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>Left mouse button pressed on this exact frame (edge, not held).</summary>
|
||||
public static bool LeftClickedThisFrame()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
return Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame;
|
||||
#else
|
||||
return Input.GetMouseButtonDown(0);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user