- Home /
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.
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.
Answer by oliver-jones · Apr 15, 2011 at 10:56 PM
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);
}
}
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
You shouldn't use Input functions in OnGUI code...use Event.current ins$$anonymous$$d, then you don't have to convert screen coords to gui coords.
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)?
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"
Answer by dvochin2 · Dec 28, 2012 at 09:30 PM
Note these techniques have been rendered obsolete by http://docs.unity3d.com/Documentation/ScriptReference/Cursor.SetCursor.html
Answer by mirw9 · Jun 24, 2011 at 02:41 PM
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);
}
}
How would you center the cursor image ins$$anonymous$$d of having it on the top left.
Answer by dkozar · Jul 11, 2012 at 08:49 PM
You could also use the framework: http://edrivenunity.com/cursors
Your answer
Follow this Question
Related Questions
UNITY3D: Custom cursors? 3 Answers
Unity 3D Screen.lockCursor Problems 2 Answers
custom mouse changing when hovering over objects 1 Answer
Custom Text Cursor Texture 1 Answer
How to center custom cursor 2 Answers