- Home /
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.
Hi Guys,
You can use this plugin Easy Downloader and Uploader
copy and paste solution for text:
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?
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);
It download pictures well, thank you! However it cannot doenload contents of txt file. WHat should I do?
$$anonymous$$y getAnswerFromOutside function does not get the content of my txt file in my server, what I am doing wrong??
Did you called "getAnswerFromOutside" with StartCoroutine()?
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"));
}
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");
}
}
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?
If your image is .jpg it is converted in .png by ".EncodeToPNG()"
You can use www.text.
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!
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
Follow this Question
Related Questions
Using WWW/WWWForum to download from a file hoster 2 Answers
WWW.text not display to UI Text.text 0 Answers
how to Put a Picture In Text ? 0 Answers
Creating a texture from pictures and text 0 Answers
using htaccess and htpassword 0 Answers