unity basic course: hide mouse cursor

In this tutorial, we will learn how to hide and control the mouse cursor in Unity. Hiding and controlling the mouse cursor during the development of games or simulations is an important technique that helps enhance user experience. This is particularly essential when managing interactions in FPS (first-person shooter) games or with specific UI elements.

1. Overview

Hiding the mouse cursor is a straightforward process in Unity. This can increase the user’s immersion, allowing them to focus more on specific elements on the screen. To hide the cursor and implement certain functionalities, some basic script modifications and UI settings are required.

1.1. What to Expect?

Through this tutorial, we aim to achieve the following goals:

  • Understanding how to hide the mouse cursor in Unity
  • Managing mouse state with event triggers
  • Creating a user interface for cursor control
  • Adding advanced features using scripts

2. Setting Up Unity Environment

First, we need to set up a Unity project. This will proceed under the assumption that Unity is already installed.

2.1. Creating a New Project

Open Unity Hub and click the ‘New Project’ button. Here are the basic settings:

  • Project Name: MouseCursorHiding
  • Template Selection: 3D
  • Location: Set your desired location.

Then click the ‘Create’ button to generate the project.

2.2. Setting Up the Initial Scene

To set up the scene, we will add a simple 3D object. For example, add a cube or a plane.

  • In the Hierarchy window, select Right Click > 3D Object > Cube to add a cube.
  • Adjust the size of the cube to a suitable dimension and position it at the center.

3. Writing the Basic Script

We need to write a script to hide the mouse cursor. We will use C# for this.

3.1. Creating the Script

The next step is to create a new C# script.

  • In the Project window, select Right Click > Create > C# Script to create a new script.
  • Name the script CursorController.
  • Double-click on the newly created script to open it in a code editor.

3.2. Writing the Code

The code to write is as follows:

using UnityEngine;

public class CursorController : MonoBehaviour
{
    void Start()
    {
        // Hide the cursor.
        Cursor.visible = false;
        // Set the cursor lock state to center.
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        // Change the cursor state when the mouse button is clicked.
        if (Input.GetMouseButtonDown(0))
        {
            ToggleCursor();
        }
    }

    void ToggleCursor()
    {
        // Show or hide based on the current cursor state.
        if (Cursor.visible)
        {
            Cursor.visible = false;
            Cursor.lockState = CursorLockMode.Locked;
        }
        else
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }
    }
}

The above script hides the cursor and locks it in place when the game starts. When the user clicks the mouse button, it toggles the visibility of the cursor.

4. Applying the Script

You need to apply the written script to the scene.

  • Select Cube from the Hierarchy.
  • In the Inspector window, click the Add Component button and search for CursorController to add it.

Now, when you run the scene, you will see that the mouse cursor is hidden. Clicking the mouse button will make the cursor reappear.

5. Adding Advanced Features

After implementing the basic cursor hiding functionality, you can develop additional advanced features.

5.1. Integration with UI Elements

When interacting with the in-game UI, there may be a need to show the cursor. You can make it so the cursor appears when clicking on the UI.

To do this, add UI click detection to the ToggleCursor method:

using UnityEngine;
using UnityEngine.EventSystems;

public class CursorController : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ToggleCursor();
        }
    }

    void ToggleCursor()
    {
        if (Cursor.visible)
        {
            Cursor.visible = false;
            Cursor.lockState = CursorLockMode.Locked;
        }
        else
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Cursor.visible = true;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }
}

This will enable the cursor to be visible when hovering over UI elements and hide again when it leaves the UI.

6. Conclusion and Final Testing

Once all the above steps are completed, it’s time to build the project and conduct testing. Make sure all functions work correctly and make adjustments as needed.

6.1. Build Settings

  • In the top menu, click File > Build Settings.
  • Select the platform and click the Switch Platform button.
  • Add the necessary scenes and click the Build button to create the executable file.

6.2. Final Result Check

Run the generated executable to check if the mouse cursor hides properly, and confirm that it interacts correctly with other UI elements.

Conclusion

In this tutorial, we learned how to hide the mouse cursor in Unity. We implemented various features, from writing basic scripts to integrating with UI elements. These skills are highly useful in the development of various games and applications, so make sure to utilize them fully.

I hope you continue to gain more professional skills through various Unity-related tutorials. Thank you!