I'm developing a CocosSharp game for both iOS and Android and instead of precompiling all the assets (as BundledResource or AndroidAsset) I want to download them when the game starts.
This has already been implemented and saving the assets works fine.
In iOS I'm also able to use the saved assets with CCSprite
for example, so for iOS it's done - great!
But in Android I can't get it to work.
I'm saving the assets to System.Environment.SpecialFolder.Personal
which seems to point to the "./files" folder for the app.
The assets are saved properly and I can see them when I look at the file system.
I've also set the CCApplication.ContentRootDirectory
to be the same folder that contains the assets.
But when I try to create a CCSprite
with one of these assets, it stays blank.CCApplication.Game.Content
also indicates that no assets are loaded.
Does anyone have experience with the thing that I'm trying to do?
Is it even possible to load assets that are not bundled as AndroidAsset in the app?
Hi Ruud,
I'll reuse this code snippet that I provided for loading images from the web, but should be equally applicable — you would just need to instead setup a file stream pointing to your local content.
Task GetStreamAsync()
{
var httpClient = new HttpClient();
return httpClient.GetStreamAsync(@"http://asset_web_page/foo.png");
}
async Task GetImage()
{
using (Stream imageStream = await GetStreamAsync())
{
var texture = new CCTexture2D (imageStream);
var sprite = new CCSprite (texture);
}
}
Hope that helps.
Ruud
This should work for Bitmap Font labels. https://github.com/mono/CocosSharp/issues/268
There is some sample code here: https://github.com/mono/CocosSharp/blob/master/tests/tests/classes/tests/LabelTestNew/LabelFNTColorAndOpacity.cs#L99.
Will be in next release.
Answers
Just figured out that it doesn't apply just to Android, but on iOS as well.
I happened to have some assets left on the device from previous builds on iOS so it looked like it worked.
Would love to get some feedback on this!
Hi Ruud,
I'll reuse this code snippet that I provided for loading images from the web, but should be equally applicable — you would just need to instead setup a file stream pointing to your local content.
Hope that helps.
Brilliant Rami, thanks!
That solves it for textures / sprites.
It's still an issue with fonts for
CCLabel
;I'm using
.fnt
files there together with.png
textures.I was hoping that passing a
CCTexture2D
to theCCLabel
would solve my problems - at least for the textures, not for the.fnt
file) but looking at the source of Cocos, it seems like the texture is completely ignored..Replacing part of the
CCLabel
functionality seems hard because some stuff is made internal, so I'd end up replacing a whole lot of classes.Maybe you have a suggestion for this as well?
Anyway, I'm already helped a lot with just the sprite textures; I could live with bundling the fonts :-)
Ruud
This should work for Bitmap Font labels. https://github.com/mono/CocosSharp/issues/268
There is some sample code here: https://github.com/mono/CocosSharp/blob/master/tests/tests/classes/tests/LabelTestNew/LabelFNTColorAndOpacity.cs#L99.
Will be in next release.
Thanks Kenneth, that's awesome!!
It was the last step to making everything in my game update-able
Hey @RamiTabbara this does not work on Android anymore. Creating the CCTexture2D with a stream like this:
var texture = new CCTexture2D (imageStream);
will result in the application hanging. It seems to be a problem in the constructor for Texture2D. More specifically it's in the following method:
PlatformConstruct(width, height, mipmap, format, type, shared);
Please help me fix this since it is very limiting to have everything compiled with the application.
In case anyone else sees the 'hanging' @PeterKrusager described, I experienced this too. The problem appears to happen when the CCTexture2D constructor is called on the wrong thread.