Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

External Loading of Images at Runtime...Yet another topic

Discussion in 'Scripting' started by palamangelus, Apr 26, 2012.

  1. palamangelus

    palamangelus

    Joined:
    Jun 8, 2010
    Posts:
    26
    I know this topic has been covered often, but I'm hoping there is a creative solution around Unity's current limitations on what it can import at runtime.

    I'm working on something that needs to load EXR data (hdr/pfm/whatever, just something that is not a png or jpg) at runtime. The images are created on the fly and need to be loaded at runtime as they change. Right now I can do the entire process and everything works correctly (other than external dll hell), but only if I import the image prior to the build. There has to be a way to get it to work after it's built.

    * Resources.Load won't work since it has to be included.
    * AssetBundles won't work as there's no unity install on the production machine (so I can't autogenerate the bundle).
    * WWW won't work since it can only do pngs/jpgs (I need to keep it an exr as the external conversion to a png doesn't really give me a great image).

    Is it something I might be able to do in an external dll, can I do it using Texture2D.SetPixels (I can probably work within ARGB32)? Anyone have any method they can recommend?

    Thanks!
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,521
    Ultimately all you need to create a texture is an array of Color structs. That being said - if you can find a solution that will read and parse the data contained in the file in a manner that allows you to create the necessary Color[] array then you should be good to go.
     
  3. palamangelus

    palamangelus

    Joined:
    Jun 8, 2010
    Posts:
    26
    Thanks for the response!

    Great - I am doing the conversion using FreeImageNET, and I can get a color array out of that. So then I should just be able to use Texture2D.SetPixels, right?

    Thanks!
     
  4. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    Yes, you could use WWW and download it as a byte array and then use SetPixels. You may want to profile this.
     
  5. palamangelus

    palamangelus

    Joined:
    Jun 8, 2010
    Posts:
    26
    I don't think I need to use WWW since that byte array would just be the actual image definition and not the color data. In other words, it's easier to actually load the image from the file system without WWW (I'm on a standalone, btw) and extract the color data from the FreeImage calls then translate over into the Unity color type and SetPixels on that. I think. :p
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,521
    That should do it. The only catch may be
    1- make sure the pixel ordering is the same. Unity orders left-to-right, top-to-bottom so GetPixel(0) is the top-left pixel. If your format orders differently you'll have to adjust accordingly.
    2- Make sure RGBA is 0-1 and not 0-255. Easy enough to convert if it is, just divide each component by 255.
     
unityunity