gpt4 book ai didi

java - 无法通过 AssetManager - libgdx 从 zip 存档加载 TextureAtlas

转载 作者:行者123 更新时间:2023-11-29 19:44:43 25 4
gpt4 key购买 nike

我有 2 个 AssetManager 实例:一个用于基本纹理,一个用于高质量纹理。基本纹理位于“android/assets”文件夹中,高质量纹理打包在 zip 文件中。此文件夹中的内容(文件名)是相同的 - zip 存档中只有质量更好的纹理。

当我尝试从 zip 文件加载 TextureAtlas 时,AssetManager 抛出异常:“无法加载 Assets 的依赖项:teamLogo.png”。当我加载纹理文件时,一切正常。加载 TextureAtlas 仅适用于“android/assets”文件夹。

AssetManager 使用“android/assets”——一切正常:

AssetManager am = new AssetManager();
am.load( "images/image.png", Texture.class );
am.load( "images/teamLogo.pack", TextureAtlas.class );

AssetManager 使用 zip 存档 - 无法加载 TextureAtlas:

ZipFile archive = new ZipFile(expansionFileHandle.file());
ArchiveFileHandleResolver resolver = new ArchiveFileHandleResolver(archive);
AssetManager amHQ = new AssetManager(resolver);

这很好用:

amHQ.load( "images/image.png", Texture.class );

这行不通:

amHQ.load( "images/teamLogo.pack", TextureAtlas.class );

存档文件句柄类:

public class ArchiveFileHandle extends FileHandle 
{
final ZipFile archive;
final ZipEntry archiveEntry;

public ArchiveFileHandle (ZipFile archive, File file)
{
super(file, FileType.Classpath);
this.archive = archive;
archiveEntry = this.archive.getEntry(file.getPath());
}

public ArchiveFileHandle (ZipFile archive, String fileName)
{
super(fileName.replace('\\', '/'), FileType.Classpath);
this.archive = archive;
this.archiveEntry = archive.getEntry(fileName.replace('\\', '/'));
}

@Override
public FileHandle child (String name)
{
name = name.replace('\\', '/');
if (file.getPath().length() == 0)
return new ArchiveFileHandle(archive, new File(name));
return new ArchiveFileHandle(archive, new File(file, name));
}

@Override
public FileHandle sibling (String name)
{
name = name.replace('\\', '/');
if (file.getPath().length() == 0)
throw new GdxRuntimeException("Cannot get the sibling of the root.");
return new ArchiveFileHandle(archive, new File(file.getParent(), name));
}

@Override
public FileHandle parent ()
{
File parent = file.getParentFile();
if (parent == null)
{
if (type == FileType.Absolute)
parent = new File("/");
else
parent = new File("");
}
return new ArchiveFileHandle(archive, parent);
}

@Override
public InputStream read ()
{
try
{
return archive.getInputStream(archiveEntry);
}
catch (IOException e)
{
throw new GdxRuntimeException("File not found: " + file + " (Archive)");
}
}

@Override
public boolean exists()
{
return archiveEntry != null;
}

@Override
public long length ()
{
return archiveEntry.getSize();
}

@Override
public long lastModified ()
{
return archiveEntry.getTime();
}

我做错了什么?

最佳答案

是的,我找到了 :) ArchiveFileHandle 无法加载 TextureAtlas 的依赖项,因为他找不到那些文件。查看 zip 存档时,您必须将“\”字符替换为“/”。该错误存在于 ArchiveFileHandle 构造函数之一中。这一行:

archiveEntry = this.archive.getEntry(file.getPath());

应该是:

archiveEntry = this.archive.getEntry(file.getPath().replace('\\', '/'));

现在一切正常

关于java - 无法通过 AssetManager - libgdx 从 zip 存档加载 TextureAtlas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38018352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com