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 voted first

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

See the code here.

more ▼

answered Apr 16 '11 at 12:15 AM

Eric5h5 gravatar image

Eric5h5
80.2k 41 132 519

Nice features. Using this, thanks.

Jul 14 '12 at 07:36 PM iMugen
(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

i use this C#(C sharp) script

using UnityEngine; using System.Collections;

public class mousePointer : MonoBehaviour { public Texture2D cursorImage;

private int cursorWidth = 32;
private int cursorHeight = 32;

void Start()
{
    Screen.showCursor = false;
}


void OnGUI()
{
    GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);
}

}

more ▼

answered Jun 24 '11 at 02:41 PM

mirw9 gravatar image

mirw9
16 1 1 3

(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
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
x198

asked: Apr 15 '11 at 10:43 PM

Seen: 10159 times

Last Updated: Jan 03 at 10:03 PM