This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

An overview of the

In the development process, in order to beautify the program, it is often necessary to hide the mouse and display it when it needs to be clicked, such as hiding the mouse in the game and displaying the mouse when the backpack is opened. Or changing a mouse style in the application to make the player feel more involved in the game. This article introduces how to hide and display the mouse and change the mouse style in the program.

Mouse shows and hides

In Unity, to control the mouse, we need to use Cursor. We directly through the setting Cursor. Visible property, you can achieve the mouse display and hide state: true display, false hide

// Hide mouse cursor.visible = false;Copy the code

The mouse locking

Usually after hiding the mouse, sometimes we don’t know where the mouse is, which causes us to have to fill the screen to find the mouse when it needs to be displayed. This requires the Cursor. LockState property, which locks the mouse in the center of the screen. It prevents the mouse from being moved out of the game even when it is hidden and having to look for it when it is displayed. Let’s take a look at what the properties of Cursor. LockState are

  1. The mouse locks and disappears
Cursor.lockState = CursorLockMode.Locked;
Copy the code
  1. The mouse unlocks and displays
Cursor.lockState = CursorLockMode.None;
Copy the code
  1. The mouse is confined to the Game view
Cursor.lockState = CursorLockMode.Confined;
Copy the code

The final results are as follows:

Changing the mouse style

There are many ways to change mouse styles in Unity, either by setting them in Unity or by modifying them in code. The premise is to prepare a mouse style picture

Method one, modify the mouse style by setting

This method is simple and takes effect immediately, displaying the changed mouse style directly in Unity’s Game view, whether it is running or not.

First, import the mouse image you need to use. Each image will default to a Texture TypeDefaultFormat orSprite(2D and UI)Format, the first step is to change the image format toCursor, remember to click after the modificationApply. If you do not modify the picture format, modify the mouse style may appear as follows, the mouse picture is not displayedAfter modifying the mouse mouse image format, the next step is to modify the mouse style, inEdit->Project Setting->Player->Default CursorAnd drag and drop the changed image toDefault CursorThe specific position is shown in the figure

The effect is as follows: This method changes only in the Game view, but remains the same everywhere else. And only need to change this one place, very convenient!

Method two, modify the mouse style through the code

The above method is very simple to modify, but the practicality is not very good, the above method can only keep a state after modification, can not achieve a variety of state switching, more to, the next method just can perfect solution. The core code is just one sentence, which is called cursor.setcursor, as shown below

Let’s take a look at the specific parameters of this code.

public static void SetCursor(Texture2D texture, Vector2 hotspot, CursorMode cursorMode);
Copy the code
  1. Texture2D Texture: The cursor image to replace
  2. Vector2 hotspot: Response area (Vector2.zero)
  3. CursorMode CursorMode: render mode

    There are two forms of rendering:
    1. Auto: platform adaptive displayCopy the code
    2. ForceSoftware: Force the use of software cursorsCopy the code

Just call this line of code when you want to change the mouse style and assign the first parameter to the image style you want! Can be the mouse into a state, mouse click a state and so on, waiting for everyone to play freely…

Write in the last

All the shared content are the author in the daily development process used a variety of small function points, share out also in a disguised review, if there is a bad place to write also please give advice. The source code of the Demo will be sorted out and shared later. Welcome to learn from each other and make progress.