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
5
Question by StepW · Jan 13, 2015 at 06:43 PM · buttonmousecursorhoverhand

Changing cursor on mouse hover?

I'll keep this quick and simple. I'm coding a button, and I want the cursor to change to a hand when it's moved onto this button. I can't quite figure it out. This is my code. I initialise the cursor texture to the cursor.jpg image I have in my Assets > Resources folder, and then I use Cursor.SetCursor to change the cursor. Maybe I'm doing something dumb. Any help would be greatly appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class ButtonBehaviour : MonoBehaviour
 {
     Texture2D cursor;
     
     void Start ()
     {
         cursor = (Texture2D)Resources.Load ("cursor.jpg");
     }
     
     void OnMouseOver()
     {
         Cursor.SetCursor (cursor, Vector2.zero, CursorMode.Auto);
     }
     
     void OnMouseExit()
     {
         Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
     }
     
     void Update ()
     {
         
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by prefix · Dec 26, 2015 at 11:18 AM

Just be sure to change the texture type in the inspector to "cursor". Im not sure why you are initializing it when you can use it publicly. However if you want to keep it private, you can [Serializefield] and still manipulate the variable within the inspector.

   public class ButtonBehaviour : MonoBehaviour
   {
 
 //if you want it private do:
 [Serializefield] 
 Texture2D cursor;
 
 //Otherwise you can do it publicly.  
    public Texture2D cursor;
       
         
       void OnMouseEnter()
       {
           Cursor.SetCursor (cursor, Vector2.zero, CursorMode.Auto);
       }
       
       void OnMouseExit()
       {
           Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
       }
   
   }

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 SeveneduS · Sep 01, 2017 at 11:54 AM 0
Share

Please replace [Serializefield] with [SerializeField] in the code snippet.

avatar image Hauthorn · Apr 23, 2019 at 01:01 PM 0
Share

This isn't any different than what the op is trying, $$anonymous$$us where the cursor asset is co$$anonymous$$g from...

avatar image
0

Answer by Fdo_decea · Jul 07, 2018 at 08:28 PM

Any idea of how to make that change smoothly ?? I've tried with solutions too dirty and huge , trying to make a fade-out effect. There is also the problem that the textures have not alpha channel, so I have to go pixel to pixel fading them out. I would like to fade in, and fade out, using the default cursor and a custom one.

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

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

6 People are following this question.

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

Related Questions

Cursor appearance change when mouse over a GUI button 2 Answers

How to make a Hover event on GUI.Button 6 Answers

GUI.Button on MouseHover 1 Answer

Cursor Disappears In Scene View When Play Mode Is On 2 Answers

Game runs on one screen, cursor wanders onto other screens? 0 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