JoaoM
JoaoM

Reputation: 33

Unity3D loading resources after build

I have many sets of images (PNG) that are placed in different subfolders inside the Resources folder on the Assets of the project. When working on the editor I'm able to load the images from the different subfolders without a problem, by just simply use the Resources.Load() command and providing the path to the specific image that I'm trying to load, such as:

firstLeftCC = Resources.Load("Case2/Left/CC/IMG-0004-00001", typeof(Texture2D)) as Texture2D;

In this example the image "IMG-0004-00001" is placed in the CC folder, and the CC folder is inside the Left folder, and the Left folder is inside the Case2 folder, and finally the Case2 folder is in the Resources Folder.

But after building the project, for a Windows application, when I run the .exe file it doesn't load any of those images. After some research, it seems that the problem is related with the presence of the subfolders inside the Resources, since the path given to the Resources.Load() function to load the image doesn't exist in the build.

I would like to know if anyone knows a solution for this problem, or if it is possible to load the images from a given folder instead of trying to load them from the Resources folder.

Thanks in advance for the help.

Upvotes: 3

Views: 8700

Answers (2)

Programmer
Programmer

Reputation: 125275

Optional method of loading your PNG file during run time. You can leave the Case2 in the Asset Folder during development. After you build for Windows, go to folder the exe built file is. Let's assume it is called Game.exe. There will be a folder called Game_Data in the-same directory the Game.exe executable file is.

Copy the Case2 folder to the Game_Data folder.

It should now look like this .... Game_Data/Case2/Left/CC/IMG-0004-00001.PNG

Now with code below, you can easily read your png files with the code below:

  Texture2D firstLeftCC = null;
    byte[] imageBytes;
    string imagePath = Application.dataPath + "/Case2/Left/CC/IMG-0004-00001.PNG";

    if (File.Exists(imagePath))
    {
        Debug.Log("Exist");
        imageBytes = File.ReadAllBytes(imagePath);
        firstLeftCC = new Texture2D(10, 10);
        firstLeftCC.LoadImage(imageBytes);
    }

Now you can freely use firstLeftCC as a Texture2D

Note: Unity creates .meta file/folder for every file/folder in the Assets directory. When when you copy the directory,IMG-0004-00001.PNG will have another file called IMG-0004-00001.PNG.meta. You should either NOT place your Case2 folder in the Assets folder during development or you will have to delete these yourself.

Include using System.IO;

Upvotes: 1

Jansen
Jansen

Reputation: 356

Alternatively to Resources.Load, you can load images from a given folder using the WWW class. To do this the URL must use the file:// protocol and contains the path to the files according to your OS.

http://docs.unity3d.com/ScriptReference/WWW.html

http://answers.unity3d.com/questions/517414/how-to-use-www-to-load-local-files.html

Upvotes: 1

Related Questions