- Home /
www image not loading from folder in Build (works in editor) - Using windows
Hi
Spent the last few hours searching for an answer but nothing has worked so far.
Essentially I am loading all files (jpgs) from a folder as textures which are then converted to a sprites and stored in an array. Then I am loading each sprite into a gameobject.
This is working perfectly fine within the editor however when I build and run I get the following error.
You are trying to load data from a www stream which had the following error when downloading. Couldn't open file /test.jpg
This occurs for every image and nothing loads.
public class LoadImage : MonoBehaviour {
// public ArrayList imageBuffer = new ArrayList();
public List<Texture2D> imageBuffer;
public List<Sprite> loadedSprites;
public GameObject textureTest;
public GameObject testObject;
string path = @"C:\Users\HWPC\Desktop\Test";
void Start()
{
List<Sprite> everyone = new List<Sprite>();
StartCoroutine("LoadImages");
}
IEnumerator LoadImages()
{
var info = new DirectoryInfo(path);
var fileInfo = info.GetFiles();
foreach (FileInfo file in fileInfo)
{
Debug.Log(file);
string fullFilename = "file:///" + file.ToString();
WWW www = new WWW(fullFilename);
yield return www;
Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
try
{ www.LoadImageIntoTexture(texTmp); }
catch { }
imageBuffer.Add(texTmp);
Sprite image = Sprite.Create(texTmp, new Rect(0, 0, texTmp.width, texTmp.height), new Vector2(0.5f, 0.5f), 100);
loadedSprites.Add(image);
testObject.GetComponent<Image>().sprite = image;
}
}
}
Advice on possible causes would be greatly appreciated !
Answer by Magius96 · Mar 18, 2016 at 04:50 PM
Why use WWW when your loading them locally?
using System.IO;
public class LoadImage : MonoBehavior
{
private Sprite LoadSpriteImage(string filepath)
{
if (File.Exists(filepath))
{
var photo = new Texture2D(4, 4);
byte[] imageData = File.ReadAllBytes(filepath);
photo.LoadImage(imageData);
return Sprite.Create(photo, new Rect(0, 0, photo.width, photo.height), new Vector2(0.5f, 0.5f));
}
return null;
}
}
Just add this method to your class and call on it inside your LoadImages method to get the sprites.
Thanks $$anonymous$$agius, that's a much better way to do it. However I am still getting the same issue where the filepath is working in the editor but not in the build (File.Exists(filepath) is failing/false).
I worked out what the issue is. But have no idea why it is occurring.
Essentially in build fullFilename = file.ToString();
is only returning test.jpg, ins$$anonymous$$d of the entire path. So a fix is if I do fullFilename = path + file.ToString();
ins$$anonymous$$d. However i'll need to make a workaround so it works in both editor and build.
Ins$$anonymous$$d of using file.ToString() use file.FullName
FullName returns a string containing the path and filename.
You could also use string[] files = Directory.GetFiles(path);
to get the containing files with path as a string array.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Why is cloud build not ignoring hidden assets? 0 Answers
Is my project ruined? 1 Answer
Hide windows build folder 1 Answer