Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Cursor.SetCursor(Texture2D,CursorMode)

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Sets the mouse cursor to the given texture.

Call this method with a Texture2D to change the appearance of the hardware pointer (mouse cursor).

The cursorMode parameter allows you to use hardware cursors on supported platforms, or force software rendering of the cursor.

In the following example, the mouse cursor is changed to a given texture when OnMouseEnter is called, and reset to default when OnMouseExit is called.

#pragma strict
public var cursorTexture: Texture2D;
public var cursorMode: CursorMode = CursorMode.Auto;
public var hotSpot: Vector2 = Vector2.zero;
function OnMouseEnter() {
	Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
}
function OnMouseExit() {
	Cursor.SetCursor(null, Vector2.zero, cursorMode);
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Texture2D cursorTexture; public CursorMode cursorMode = CursorMode.Auto; public Vector2 hotSpot = Vector2.zero; void OnMouseEnter() { Cursor.SetCursor(cursorTexture, hotSpot, cursorMode); }

void OnMouseExit() { Cursor.SetCursor(null, Vector2.zero, cursorMode); } }

public static method SetCursor(texture: Texture2D, hotspot: Vector2, cursorMode: CursorMode): void;
public static void SetCursor(Texture2D texture, Vector2 hotspot, CursorMode cursorMode);

Parameters

texture The texture to use for the cursor or null to set the default cursor. Note that a texture needs to be imported with "Read/Write enabled" in the texture importer (or using the "Cursor" defaults), in order to be used as a cursor.
hotspot The offset from the top left of the texture to use as the target point (must be within the bounds of the cursor).
cursorMode Allow this cursor to render as a hardware cursor on supported platforms, or force software cursor.

Description

Specify a custom cursor that you wish to use as a cursor.

Call this method with a Texture2D to change the appearance of the hardware pointer (mouse cursor).

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答