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.
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 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?
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!