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
0
Question by Stealthygolem · Feb 03, 2015 at 02:57 PM · texturespritetexture2dcursorcast

Using Sprite for Custom mouse Cursor using SetCursor

Hello! I've checked around for how to use a custom mouse cursor, I've gotten in to work with the Texture2D which it implies that it needs using this code:

     CursorMode cursorThing;
     Texture2D cursorText;
 
     void Start() {
         cursorThing = CursorMode.Auto;
         cursorText = new Texture2D(128, 128, TextureFormat.ARGB32, false);
         renderer.material.mainTexture = cursorText;
     }
 
     void Update () {
         Cursor.SetCursor(cursorText, Vector2.zero, cursorThing);
     }
         

Is there any way that I could cast this into a sprite instead? This way I wouldn't have to work around adding another texture thingy for working with the same image that already is a sprite in my asset folder. Any tips on implementing this would be highgly appreciated :)

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
0
Best Answer

Answer by Stealthygolem · Feb 03, 2015 at 06:17 PM

For anyone interested, I figured it out myself by looking through some other threads that were answered in a similar way. This is how I ended up converting my sprite to a texture.

Firstly, you have to go to your Sprite in your asset folder. Change the "Texture Type" to "Advanced". Make sure "Read/Write Enabled" is checked. Now we are able to manipulate this thing. Here is the code that makes this sprite function as a texture as well:

     //Office objects
     public Sprite office_Stamp;
     public Texture2D office_Stamp_Text;
 
     void Start () {
         //Office Object collector
         office_Stamp = Resources.LoadAssetAtPath<Sprite>("Assets/Sprites/Objects/Stamp.png");
     }
     
     void Update () {
         TexturizeSprites();
     }
 
     void TexturizeSprites() {
         //office_Stamp
         office_Stamp_Text = new Texture2D((int)office_Stamp.rect.width, (int)office_Stamp.rect.height );
         var pixels = office_Stamp.texture.GetPixels((int)office_Stamp.textureRect.x, 
                                                     (int)office_Stamp.textureRect.y, 
                                                     (int)office_Stamp.textureRect.width, 
                                                     (int)office_Stamp.textureRect.height );
         office_Stamp_Text.SetPixels( pixels );
         office_Stamp_Text.Apply();

I hope this can help someone in the future :) Feel free to add a comment or something if there's anything you feel you can improve.

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
2

Answer by smobby · Mar 30, 2015 at 09:36 AM

Hi, in response to your answer... i don't think its necessary to write the pixel data into a new Texture2D object, you can simply access the texture via sprite.texture.

i've tested this using this function.. no need to go into advanced options for your sprite.

hope it helps ^^

 void SetCursor( Sprite sprite , Vector2 center ) {
      Cursor.SetCursor (sprite.texture, center, CursorMode.Auto);
 }

change CurserMode to ForceSoftware if you'd like the cursor to be the same size as your sprite.

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 DannyBlackwell · Jun 04, 2016 at 11:25 PM 0
Share

Hey, this seems to be a bug that's been reported (and reportedly fixed in Unity 5.4), but your solution works fine during testing inside the editor, but when a build is created the texture for the cursor gets corrupted as soon as it's changed in your way.

Changing sprites to texture type "Cursor" fixed this for cursor specific textures, however using the texture directly from the sprite in the way you describe seems to corrupt the image when it's being tested in a build.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Swap Custom Cursor for Animated Movie Texture or Sprite Sheet? 1 Answer

Difference between Sprite and Default texture types? 0 Answers

Get sprite to have top-left corner always at top-left of screen 0 Answers

why texture2d sprite rendering not showing in game 0 Answers

How do you crop a Texture2d 1 Answer


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