- 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.
I think I'll use first method anyway so players can use images on web.
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 IMD. 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.
Your answer
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
Loading image runtime from file 1 Answer
Load a jpeg from the users drive to use as a texture in a standalone build? 1 Answer
My textures looks bad in unity 1 Answer
Image quality deteriorates in Unity 1 Answer
How can I load pre-compressed textures at run-time (without using assetBundles) 3 Answers