构建后Unity3D加载资源 [英] Unity3D loading resources after build

查看:123
本文介绍了构建后Unity3D加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多图像集(PNG),它们放在项目资产上资源文件夹内的不同子文件夹中。在编辑器上工作时,我只需使用 Resources.Load()命令并提供路径即可从不同子文件夹加载图像而不会出现问题。我正在尝试加载的特定图片,例如:

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;

在此示例中,图像IMG-0004-00001放置在CC文件夹中,并且CC文件夹位于Left文件夹内,Left文件夹位于Case2文件夹内,最后Case2文件夹位于Resources文件夹中。

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.

但是在构建项目之后,对于Windows应用程序,当我运行 .exe 文件时,它不会加载任何这些图像。经过一些研究,似乎问题与资源中子文件夹的存在有关,因为在构建中不存在加载图像的Resources.Load()函数的路径。

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.

我想知道是否有人知道这个问题的解决方案,或者是否可以从给定文件夹加载图像而不是尝试从Resources文件夹加载它们。

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.

提前感谢您的帮助。

推荐答案

可选在运行时加载PNG文件的方法。
您可以在开发期间将 Case2 保留在资源文件夹中。 为Windows构建后,转到 exe 构建文件所在的文件夹。我们假设它被称为 Game.exe Game.exe 可执行文件所在的目录中将有一个名为 Game_Data 的文件夹。

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.

Case2 文件夹复制 Game_Data 文件夹。

现在看起来应该是这样.... Game_Data / Case2 / Left / CC / IMG-0004-00001.PNG

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

现在使用以下代码,您可以使用以下代码轻松读取 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);
    }

现在你可以自由地使用firstLeftCC作为Texture2D

Now you can freely use firstLeftCC as a Texture2D

注意:Unity为资产<中的每个文件/文件夹创建.meta 文件/文件夹 / strong>目录。当您复制目录时, IMG-0004-00001.PNG 将有另一个名为 IMG-0004-00001.PNG.meta 的文件。您应该在开发期间不要将 Case2 文件夹放在Assets文件夹中,否则您必须自己删除

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.

使用System.IO包含;

Include using System.IO;

这篇关于构建后Unity3D加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆