How To Disable Mouse Input UI In Unity Tutorial | Unity UI Input System
In this tutorial, you'll learn how to disable mouse input for the UI in Unity, preventing any unwanted interactions. We'll guide you step-by-step through the process, covering both simple and advanced techniques, so you can control your game's UI behavior more effectively. Perfect for developers looking to manage input in specific scenarios!
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
3 Unity Projects Only $50 - ON SALE!!!!
Contact: unitycoderz2022@gmail.com
=========================================================
Script:
DisableMouseInput.cs
using UnityEngine;
using UnityEngine.EventSystems;
public class DisableMouseInput : MonoBehaviour
{
// Toggle in the Inspector to enable/disable mouse input
[Header("Mouse Input Control")]
public bool disableMouseInput = false;
private StandaloneInputModule inputModule;
void Start()
{
// Get the StandaloneInputModule component attached to the EventSystem
inputModule = FindObjectOfType<EventSystem>().GetComponent<StandaloneInputModule>();
// Ensure it is found in the scene
if (inputModule == null)
{
Debug.LogError("StandaloneInputModule not found. Please ensure your EventSystem has a StandaloneInputModule.");
}
// Update the input module state based on the initial value of the toggle
UpdateInputModuleState();
}
void Update()
{
// Update the input module state every frame based on the toggle value
if (inputModule != null)
{
// If the toggle value changes, update the input module's enabled state
if (disableMouseInput != inputModule.enabled)
{
UpdateInputModuleState();
}
}
}
// Method to enable or disable the input module based on the toggle
private void UpdateInputModuleState()
{
if (inputModule != null)
{
inputModule.enabled = !disableMouseInput;
}
}
}