无法通过AssetManager - libgdx从zip存档加载TextureAtlas [英] Couldn't load TextureAtlas from zip archive via AssetManager - libgdx

查看:165
本文介绍了无法通过AssetManager - libgdx从zip存档加载TextureAtlas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 2 instances of AssetManager: one for basic textures and one for high quality textures. Basic textures are located in "android/assets" folder and high quality textures are packed in zip file. Content (file names) in this folders are the same - there are only better quality textures in zip archive.

AssetManager抛出异常:无法加载资产的依赖关系:teamLogo.png当我试图从zip文件加载TextureAtlas时。当我加载纹理文件时一切正常。加载TextureAtlas仅适用于'android / assets'文件夹。

AssetManager throws an exception: "Couldn't load dependencies of asset: teamLogo.png" when I'm trying to load TextureAtlas from zip file. When I'm loading Texture file everything is ok. Loading TextureAtlas works only from 'android/assets' folder.

使用'android / assets'的AssetManager - 一切正常:

AssetManager using 'android/assets' - everything is ok:

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

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

AssetManager using zip archive - can't load 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 );

ArchiveFileHandle类:

ArchiveFileHandle 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();
}

我做错了什么?

推荐答案

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

Yeaaahhh, I found it :) ArchiveFileHandle couldn't load dependencies of TextureAtlas because he can't find those files. When looking in zip archive you have to replace '\' char to '/'. The bug was in one of ArchiveFileHandle constructors. This line:

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

应为:

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

现在一切正常

这篇关于无法通过AssetManager - libgdx从zip存档加载TextureAtlas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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