Search code examples
c#xamarin.iospreloadtexture-atlassktextureatlas

How to preload texture atlas (SKTextureAtlas) in Xamarin.iOS (in C#)?


Would anyone please show me a small code sample written in C# within the Xamarin.iOS framework that would preload texture atlases ?

Xamarin website has this page:

https://developer.xamarin.com/api/type/MonoTouch.SpriteKit.SKTextureAtlas/

On that webpage, they list the many methods that preload the texture atlases. But, I am only interested in these 2 specific methods:

PreloadTextures(SKTextureAtlas[], NSAction)

PreloadTexturesAsync(SKTextureAtlas[])

I guess both methods will work well for my game. Unfortunately, I don't know how to properly call them in C# within the Xamarin.iOS framework.

While there are many code samples for preloading texture atlases on the web, these samples are written in Objective-C, and, unfortunately, I don't know how to translate Objective-C code to C# code yet.

So, I would greatly appreciate if you could show me how to write some small code samples in C# within the Xamarin.iOS framework that preload texture atlases, using the 2 methods that I mentioned above.

Thank you very much.


Solution

  • I am not an expert on game development nor have i been involved in one before;however, I am reasonably familiar with the Xamarin.iOS to sort of "translate" the Objective-C codes to Xamarin C# codes.

    First you need to create a "texture atlas" array

    var textureCollection = new SKTextureAtlas[]
    {
        SKTextureAtlas.FromName("firsttexturename"),
        SKTextureAtlas.FromName("secondtexturename"),
    };
    await SKTextureAtlas.PreloadTexturesAsync(textureCollection);
    // rather than using a completion handler like in ObjC xamarin uses the c# 5's await keyword to achieve similar functions
    //You can do what you want after it has preloaded here 
    //for example
    this.StartScene()