0

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.

Job_September_2020
  • 858
  • 2
  • 10
  • 25

1 Answers1

1

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()
Peter Zhong
  • 86
  • 1
  • 7
  • Thank you very much for your excellent answer. I will try your code today, and will mark it "Answered". BTW, I wonder if I use the method with the completion handler "PreloadTextures(SKTextureAtlas[], NSAction)", does it mean that I still can do some other things such as printing out text messages while the preloading is in progress ? Or does it mean that I still can't do any thing else in the code while the preloading is still in progress ? (Thanks again in advance.) – Job_September_2020 Jul 31 '16 at 03:29
  • From what I read in the Apple documentation, this method is run on the background, the xamarin async method is simply a wrapper around it and there is no reason why you can't use the NSAction call back mechanism. Alternatively, you may use Task.ContinueWith to stay in the c# world. – Peter Zhong Jul 31 '16 at 03:52
  • Your explanation is absolutely correct. All async/await methods in C# and iOS only run on the background and do NOT block the flow of the program or app. I try out your code today, and it works perfectly well. That is wonderful. Thank you so much for your help. (Stackoverflow is an awesome forum :-)) – Job_September_2020 Aug 01 '16 at 09:08