Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
2
Question by TechSupportIncoming1 · Jul 16, 2015 at 09:13 AM · c#wwwwww.texture

Runtime Image Conversion (C#) .ICO to .PNG? (.ICO not supported; WWW.texture only accepts .PNG and .JPEG)

Any alternatives? Perhaps I should use WWW.Bytes and somehow transform that array of bytes from .ICO to .PNG?

Quote from Unity3D Docs:

The data must be an image in JPG or PNG format. If the data is not a valid image, the generated texture will be a small image of a question mark.

And indeed, that's what I am getting — a red-question-mark photo! I am 100% sure that I am passing the correct link of the .ICO file and successfully downloading it.

Here's the download code:

 void Update()
 {
     if (gotIconURL && !gotIconTexture)
     {
         //StopCoroutine("GetIcon");
         StartCoroutine("GetIcon");
         gotIconURL = false; // We don't have the new/future icon URL
     }
 }

 IEnumerator GetIcon()
 {
     ///NOTE!
     ///Each invocation of texture property allocates a new Texture2D, do something about it! 
     ///If I continously download textures I must use LoadImageIntoTexture or Destroy the previously created texture.
     
     // Start a download of the given URL
     var www = new WWW(iconURL); // e.g. https://www.google.com/favicon.ico
     //Debug.Log(iconURL);

     // Wait for download to complete
     yield return www;

     // Assign texture
     rawIcon.texture = www.texture;

     gotIconTexture = true;
 }


Any ideas?

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 Dave-Carlile · Jul 16, 2015 at 12:50 PM 1
Share

The answer to this is spelled out specifically by the error message. If you want to convert from ico then you'll need to find something that can convert it to jpg or png.

avatar image TechSupportIncoming1 · Jul 16, 2015 at 01:44 PM 0
Share

@Dave I do realize that. I know what the error is. I merely need some directions on converting an array of bytes representing .ICO image to a PNG one so I could use it as a Texture2D...

avatar image NewPath · Jul 17, 2015 at 10:46 AM 1
Share

This is not a very portable solution, but you could use the Bitmap class from the System.Drawing assembly to read in the icon and then save it out to a memory stream in the format of your choice. If I'm not mistaken, it uses system codecs to handle the file I/O though, so this might be limited to Windows, but you might find a way to include the appropriate libraries with your distro.

avatar image TechSupportIncoming1 · Jul 17, 2015 at 11:57 AM 0
Share

Thanks NewPath! Gonna give that a try.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Paulius-Liekis · Jul 16, 2015 at 03:00 PM

Generally people convert it offline, i.e. using some program and then simply download png (instead of ico or other format). If you really need to support ico, then you will have to find a converter and integrate it into your app. There is no simple solution - Unity has no API for that.

Comment
Add comment · Show 7 · 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 TechSupportIncoming1 · Jul 17, 2015 at 07:44 AM 0
Share

Thanks for your response. I hoped somebody would provide a C# solution cuz as far as I know it is possible to do this in C# (e.g. using bitmap decoder).

avatar image Bunny83 · Jul 17, 2015 at 04:48 PM 2
Share

@TechSupportInco$$anonymous$$g1:
Well, it's of course possible since the ICON format is a relatively simple one and quite well documented. If you were to build a native .NET / C# application you could simply use the wrapper classes that $$anonymous$$icrosoft provides in their libraries. I'm not sure if those are even implemented in the $$anonymous$$ono framework and if they are compatible with Unity. Even if they are you would have to include the assembly that contains the "Icon" class and of course all dependencies (which can result in a huge amount). Also you would be restricted to window as target platform since the .NET / $$anonymous$$ono class "Icon" just wraps the native icon support of windows.

It's probably easier to write your own Icon loader. I was actually very suprised that the wikipedia article is almost like a tec-specification. It should be all you need to know to load a ICO file yourself. It's of course quite a bit of work and since the format has grown in features overtime you probably won't implement all possible cases.

I already thought about writing an ICO and CUR loader myself, but i'm not sure if i can find the time ^^.

avatar image TechSupportIncoming1 · Jul 17, 2015 at 06:01 PM 0
Share

@Bunny83: Thank you for the informative answer you provided. Also for the Wikipedia article reference. If I ran out of options and had to port the project to several platforms, then yes I suppose I will have to write my own ICO loader, which I wouldn't $$anonymous$$d to invest some time in. Also, what do you mean with CUR loader?

EDIT: never$$anonymous$$d, mouse cursor :P Graphics file format for mouse cursors.

@Paulius Liekis: I need to perform the conversion in run time. So offline converters won't work in my case.

avatar image Dave-Carlile · Jul 17, 2015 at 06:12 PM 1
Share

Since you're grabbing the .ico files from a web link, have you considered converting them on the web server as you serve them up so Unity only sees the .png?. You should be able to convert them in real time on the server. This of course wouldn't be viable if you're not in control of the server that's hosting the files, but thought I'd throw it out there.

avatar image Dave-Carlile · Jul 17, 2015 at 06:59 PM 1
Share

It can never be easy right? :) Another thought then - if you can't find a good portable C# implementation, you can maybe do something with custom Unity plugin.

Show more comments
avatar image
0

Answer by TechSupportIncoming1 · Jul 17, 2015 at 07:59 AM

Apparently nobody went through this issue I am having. Suppose I have to convert the image to PNG using something like the answer below and then use Resources.LoadAsset...

http://stackoverflow.com/a/10377361

Getting the variable ICON link in real-time was a real headache, let alone converting it now and THEN loading it back into Unity! May the force be with me lol

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 Paulius-Liekis · Jul 17, 2015 at 08:43 AM 1
Share

You can use Resources.LoadAsset only for assets that live in Resources folder only. So I get an impression that you're confused about what's possible. Please read about Resources and AssetBundles, that might give you some ideas how to solve your problem in other ways.

avatar image Dave-Carlile · Jul 17, 2015 at 07:01 PM 1
Share

Yeah, you can't use Resources.Load, but once you get the raw PNG bytes and do Texture2D.SetPixels.

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

25 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

Related Questions

How to assign texture from url 1 Answer

WebClient on Android 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using WWW class to get multiple urls 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