简体   繁体   English

构建后Unity3D加载资源

[英]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.我有很多组图像 (PNG) 放置在项目资产的Resources文件夹内的不同子文件夹中。 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:在编辑器上工作时,我可以毫无问题地从不同的子文件夹加载图像,只需使用Resources.Load()命令并提供我尝试加载的特定图像的路径,例如:

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.在这个例子中,图像“IMG-0004-00001”被放置在CC文件夹中,CC文件夹在Left文件夹中,Left文件夹在Case2文件夹中,最后Case2文件夹在Re​​sources文件夹中。

But after building the project, for a Windows application, when I run the .exe file it doesn't load any of those images.但是在构建项目后,对于 Windows 应用程序,当我运行.exe文件时,它不会加载任何这些图像。 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.经过一些研究,似乎问题与资源中存在子文件夹有关,因为在构建中不存在提供给 Resources.Load() 函数以加载图像的路径。

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.我想知道是否有人知道此问题的解决方案,或者是否可以从给定文件夹加载图像而不是尝试从 Resources 文件夹加载它们。

Thanks in advance for the help.在此先感谢您的帮助。

Optional method of loading your PNG file during run time.在运行时加载 PNG 文件的可选方法。 You can leave the Case2 in the Asset Folder during development.您可以在开发过程中将Case2留在 Asset 文件夹中。 After you build for Windows, go to folder the exe built file is.为 Windows 构建,转到exe构建文件所在的文件夹。 Let's assume it is called Game.exe .让我们假设它被称为Game.exe There will be a folder called Game_Data in the-same directory the Game.exe executable file is.Game.exe可执行文件所在的目录中会有一个名为Game_Data的文件夹。

Copy the Case2 folder to the Game_Data folder.Case2文件夹复制Game_Data文件夹。

It should now look like this .... Game_Data/Case2/Left/CC/IMG-0004-00001.PNG它现在应该是这样的...... Game_Data/Case2/Left/CC/IMG-0004-00001.PNG

Now with code below, you can easily read your png files with the code below:现在使用下面的代码,您可以使用以下代码轻松读取您的png文件:

  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现在你可以自由地使用 firstLeftCC 作为 Texture2D

Note : Unity creates .meta file/folder for every file/folder in the Assets directory.注意:Unity 为Assets目录中的每个文件/文件夹创建 .meta文件/文件 When when you copy the directory, IMG-0004-00001.PNG will have another file called IMG-0004-00001.PNG.meta .当您复制目录时, IMG-0004-00001.PNG将有另一个名为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.在开发过程中,您不应该将Case2文件夹放在 Assets 文件夹中,否则您必须自己删除这些文件夹。

Include using System.IO;包括using System.IO;

Alternatively to Resources.Load, you can load images from a given folder using the WWW class.除了 Resources.Load,您还可以使用 WWW 类从给定文件夹加载图像。 To do this the URL must use the file:// protocol and contains the path to the files according to your OS.为此,URL 必须使用 file:// 协议并根据您的操作系统包含文件的路径。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM