ContentLoadBatch
Introduction
ContentLoadBatch is designed to make loading multiple assets more simple. It does this by allowing you to group together all of the resources associated with a single event (ex. all of the Textures for a single level) to be loaded at once. ContentLoadBatch also provides a method for loading these assets in a separate thread on both the PC and Xbox360, making asynchronous loading less to worry about.
Content Tutorial
Using ContentLoadBatch
Order of Operation
Loading and Unloading assets using ContentLoadBatch consists of four primary steps.
Add the filename of each asset to the ContentLoadBatch object.
Call the ContentLoadBatch's Load() method to load the content.
Use the object's Get method to retrieve the loaded asset.
Call ContentLoadBatch's Unload() method to unload the provided Content Manager when you are finished with the loaded assets.
The ContentLoadBatch is meant to operate in this order and will throw exceptions if you, for example, try to call Get() to retrieve an asset after calling contentLoadBatch.Unload().
Code Example
Before running this example, be sure to follow the following steps to ensure the sample runs as intended.
Download this file (FrblogoHighRes.png) and then drag it into the Content section of the Solution Explorer. It's a larger version of what already exists there, so go ahead and overwrite the file that's currently in the project.
Select Frblogo.png in the Solution Explorer to view its properties. Change the Build Action to “None�, and next to Copy to Output Directory select “Copy if Newer.�
The following is an example that demonstrates how to use ContentLoadBatch for both regular and asynchronous asset loading, and demonstrates the difference between the two. Press 1 to load the logo through regular loading, and press 2 to load the logo asynchronously. Pressing 3 will simply remove the logo from the screen.
When you run this example, the result should be a blank screen with a single moving ball. Pressing 1 to load the FlatRedBall logo pauses the ball's motion while loading, and pressing 2 loads the image in a separate thread without interrupting the ball's motion.
ContentLoadBatch.Load() vs FlatRedBallServices.Load()
Before ContentLoadBatch, the most common way of loading assets would have been to use multiple calls to FlatRedBall.FlatRedBallServices.Load(). For example, if you wanted to simply load a Texture and apply it in your code, you could do something like the following:
This method works great for loading just a few assets, or for when you absolutely have to load something on the fly. However, more often than not all of the assets for a scenario will be loaded at once before the user views it. The above code could have to be repeated dozens of times to load additional textures, sounds and other assets.
Alternatively, you can add the filenames of all assets to be loaded to a ContentLoadBatch and then load them all with one call to ContentLoadBatch.Load() or ContentLoadBatch.LoadAsync(). This allows for a more organized approach to managing content for different scenarios. All benefits of using FlatRedBall.FlatRedBallServices.Load, including content caching for previously loaded resources, are maintained as well.
Related Reading
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
Last updated