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 frostymm · Jun 02, 2015 at 10:42 AM · c#errorspritetexture2d

Creating Sprites in code for use Dynamically.

I'm trying to make it so that the user can upload an image for their character portrait. I get the path for the image that they want to use and then I make a Texture2D load the image through a byte array.

Right now I'm just trying to make the UI Image object display the image. No matter what I do though it doesn't seem to want to work. If I replace the texture of the sprite that already exists, it replaces all the instances of that sprite I've used. (It even permanently replaces the sprite texture in the editor) If I try to use the sprite.create method at all, it throws a "!hasError" and the sprite that loads onto the Image object is blank and not usable.

I've seen a few other posts who claim to have gotten it to work but with SpriteRenderer rather than Image and I'm not quite sure how those work with UI.

Any help is appreciated.

Here's a code snippet:

 public Image Portrait;
     public void SetPortrait(string imgPath)
     {
         if(File.Exists(imgPath) && Path.GetExtension(imgPath).Equals(".png", System.StringComparison.InvariantCultureIgnoreCase))
         {
             byte[] fileData;
             fileData = File.ReadAllBytes(imgPath);
 
             Rect rct = Portrait.rectTransform.rect;
 
             Texture2D OldTexture = Portrait.sprite.texture;
 
             Portrait.sprite = Sprite.Create(new Texture2D(OldTexture.width, OldTexture.height), 
                                             new Rect(rct), new Vector2(0.5f,0.5f), Portrait.sprite.pixelsPerUnit);
 
             Portrait.sprite.texture.LoadImage(fileData);
             
             GetTemporaryCharacter().SetHashtableValue("Image", fileData);
         }
     }



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

1 Reply

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

Answer by frostymm · Jun 05, 2015 at 07:18 AM

Okay so I got it to work by creating an entirely new Rect in the Sprite.Create() function rather than just copying the one I currently had.

I remember that I had done that in the first place but it didn't work at the time. I must have fixed something else between those two times though I'm not sure what. Regardless it works now so that's cool.

My guess for why the old Rect was breaking it is because instead of copying the rect I put into it, it must have made an entirely new one with a width and height of 0. Unity bug maybe? My own stupidity maybe? I'm not one to go back and find out.

Here's the fixed portion of code:

 public static void ChangeImageTexture(Image img, byte[] newImgData)
         {
             Texture2D OldTexture = img.sprite.texture;
             Texture2D newTexture = new Texture2D(OldTexture.width, OldTexture.height);
 
             newTexture.LoadImage(newImgData);
             
             img.sprite = Sprite.Create(newTexture, 
                                             new Rect(0, 0, newTexture.width, newTexture.height), new Vector2(0.5f,0.5f), img.sprite.pixelsPerUnit);
 
         }

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

2 People are following this question.

avatar image avatar image

Related Questions

Resize a sprite by giving it more transparent pixels 0 Answers

My sprite from sprite.create looks pixelated. 1 Answer

Slicing sliced sprite via script 0 Answers

Can I convert a sprite to a texture2D or use the sprite on a GUI? 1 Answer

Texture2D.Apply() not updating pixels 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