Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
1
Question by SuperMasterBlasterLaser · May 11, 2014 at 01:09 PM · textdownloadimageswwwform

Downloading images and text files from server.

Hello.

In my server, there is a folder that got images and txt file. Can someone show me simpliest script (C#) that can download them and place them on new created folder in my Resources?

I know that I must use WWWForm to do that kind of things, but I don't know how, all examples that I found are to upload files to server.

Comment
Add comment · Show 2
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 DeveshPandey · Oct 22, 2014 at 08:13 AM 0
Share

Hi Guys,

You can use this plugin Easy Downloader and Uploader

avatar image Fattie · Jan 25, 2016 at 09:25 PM 0
Share

copy and paste solution for text:

http://answers.unity3d.com/answers/1133287/view.html

3 Replies

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

Answer by SuperMasterBlasterLaser · May 11, 2014 at 04:05 PM

So...

I have my folder inside **http://localhost/UnityFileUpload/questions/*.

I have turned my XAMPP on.

Then in Unity for my GameObject wich shows me picture i have written this code:

 public void setPicture(string picturePathAndName, bool isExternal) {
         if (!isExternal) {
             Debug.Log("Loading from inside");
         mySprite = Resources.Load(picturePathAndName, typeof(Sprite)) as Sprite;
         renderer.sprite = mySprite;
         } else {
             picturePathAndName += ".jpg";
             Debug.Log("Loading from outside");
             downloadImg("http://localhost/UnityFileUpload/" + picturePathAndName);
 
         }
         
     }
 
 IEnumerator downloadImg (string url)
     {
         Texture2D texture = new Texture2D(1,1);
         WWW www = new WWW(url);
         yield return www;
         www.LoadImageIntoTexture(texture);
 
         Sprite image = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
         renderer.sprite = image;
     }

It loads well from Resources, but does not download from server!

Then to get contents of my txt file I have written this code:

 private void initializeQuestion() {
         string answerTextLocation = "questions/" + currentQuestionNumber + "/" + currentQuestionNumber + "_ans";
         if (currentQuestionNumber <= questionNumbers) {
             TextAsset textAsset = Resources.Load(answerTextLocation, typeof(TextAsset)) as TextAsset;
             currentQuestionAnswer = textAsset.text;
         } else {
             getAnswerFromOutside("http//localhost/UnityFileUpload/" + answerTextLocation + ".txt");
         }
         
     }
 
 IEnumerator getAnswerFromOutside(string url) {
         WWW www = new WWW(url);
         yield return www;
         currentQuestionAnswer = www.text;
     }

And it does not get text!

What I am doing wrong?

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 $$anonymous$$ · May 11, 2014 at 04:51 PM 1
Share

You have to call the IEnumerator like this:

 StartCoroutine(downloadImg (url));

And I forgot that you have to use DXT1 format:

 Texture2D texture = new Texture2D(4, 4,TextureFormat.DXT1, false);
avatar image SuperMasterBlasterLaser · May 12, 2014 at 01:49 AM 0
Share

It download pictures well, thank you! However it cannot doenload contents of txt file. WHat should I do?

avatar image SuperMasterBlasterLaser · May 12, 2014 at 03:09 PM 0
Share

$$anonymous$$y getAnswerFromOutside function does not get the content of my txt file in my server, what I am doing wrong??

avatar image $$anonymous$$ · May 12, 2014 at 03:20 PM 0
Share

Did you called "getAnswerFromOutside" with StartCoroutine()?

avatar image SuperMasterBlasterLaser · May 13, 2014 at 08:11 AM 0
Share

Yes, I have called it with StartCourotine():

    if (currentQuestionNumber <= questionNumbers) {
             TextAsset textAsset = Resources.Load(answerTextLocation, typeof(TextAsset)) as TextAsset;
             currentQuestionAnswer = textAsset.text;
         } else {
             StartCoroutine(getAnswerFromOutside("http://localhost/UnityFileUpload/" + answerTextLocation + ".txt"));
 
         }
Show more comments
avatar image
1

Answer by $$anonymous$$ · May 11, 2014 at 01:32 PM

You cant add files (to Resources folder) in runtime because after building this folder does not exist any more, but you can locate the files somewhere else like in this example:

 IEnumerator DownloadImg ()
     {
         string url = "https://... Your url (Internet url)";
         Texture2D texture = new Texture2D(1,1);
         WWW www = new WWW(url);
         yield return www;
         www.LoadImageIntoTexture(texture);
         StartCoroutine(UploadImg(texture));
     }
     
     IEnumerator UploadImg(Texture2D texture)
     {
         string screenShotURL = "file:///... Your Url to upload ( Device url)";
         byte[] bytes = texture.EncodeToPNG();
         Destroy( texture );
         
         // Create a Web Form
         WWWForm form = new WWWForm();
         form.AddField("frameCount", Time.frameCount.ToString());
         form.AddBinaryData("file", bytes, "Image.png", "image/png");
         
         // Upload to a cgi script
         WWW w = WWW(screenShotURL, form);
         yield return w;
         if (w.error != null){
             print(w.error);
             Application.ExternalCall( "debug", w.error);
             //print(screenShotURL);
         }
         else{
             print("Finished Uploading Screenshot");
             //print(screenShotURL);
             Application.ExternalCall( "debug", "Finished Uploading Screenshot");
         }
     }
Comment
Add comment · Show 3 · 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 SuperMasterBlasterLaser · May 11, 2014 at 02:19 PM 0
Share

So I have downloaded image and placed it in external folder, but there appears more questions:

1) If my image is not .png it is *.jpg does texture.EncodeToPNG() will work to both of them?

2) If my application will be in this server where images are located. So to my application it will always be localhost. Can I simple make application use that folder in my server as external folder? If yes, hot to get them?

3) How to get txt file?

avatar image $$anonymous$$ · May 11, 2014 at 03:12 PM 0
Share
  1. If your image is .jpg it is converted in .png by ".EncodeToPNG()"

  2. You can use www.text.

avatar image SuperMasterBlasterLaser · May 11, 2014 at 04:11 PM 0
Share

aaaaaand for 2) is .... :)

avatar image
1

Answer by mamad_m2 · Mar 11, 2020 at 10:45 PM

For everyone else, to download and display images in Unity you can easily use Davinci. This library has a simple usage and supports Unity UI.Image and 3D model textures and lots of other cool features like download progress, placeholders and etc.

  Davinci.get().load(imageUrl).into(image).start();

Hope this helps!

Comment
Add comment · Show 3 · 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 TechiePi · Mar 12, 2020 at 06:40 AM 0
Share

I'm using It and is incredible and easy! ^^

avatar image TechiePi · Mar 12, 2020 at 06:41 AM 0
Share

Is only a script and is easy to use. For me IS perfect…

avatar image mesbah · Jan 18, 2021 at 01:27 PM 0
Share

I've tried to use it for a grid. Superb asset, but looks like it's not optimized for mass download (UI blocks). Or am I missing something?

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

25 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

Related Questions

How to download an image and stock it on the device in C# ? 0 Answers

Download and save images into local phone storage makes the app hang 1 Answer

Writing an array to a txt document and send it to my server 2 Answers

Image and Text Quality 1 Answer

WWW object download 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