plist 中的 textureImageData? - Cocos 2D V 3.4 (CCParticleSystem)

textureImageData in plist? - Cocos 2D V 3.4 (CCParticleSystem)

我目前正在为新的 V 3.4 引擎修复旧的 Cocos iPhone Xcode 项目。它曾经工作得非常好,但是我发现了这行代码:

CCParticleSystem *fire = [CCParticleSystemQuad particleWithFile:@"fire.plist"];

CCParticleSystemQuad 已被删除,因此我将其替换为 "CCParticleSystem":

CCParticleSystem *fire = [CCParticleSystem particleWithFile:@"fire.plist"];

它一直在该行代码处崩溃并显示以下错误消息:

- cocos2d: Couldn't find file:user_particle30956.png
- *** Assertion failure in -[CCTextureCache addCGImage:forKey:], /Users/.../Source/libs/cocos2d-iphone/cocos2d/CCTextureCache.m:370
- *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

该图像不存在,实际上作为纹理图像数据嵌入到 plist 文件中。我知道在 Cocos V 2 中它会自动创建并将此纹理图像数据存储为具有名称的图像文件。 我怎样才能让这个适用于 V 3?

我不确定您需要多少关于 plist 文件的信息。这是一长串粒子系统配置(角度、持续时间、发射器类型等)之后的信息:

<key>textureFileName</key>
<string>user_particle30956.png</string>
<key>textureImageData</key>
<string>eJztnQecXWURxXdDgCCggBQLuisqKlbslcSCBcEC9pa1996wkbUr9q5...</string>

谢谢大家。

我遇到了同样的问题。从 particle designer docs 来看,嵌入的纹理显然是 TIFF,而不是 Cocos2D 所期望的 PNG。

我不明白这个,因为它曾经与 Cocos2D v2.2 一起工作得很好。

对于 v3.4.x 我所做的是更改粒子设计器项目以将纹理导出为文件,然后将该文件添加到 Xcode 项目以便粒子系统加载它艰难的道路。

您也许可以查看 v2.2 代码以了解它做了什么。也许该代码可以带到 v3.4.x

[已更新] 查看了 v2.2 代码后,从嵌入图像中获取二进制数据并将其转换为纹理的机制使用 UIImage 而不是某些 PNG/JPEG 特定代码。

// Cocos2d v2.2
//
#ifdef __CC_PLATFORM_IOS
            UIImage *image = [[UIImage alloc] initWithData:data];
#elif defined(__CC_PLATFORM_MAC)
            NSBitmapImageRep *image = [[NSBitmapImageRep alloc] initWithData:data];
#endif

v3.x 代码执行此操作(对于 iOS 和 Android):

// Cocos2D v3.x
//
BOOL png = [[[dictionary valueForKey:@"textureFileName"] lowercaseString] hasSuffix:@".png"];
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGImageRef image = (png) ? CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault) : CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault) ;

我强烈怀疑以下方法可能有效(尽管我自己还没有测试过):

#if __CC_PLATFORM_IOS
    UIImage *image = [[UIImage alloc] initWithData:data];

    [self setTexture:  [ [CCTextureCache sharedTextureCache] addCGImage:[image CGImage] forKey:textureName]];

#elif __CC_PLATFORM_ANDROID
    BOOL png = [[[dictionary valueForKey:@"textureFileName"] lowercaseString] hasSuffix:@".png"];
    CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
    CGImageRef image = (png) ? CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault) : CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault) ;

    [self setTexture:  [ [CCTextureCache sharedTextureCache] addCGImage:image forKey:textureName]];

    CGDataProviderRelease(imgDataProvider);
    CGImageRelease(image);
#elif __CC_PLATFORM_MAC
    NSBitmapImageRep *image = [[NSBitmapImageRep alloc] initWithData:data];
    [self setTexture:  [ [CCTextureCache sharedTextureCache] addCGImage:[image CGImage] forKey:textureName]];
#endif

我试图在这里允许 Android,但我不知道如果嵌入的纹理确实是 TIFF 它将如何工作,并且 Android 桥接库也期望 PNG/JPEG.