Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by CJCurrie · Jul 26, 2010 at 04:12 AM · cursorcursor-customization

How do I change the hardware cursor?

I know how to use UnityGUI and DrawTexture to draw a cursor at Input.mousePosition, but it lags behind the real cursor too much when the framerate gets low. How can I change the hardware cursor (the one that is rendered independently of the memory allocated to the program)? On windows I would use the System.Windows.Input.Mouse.SetCursor() method, but I don't think this works in Unity (it uses Windows libraries).

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · Jul 26, 2010 at 06:47 AM

You can't change the hardware cursor in Unity.

Edit: in Unity 3, that is. You can in Unity 4.

Comment
Add comment · Show 2 · 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 CJCurrie · Jul 27, 2010 at 03:02 PM 0
Share

This is true. However, there may be more options, perhaps within the $$anonymous$$ono framework.

avatar image No_Username_Found · Aug 02, 2014 at 01:28 AM 0
Share

You can use Cursor.SetCursor(). See my answer below and the documentation here.

avatar image
4

Answer by MattijsKneppers · Apr 16, 2019 at 08:00 AM

In case you landed here trying to change the cursor to another standard/native cursor, so not a custom texture but one available in the OS by default, here is what I ended up doing (Windows only!):

   public enum WindowsCursor
     {
         StandardArrowAndSmallHourglass = 32650,
         StandardArrow = 32512,
         Crosshair = 32515,
         Hand = 32649,
         ArrowAndQuestionMark = 32651,
         IBeam = 32513,
         //Icon = 32641, // Obsolete for applications marked version 4.0 or later. 
         SlashedCircle = 32648,
         //Size = 32640,  // Obsolete for applications marked version 4.0 or later. Use FourPointedArrowPointingNorthSouthEastAndWest
         FourPointedArrowPointingNorthSouthEastAndWest = 32646,
         DoublePointedArrowPointingNortheastAndSouthwest = 32643,
         DoublePointedArrowPointingNorthAndSouth = 32645,
         DoublePointedArrowPointingNorthwestAndSoutheast = 32642,
         DoublePointedArrowPointingWestAndEast = 32644,
         VerticalArrow = 32516,
         Hourglass = 32514
     }
 
     private static void ChangeCursor(WindowsCursor cursor){
         SetCursor(LoadCursor(IntPtr.Zero , (int)cursor));
     }
 
     [DllImport("user32.dll", EntryPoint = "SetCursor")]
     public static extern IntPtr SetCursor(IntPtr hCursor);
 
     [DllImport("user32.dll", EntryPoint = "LoadCursor")]
     public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

Comment
Add comment · Show 3 · 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 Pangamini · Apr 16, 2019 at 08:46 AM 0
Share

At first I was like "Wtf, 9 year old question" but this is pretty useful

avatar image Bunny83 Pangamini · Apr 16, 2019 at 10:36 AM 0
Share

Yes, true, but keep in $$anonymous$$d that this only works for windows builds. When you upvote this answer you should also upvote the answer by "No_Username_Found" as this answer is actually crossplatform.

avatar image MattijsKneppers Bunny83 · Apr 16, 2019 at 11:59 AM 0
Share

I added an extra callout that this only works on Windows.

Note though that my answer covers a different issue than the one addressed by No_Username_Found: I needed to use an anternative STANDARD windows cursor, not a Texture2D. The reason I posted here is that I expect people to end up at this thread when searching for the specific issue I was dealing with.

avatar image
2

Answer by Senhor de todo o Mal · Oct 20, 2010 at 12:32 PM

If its for a Windows build you can try using user32.dll:

</p> <pre><code>[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern IntPtr SetCursor(IntPtr hCursor); [DllImport("user32.dll")] public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName); [DllImport("user32.dll")] public static extern IntPtr LoadImage( IntPtr hInstance, string lpImageName, uint uType, int cxDesired, int cyDesired, uint fuLoad ); </code></pre> <p>

And write some methods that you can call from your scripts:

</p> <pre><code>public static void changeCursorToHand(){ SetCursor(LoadCursor(IntPtr.Zero ,32649)); } public static void changeCursorToArrow(){ SetCursor(LoadCursor(IntPtr.Zero ,32512)); } </code></pre> <p>

Comment
Add comment · Show 5 · 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 CJCurrie · Oct 21, 2010 at 06:39 AM 0
Share

This looks valid... But how do we do platform-based compile-time instructions? I remember a few weeks ago hearing that Unity 3 would have the ability for it. $compilerinstructions didn't work in the last C# implementation Unity used, and I'm not sure if that's been fixed yet.

Anyone know a mac solution, too?

avatar image Senhor de todo o Mal · Oct 21, 2010 at 01:15 PM 0
Share

http://unity3d.com/support/documentation/$$anonymous$$anual/Platform%20Dependent%20Compilation.html - Check this link for platform dependent compilation. I don't know of a mac solution, it's probably something similar, but using the equivalent to user32.dll

avatar image roamcel · Sep 02, 2011 at 03:40 PM 0
Share

@Senhordetodoo$$anonymous$$al do you know if there is an alternative to your great example for unity free version? Apparently DllImport is a pro only feature :/

avatar image eanton_KickBack · Nov 06, 2012 at 08:50 PM 0
Share

I know this is an old post but still the problem persists and yours is I think the only solution that works without any external 3rd party dlls. The problem is that with my current version of Unity (3.5.5) the cursor changes back to the original whenever I move the mouse. Am I doing something wrong? Do you know if unity is the problem and if so any workarounds. Thanks a lot!

avatar image Eric5h5 · Nov 06, 2012 at 09:07 PM 0
Share

A good "workaround" is to use Unity 4, which has native support for hardware cursors.

avatar image
0

Answer by No_Username_Found · Aug 02, 2014 at 01:26 AM

Though this question is old, perhaps this answer will help someone else.

The simplest way to do this is to have a Texture2D replace the default OS cursor. This is not a Pro-Only feature and is not limited to Windows only. This is the code I used:

     public Texture2D customCursor;
     private CursorMode cursorMode;
     private Vector2 hotSpot;
 
 
     // Use this for initialization
     void Start () {
 
         //change the cursor texture
         cursorMode = CursorMode.ForceSoftware;
         hotSpot = new Vector2 (0, 0);
         Cursor.SetCursor (customCursor, hotSpot, cursorMode);
     }

Please be aware that when you are testing this, you will have to BUILD AND RUN your project to see it work. It will not change the cursor if you just press the play button in Unity.

There is more documentation available here if you want to see about having the cursor change as you mouse over other objects.

Comment
Add comment · 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
-1

Answer by qJake · Jul 26, 2010 at 05:46 AM

If the cursor is lagging, the rest of your game must be, too (that, or your mouse cursor code is inefficient - maybe you should post it here). The only way I've discovered of drawing a cursor is via UnityGUI (of course there are more inefficient ways then UnityGUI, but none more efficient, as far as I can tell).

Comment
Add comment · 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 CJCurrie · Jul 27, 2010 at 03:04 PM 0
Share

The game runs at ~40fps at full graphics, and the GUI code uses <6%, according to the profiler. It's just a little bit of lag, but that lag gets really annoying. It's an $$anonymous$$$$anonymous$$O, and as the number of users increases the lag does, too, which makes it even worse.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Keep Cursor at center and project interactions to cursor position on mobile touch devices (IOS) 0 Answers

How do I change the cursor without a separate Texture2D? 0 Answers

Multiple Cursors - WebGL Unity 5.6 2 Answers

Cursor / MouseOver Glitch 0 Answers

Changing Color of Curser 2 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