x


Custom Mouse Cursor

Is there a code allowing me to add a custom mouse cursor for my game? If so, an example would be nice.

more ▼

asked Apr 15 '11 at 10:43 PM

SpeedySpikes gravatar image

SpeedySpikes
55 6 6 14

Important note: If you are implementing these code snippets listed here as a script object, it should be attached to, the last object added on any scene. Also, make sure its the very last (bottom-most) script in said object.

If you decide to put it anywhere else, Unity3D GUI order of execution applies; your mouse cursor may be rendered before any other GUI call. This potentially causes effects such as "mouse-going-under-button/label", depending on how the rest of your GUI is structured and where your other HUD scripts are.

If you add objects to your scene afterward, and any of the added objects contains a class that makes OnGUI calls, or has a GUIText or GUITexture, you will need to move the mouse cursor replacement script.

Jun 14 '12 at 03:33 AM greatchicken
(comments are locked)
10|3000 characters needed characters left

7 answers: sort newest

I use this one:

var originalCursor : Texture2D;


var cursorSizeX: int = 32;  // set to width of your cursor texture
var cursorSizeY: int = 32;  // set to height of your cursor texture

static var showOriginal : boolean = true;

function Start(){
Screen.showCursor = false;
//Screen.lockCursor = true;
}


function OnGUI(){

    if(showOriginal == true){
        GUI.DrawTexture (Rect(Input.mousePosition.x-cursorSizeX/2 + cursorSizeX/2, (Screen.height-Input.mousePosition.y)-cursorSizeY/2 + cursorSizeY/2, cursorSizeX, cursorSizeY),originalCursor);
    }


}
more ▼

answered Apr 15 '11 at 10:56 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 225 253

Well, -cursorSizeX/2 + cursorSizeX/2 doesn't make much sense :D. If you just use the mouse position the texture will be right down. For arrows that points into the top left that's good, but if you have a centered cursor you should just subtract the half size. Anyways it's quite the easiest way to implement a cursor. +1

Apr 16 '11 at 01:42 AM Bunny83

You shouldn't use Input functions in OnGUI code...use Event.current instead, then you don't have to convert screen coords to gui coords.

Apr 16 '11 at 03:14 AM Eric5h5

I used his code and it worked! Thanks, everyone!

Apr 24 '11 at 08:49 PM SpeedySpikes

Good, now can any of you tell me how I can create an animation that loops and changes the cursor size (Bigger, then smaller, then repeat)?

May 06 '11 at 09:38 PM SpeedySpikes

click on your cursor game object, then click on the animation tab. right click the scale.x under "transform" then choose "add curves". Right click on the scale.x curve and add a second keyframe a few seconds out, then drag that keyframe up a little. Press play, you will see your object get wider as time goes on. Do the same thing for scale.y or scale.z, depending on how your camera is set up. then in the bottom right, change "default" to "loop"

Jun 24 '11 at 04:17 PM christo1745
(comments are locked)
10|3000 characters needed characters left

one question why do you minus screen height

more ▼

answered Jan 03 at 10:03 PM

DrowKiroth gravatar image

DrowKiroth
17 1 1 3

(comments are locked)
10|3000 characters needed characters left

Note these techniques have been rendered obsolete by http://docs.unity3d.com/Documentation/ScriptReference/Cursor.SetCursor.html

more ▼

answered Dec 28 '12 at 09:30 PM

dvochin2 gravatar image

dvochin2
51

(comments are locked)
10|3000 characters needed characters left

IT IS WORK IN CENTER OF CURSOR for you

var originalCursor : Texture2D;


var cursorSizeX: int = 128;  // set to width of your cursor texture
var cursorSizeY: int = 58;  // set to height of your cursor texture

static var showOriginal : boolean = true;

function Start(){
//Screen.showCursor = false;
//Screen.lockCursor = true;
}


function OnGUI(){

    if(showOriginal == true){
        GUI.DrawTexture (Rect(Input.mousePosition.x-cursorSizeX/2 + 1, (Screen.height-Input.mousePosition.y)-cursorSizeY/2 + 1, cursorSizeX, cursorSizeY),originalCursor);
    }


}
more ▼

answered Sep 28 '12 at 04:49 PM

okokok gravatar image

okokok
16 1 2

(comments are locked)
10|3000 characters needed characters left

You could also use the framework: http://edrivenunity.com/cursors

more ▼

answered Jul 11 '12 at 08:49 PM

dkozar gravatar image

dkozar
31 2

lol why is it latin?

Sep 28 '12 at 08:50 PM Hybris
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x199
x197

asked: Apr 15 '11 at 10:43 PM

Seen: 10113 times

Last Updated: Jan 03 at 10:03 PM