- Home /
Loading texture file from png/jpg file on disk
In my game, I'd like players to be able to customize their profiles by loading "avatar" from HDD (which would be standard image file). However I don't know how to do that, and couldn't find anything in the docs.
Answer by Eric5h5 · Apr 06, 2013 at 04:12 AM
http://docs.unity3d.com/Documentation/ScriptReference/WWW.LoadImageIntoTexture.html http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.LoadImage.html
Can be first one be used with file:// protocol (second one is no go for me)?
Yes, but if you use file:// then you can also use the second method. If you're doing a webplayer, then neither method is allowed.
Well, I don't want players to change image file extensions to .txt, so first one is better. And since it will be game for sale, I don't think I'll make webplayer version.
The second method doesn't involve changing image file extensions to .txt. You would only do that if you were loading the image file from a TextAsset, which is not an external file, so that doesn't apply at all.
Since Unity 2017, Texture2D.LoadImage has been replaced by ImageConversion.LoadImage.
Answer by IMD · Oct 03, 2014 at 04:23 PM
I just cooked this up for something I'm working on - hope you find it useful. :)
public static Texture2D LoadPNG(string filePath) {
Texture2D tex = null;
byte[] fileData;
if (File.Exists(filePath)) {
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
}
return tex;
}
You don't need yield when using LoadImageIntoTexture, either, as long as you're loading a local file.
If your texture ends up blurry and deformed, you might want to declare your texture that way:
Texture2D tex = new Texture2D (2, 2, TextureFormat.BGRA32,false);
I had to look around for a while before finding this so hopefully it'll help someone else!
Thanks I$$anonymous$$D. Could you make a video tutorial? I have no idea what to do with that. What I want to do is very simple, I have an image on a canvas, a button, by pressing the button you choose file name and it is loaded in the image object. How do I do this?
This doesn't work for PVRTC or any other format, besides jpg/png as far as I understand.
Answer by akhror_unity · Mar 24, 2021 at 04:08 AM
Loading local file can also be done like this:
IEnumerator LoadTextureFromCache(string filePath)
{
if (!File.Exists(filePath))
{
yield break;
}
var www = UnityWebRequestTexture.GetTexture("file://" + filePath);
yield return www.SendWebRequest();
//texture loaded
var texture = DownloadHandlerTexture.GetContent(www);
}
Answer by iNoire · Aug 26, 2021 at 09:53 PM
How do you load a tif image from code? supposedly you can use them if you import them from the editor, but im trying to load preexisting mods for a different game that uses tif.
im currently working on a netstandard library for parsing tiff files from binary but id prefer not to do it myself if i dont have to
This is not an "Answer" to the question. If you have a question, please ask your own, seperate question. Apart from that, Unity does not support loading tiff images at runtime. The engine only supports PNG and JPG at runtime. The Unity editor supports much more formats because it includes licensed loaders for those formats. So if you want / need to load other formats, you have to find a library or script that can load those formats. I've written a bmp and a gif loader myself. A quick google search brought up this TIFF library which is under MIT license.
Your answer
Follow this Question
Related Questions
Load a jpeg from the users drive to use as a texture in a standalone build? 1 Answer
Loading image runtime from file 1 Answer
flat material?(no light info) 4 Answers
How can I prevent my GUI background texture from getting pixelated or stretched? 1 Answer
load all png files as resource but NOT from resource folder 2 Answers