Kanzi学习之路(7):kanzi的资源预加载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010977122/article/details/72599003

为了便于资源文件的管理,kanzi有着一套自己的资源文件管理系统,将所有的资源文件打包进.kzb文件中。但是资源文件又很庞大,为了加快开机速度,应用程序的响应速度,很多时候我们要选择预加载资源,多线程加载资源。今天我们来看看kanzi如何完成我们的需求。

并行加载资源

当用户在多核处理器的环境下运行kanzi程序的时候,kanzi会自动使用多个CPU核加载kzb文件里的GPU资源到RAM。因为在加载过程中最大的数据量是由GPU资源处理的,因此并行加载资源可以显著的提高应用的加载时间。

kanzi并行加载的GPU资源包括各种纹理、着色器和网格数据。为了将这些资源从RAM部署到GPU内存,加载一些预制的模板,kanzi总是使用主线程。

在kanzi中,你可以在app展示给用户之前,加载在你的工程中要使用的模板的资源字典。比如,你可以创建一个展示给用户看的加载窗口,而后台却在加载app的剩余的资源字典,一旦加载完成,就从加载页面跳到你选择的应用的部分的界面。

不同的平台,用于加载的线程的数量也是不同的。当针对你的平台使用了最优化的数量的加载线程,那么你的应用程序的启动和整个加载时间都会得到减少。


预加载资源字典

1、在Screen->RootPage下创建加载界面或者加载动画。

2、在Prefabs下创建你的kanzi应用程序。

3、在Project窗口,选中RootPage,在Properties窗口点击Add,添加On Attached触发器。

扫描二维码关注公众号,回复: 4959731 查看本文章

4、在Properties窗口下,On Attached触发器下点击Add下拉菜单,选择Preload Resources行为。

5、在Preload Resources行为的参数编辑框中,在Resource Dictionaries For Preloading属性中,选择你想预加载的资源字典。

6、在Project窗口选中Screen控件,在Trigers面板添加On Preloading of Resources Completed 触发器。

7、为On Preloading of Resources Completed 触发器添加Execute Script行为。

8、在Execute Script窗口选择<add new script...>创建新的脚本。

9、编辑运行脚本:

// Find the RootPage object that is the parent of the Loading screen
// object you want to replace when preloading of resource is completed.
var rootPage = node.lookupNode('./RootPage');
// Find the Loading screen object you want to replace.
var loadingScreen = node.lookupNode('./RootPage/Loading screen');
// Instantiate the prefab that contains the content you want to show
// after you remove the Loading screen object.
var firstScreen = instantiatePrefab('kzb://<ProjectName>/Prefabs/Viewport 2D', 'First screen');
// Remove the Loading screen object.
rootPage.removeChild(loadingScreen);
// Add the prefab named First screen to the RootPage.
rootPage.addChild(firstScreen);



设置加载核的数量

在应用的代码中,使用应用的框架配置或者资源管理的API,你都可以去设置应用程序中用于加载GPU资源的核心数。

在设置核心数的时候,你也可以设置管理内存的大小。如果你不设置管理内存的大小,或者设置为0,并且加载的线程数大于0的话,kanzi会自动使用主内存池的一半大小用于资源的加载。在C++代码中,可以在onConfigure()函数里进行设置:

// Set loadingThreadCount to the number of cores in the processor
// of your target device.
// For example, to use all four cores of a four-core processor,
// set the value to 3, to use only two cores, set the value to 1.
configuration.loadingThreadCount = 3;

// Set the size of the memory manager between half and full amount allocated
// for the memory pool of your application.
// For example, if you allocated 200 MB for the memory pool, set 
// the size of the memory manager between 100 and 200 MB.
configuration.loadingThreadsMemoryManagerSize = 100 * 1024 * 1024;


此外,你还可以在C++中的onConfigure()函数或者工程配置文件applicat.cfg中来进行偏好设置。(请参考下一篇)


取消并行加载

取消并行加载,只需要将loadingThreadCount设置为0就可以了,这时,kanzi就会使用主线程来加载资源,而且,kanzi不会分配内存管理。



猜你喜欢

转载自blog.csdn.net/u010977122/article/details/72599003