如何在Xamarin.iOS(在C#中)预加载纹理图集(SKTextureAtlas)?

时间:2016-07-30 12:10:43

标签: c# xamarin.ios preload texture-atlas sktextureatlas

请问有人在Xamarin.iOS框架中向我展示一个用C#编写的小代码示例,它会预加载纹理图集吗?

Xamarin网站有这个页面:

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

在该网页上,他们列出了许多预加载纹理地图集的方法。但是,我只对这两种具体方法感兴趣:

PreloadTextures(SKTextureAtlas [],NSAction)

PreloadTexturesAsync(SKTextureAtlas [])

我猜这两种方法都适用于我的游戏。不幸的是,我不知道如何在Xamarin.iOS框架内用C#正确调用它们。

虽然有许多代码示例用于在Web上预加载纹理图集,但这些示例是用Objective-C编写的,不幸的是,我不知道如何将Objective-C代码转换为C#代码。

所以,如果你能告诉我如何使用上面提到的2种方法在Xamarin.iOS框架中编写一些预装纹理地图集的C#中的一些小代码样本,我将不胜感激。

非常感谢。

1 个答案:

答案 0 :(得分:1)

我不是游戏开发方面的专家,也不是我之前参与过的游戏;但是,我对Xamarin.iOS非常熟悉,可以将Objective-C代码“翻译”为Xamarin C#代码。

首先,您需要创建一个“纹理图集”数组

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()