• 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
7
Question by darkhog · Apr 06, 2013 at 04:00 AM · textureimageloadingfile

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.

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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

Comment
Add comment · Show 6 · 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 darkhog · Apr 06, 2013 at 04:37 AM 0
Share

Can be first one be used with file:// protocol (second one is no go for me)?

avatar image Eric5h5 · Apr 06, 2013 at 04:44 AM 0
Share

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.

avatar image darkhog · Apr 06, 2013 at 04:57 AM 0
Share

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.

avatar image Eric5h5 · Apr 06, 2013 at 05:05 AM 0
Share

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.

avatar image Edy · Jan 10, 2018 at 05:27 PM 5
Share

Since Unity 2017, Texture2D.LoadImage has been replaced by ImageConversion.LoadImage.

Show more comments
avatar image
74

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;
 }
Comment
Add comment · Show 11 · 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 Radivarig · Nov 17, 2014 at 04:40 AM 1
Share
  • Doesn't require yield!! Thanks a bunch

avatar image Eric5h5 · Nov 17, 2014 at 05:02 AM 1
Share

You don't need yield when using LoadImageIntoTexture, either, as long as you're loading a local file.

avatar image another_kind · Nov 09, 2015 at 08:48 PM 5
Share

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!

avatar image dh4m13l · Dec 21, 2015 at 01:14 PM 0
Share

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?

avatar image Paulius-Liekis · Oct 07, 2016 at 12:40 PM 0
Share

This doesn't work for PVRTC or any other format, besides jpg/png as far as I understand.

Show more comments
avatar image
3

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);
 
     }

Comment
Add comment · 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
0

Answer by doublegumbo · Jul 15, 2019 at 10:35 PM

https://docs.unity3d.com/2017.1/Documentation/ScriptReference/ImageConversion.LoadImage.html

Comment
Add comment · 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
0

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

Comment
Add comment · Show 1 · 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 Bunny83 · Aug 27, 2021 at 09:17 AM 0
Share

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

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image

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

How to stop gradient banding? 2 Answers

Assigning UV Map to model at runtime 0 Answers

Saving and loading to/from Android 1 Answer


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