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.
5 Answers
5I 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;
}
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.
– Eric5h5Well, 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.
– darkhogThe 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.
– Eric5h5Loading 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);
}
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
+1 Doesn't require yield!! Thanks a bunch
– RadivarigYou don't need yield when using LoadImageIntoTexture, either, as long as you're loading a local file.
– Eric5h5If 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!
– another_kindThanks 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?
– dh4m13lThis doesn't work for PVRTC or any other format, besides jpg/png as far as I understand.
– Paulius-Liekis