Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 akisrn · May 16, 2017 at 08:01 AM · instantiateimage

Loading Base64 String into RawImage

Hello, everyone!

I'm trying to load a base64 string to my instantiated prefab's RawImage. When i check the texture of the RawImage, it shows a red question mark. but the RawImage in my scene still remains black (default color i had set)

Here is my code:

 Texture2D convertedBase64String = new Texture2D (2,2);
     
 string iconBase64String = "base64 string here"; // string works when converted using online converter
 byte[] decodedBytes = System.Text.Encoding.UTF8.GetBytes(iconBase64String);
 convertedBase64String.LoadImage(decodedBytes);
     
 // Instantiated object
 appButton.transform.Find("Icon").GetComponent<RawImage>().texture = convertedBase64String;
Comment
Add comment · Show 4
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 NoBrainer-David · May 16, 2017 at 08:19 AM 0
Share

So in the comments you are saying that you get the desired behavior, when an online converter creates a base64 string for you?

Have you checked out these methods?

 System.Convert.ToBase64String(byte[] bytes)
 System.Convert.FromBase64String(byte[] bytes)
avatar image akisrn NoBrainer-David · May 16, 2017 at 08:23 AM 0
Share

Yes I have. When I replace System.Text.Encoding.UTF8.GetBytes with System.Convert.FromBase64String, I get an error that says "System.FormatException: Invalid length."

avatar image NoBrainer-David akisrn · May 16, 2017 at 08:40 AM 0
Share

It sounds like that string you passed in was not a Base64 string. A Base64 string cannot be any arbitrary size or contain any characters.

Okay, so using ToBase64 is not used on its own. You use it in conjunction with UTF8 encoding, for example.

To clarify: UTF8 deter$$anonymous$$es how the characters of a string are encoded into bytes. A base 64 string deter$$anonymous$$es how to encode those bytes into characters that you can easily transfer over the wire (no sequence control character, etc. are contained in that string).

Check out the following link to see how to create Base64 strings from arbitrary string and backwards again:

https://arcanecode.com/2007/03/21/encoding-strings-to-base64-in-c/

If you show more of your code where you get the string from, I could help further.

avatar image akisrn NoBrainer-David · May 16, 2017 at 08:54 AM 0
Share

Thanks for clarifying that up! I use base64-image.de to convert my images.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · May 16, 2017 at 08:55 AM

You can't assign a byte array of arbitrary image data to a variable that expects a class instance of type Texture. You have to create a Texture2D object and make it load the image from your data.

 Texture2D tex = new Texture2D(1,1);
 tex.LoadImage( decodedBytes );
 appButton.transform.Find("Icon").GetComponent<RawImage>().texture = tex;

Note that base64 is just a way to encode arbitrary bytes into a human readable datastream. When you decode the data stream you get back the original byte array. Those bytes could contain any kind of data.

Unity only supports loading JPG and PNG files at runtime. So make sure you image is in one of those two formats or it can't be loaded.

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

80 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 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 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 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

Instantiate Prefab (Image) and show it on the screen? 1 Answer

I am trying to spawn coloured cubes that match a pixel image. At the moment the cubes spawn but i do not know how to match up the colours. 0 Answers

Checking if object intersects? 1 Answer

i am fetch the images using (j son)server but it take too much time?? 0 Answers

inspector panel blank 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