// ╔════════════════════════════════════════════════════════════════╗ // ║ Copyright © 2025 NWH Coding d.o.o. All rights reserved. ║ // ║ Licensed under Unity Asset Store Terms of Service: ║ // ║ https://unity.com/legal/as-terms ║ // ║ Use permitted only in compliance with the License. ║ // ║ Distributed "AS IS", without warranty of any kind. ║ // ╚════════════════════════════════════════════════════════════════╝ #region using UnityEngine; #endregion namespace NWH.Common.Input { /// /// Utility methods for safe input retrieval with automatic fallback to default keys. /// Prevents errors when Input Manager bindings are missing. /// public class InputUtils { private static int _warningCount; /// /// Attempts to retrieve button state from Input Manager, falls back to KeyCode if binding is missing. /// /// Input Manager button name to query. /// Fallback KeyCode to use if binding is missing. /// Display warning message when falling back to default key. /// True if button is currently held down. public static bool TryGetButton(string buttonName, KeyCode altKey, bool showWarning = true) { try { return UnityEngine.Input.GetButton(buttonName); } catch { // Make sure warning is not spammed as some users tend to ignore the warning and never set up the input, // resulting in bad performance in editor. if (_warningCount < 100 && showWarning) { Debug.LogWarning(buttonName + " input binding missing, falling back to default. Check Input section in manual for more info."); _warningCount++; } return UnityEngine.Input.GetKey(altKey); } } /// /// Attempts to retrieve button press from Input Manager, falls back to KeyCode if binding is missing. /// /// Input Manager button name to query. /// Fallback KeyCode to use if binding is missing. /// Display warning message when falling back to default key. /// True on the frame the button was pressed. public static bool TryGetButtonDown(string buttonName, KeyCode altKey, bool showWarning = true) { try { return UnityEngine.Input.GetButtonDown(buttonName); } catch { if (_warningCount < 100 && showWarning) { Debug.LogWarning(buttonName + " input binding missing, falling back to default. Check Input section in manual for more info."); _warningCount++; } return UnityEngine.Input.GetKeyDown(altKey); } } /// /// Attempts to retrieve axis value from Input Manager, returns 0 if binding is missing. /// /// Input Manager axis name to query. /// Display warning message when axis is missing. /// Axis value between -1 and 1, or 0 if binding is missing. public static float TryGetAxis(string axisName, bool showWarning = true) { try { return UnityEngine.Input.GetAxis(axisName); } catch { if (_warningCount < 100 && showWarning) { Debug.LogWarning(axisName + " input binding missing. Check Input section in manual for more info."); _warningCount++; } } return 0; } /// /// Attempts to retrieve raw axis value from Input Manager, returns 0 if binding is missing. /// Raw axes return only -1, 0, or 1 without smoothing. /// /// Input Manager axis name to query. /// Display warning message when axis is missing. /// Raw axis value (-1, 0, or 1), or 0 if binding is missing. public static float TryGetAxisRaw(string axisName, bool showWarning = true) { try { return UnityEngine.Input.GetAxisRaw(axisName); } catch { if (_warningCount < 100 && showWarning) { Debug.LogWarning(axisName + " input binding missing. Check Input section in manual for more info."); _warningCount++; } } return 0; } } }