• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by SpeedySpikes · Apr 15, 2011 at 10:43 PM · cursorcustom

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.

Comment
maraoz

People who like this

1 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image greatchicken · Jun 14, 2012 at 03:33 AM 0
Share

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.

6 Replies

  • Sort: 
avatar image
Best Answer

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);
 }


}

Comment
Bunny83
Joshua
SpeedySpikes
Barrett-Fox
hardRock
maraoz
seckincengiz

People who like this

7 Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 16, 2011 at 01:42 AM 2
Share

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

avatar image Eric5h5 · Apr 16, 2011 at 03:14 AM 0
Share

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.

avatar image SpeedySpikes · Apr 24, 2011 at 08:49 PM 0
Share

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

avatar image SpeedySpikes · May 06, 2011 at 09:38 PM 0
Share

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)?

avatar image christo1745 · Jun 24, 2011 at 04:17 PM 0
Share

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"

Show more comments
avatar image

Answer by Eric5h5 · Apr 16, 2011 at 12:15 AM

See the code here.

Comment
Bunny83
flamy
iMugen

People who like this

3 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iMugen · Jul 14, 2012 at 07:36 PM 0
Share

Nice features. Using this, thanks.

avatar image

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

Comment
Eric5h5
edwardrowe

People who like this

2 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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);
 }

}

Comment
roberto.guzman

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Keegz · May 28, 2013 at 10:03 AM 0
Share

How would you center the cursor image instead of having it on the top left.

avatar image

Answer by dkozar · Jul 11, 2012 at 08:49 PM

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

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hybris · Sep 28, 2012 at 08:50 PM 0
Share

lol why is it latin?

  • 1
  • 2
  • ›

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

custom mouse changing when hovering over objects 1 Answer

Custom Text Cursor Texture 1 Answer

How to center custom cursor 2 Answers

How can I add cursor acceleration? 0 Answers

UNITY3D: Custom cursors? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges