Unigine.AsyncQueue Class
Loading of files/meshes/images/nodes on demand is performed in a separate system thread, in addition to asynchronous content streaming. This class is used to control asynchronous data loading. It is possible to control loading queue by organizing files in groups and setting individual weights inside these groups.
To load files on demand, call the functions in the following order:
- First LoadFile() function is called to place the file in the queue for loading and obtain an ID, that will be used to identify a resource in subsequent operations.
- After that, different operations can be performed:
- ForceFile() to load the file immediately.
- RemoveFile() to remove the file from the queue in case there is no need to load it any more of to remove a file from the list of loaded files if it was loaded but no longer needed.
- CheckFile() to check if the given file was requested to be loaded via loadFile() or if it has already been loaded.
The same algorithm is true for:
- Meshes - see methods LoadMesh(), ForceMesh(), GetMesh(), TakeMesh(), CheckMesh(), and RemoveMesh().
- Images - see methods LoadImage(), ForceImage(), GetImage(), TakeImage(), CheckImage(), and RemoveImage().
- Nodes - see methods LoadNode(), ForceNode(), GetNode(), TakeNode(), CheckNode(), and RemoveNode().
To get a loaded resource (image, mesh, or node) the following 2 groups of methods can be used:
- Get-methods - these methods return the loaded resource:
- Take-methods - these methods return the loaded resource and remove it from the list of loaded ones:
For example:
// specify relative paths to resources to be loaded to a queue
static string[] meshes = {
"meshes/box.mesh",
"meshes/capsule.mesh",
"meshes/cbox.mesh",
};
struct AsyncLoadRequest
{
public string name;
public int id;
};
List<AsyncLoadRequest> mesh_load_requests;
private void Init()
{
mesh_load_requests = new List<AsyncLoadRequest>();
// enqueue meshes to load
for (int i = 0; i < meshes.Length; ++i)
{
string name = meshes[i];
AsyncLoadRequest request;
request.name = name;
request.id = AsyncQueue.LoadMesh(name);
mesh_load_requests.Add(request);
}
}
private void Update()
{
// check mesh load requests
for (int i = 0; i < mesh_load_requests.Count; ++i)
{
var r = mesh_load_requests[i];
if (AsyncQueue.CheckMesh(r.id) == 0)
continue;
// remove the mesh from the list of the loaded meshes and print a message
AsyncQueue.RemoveMesh(r.id);
Unigine.Log.Message("Loaded mesh {0}\n", r.name);
mesh_load_requests.Remove(r);
}
}
Adding Callbacks
You can add "resource loaded" callbacks for files, images, meshes or nodes and perform certain actions as a resource is loaded. The example below shows how to add a "loaded" callback for image resource, that reports the name and id of the loaded image.
// specify relative paths to resources to be loaded to a queue
static string[] textures = {
"textures/black_d.dds",
"textures/blue_d.dds",
"textures/green_d.dds",
"textures/orange_d.dds",
"textures/red_d.dds",
"textures/white_d.dds",
"textures/yellow_d.dds",
};
private void Init()
{
// enqueue textures to load
for (int i = 0; i < textures.Length; ++i)
AsyncQueue.LoadImage(textures[i]);
// add an image loaded callback
AsyncQueue.AddCallback(AsyncQueue.CALLBACK_INDEX.IMAGE_LOADED, image_loaded);
}
// an image loaded callback function
private void image_loaded(string name, int id)
{
AsyncQueue.RemoveImage(id);
Log.Message("Image {0} loaded, ID: {0}\n", name, id);
}
See Also#
A set of UnigineScript API samples located in the <UnigineSDK>/data/samples/systems/ folder:
- async_02
- stream_00
AsyncQueue Class
Enums
CALLBACK_INDEX#
Properties
int NumLoadedResources#
int NumLoadedNodes#
int NumLoadedMeshes#
int NumLoadedImages#
int NumLoadedFiles#
int NumLoadedData#
int NumQueuedResources#
int NumQueuedNodes#
int NumQueuedMeshes#
int NumQueuedImages#
int NumQueuedFiles#
int NumQueuedData#
float TotalTime#
Members
IntPtr addCallback ( CALLBACK_INDEX callback, CallbackDelegate func ) #
Adds a callback of the specified type. Callback functions can be used to determine actions to be performed when certain resources are loaded.Arguments
- CALLBACK_INDEX callback - Callback type. One of the CALLBACK_* variables.
- CallbackDelegate func - Callback function with the following signature: void CallbackDelegate(string arg1, int arg2)
Return value
ID of the last added callback of the specified type, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.Image GetImage ( int id ) #
Returns the image loaded by the specified operation.Arguments
- int id - Loading operation identifier (see the LoadImage() method).
Return value
Image instance.Mesh GetMesh ( int id ) #
Returns the mesh loaded by the specified operation.Arguments
- int id - Loading operation identifier (see the LoadMesh() method).
Return value
Mesh instance.Node GetNode ( int id ) #
Returns the node loaded by the specified operation. If the node loaded by the specified operation consists of multiple objects, a new dummy object to combine them is created and its smart pointer is returned.Arguments
- int id - Loading operation identifier (see the LoadNode() method).
Return value
Node instance.int GetNodes ( int id, Node[] nodes ) #
Puts the nodes loaded by the specified operation to the specified array. If the node loaded by the specified operation consists of multiple objects, they are put into the array.Arguments
- int id - Loading operation identifier.
- Node[] nodes - Array of loaded nodes' instances.
Return value
1 if the operation is successful; otherwise, 0.int CheckFile ( int id ) #
Returns a value indicating if the file is in the loading queue or already loaded.Arguments
- int id - Loading operation identifier (see the LoadFile() method).
Return value
1 if the file is in the loading queue or already loaded; otherwise, 0.int CheckImage ( int id ) #
Returns a value indicating if the image is in the loading queue or already loaded.Arguments
- int id - Loading operation identifier (see the LoadImage() method).
Return value
1 if the image is in the loading queue or already loaded; otherwise, 0.int CheckMesh ( int id ) #
Returns a value indicating if the mesh is in the loading queue or already loaded.Arguments
- int id - Loading operation identifier (see the LoadMesh() method).
Return value
1 if the mesh is in the loading queue or already loaded; otherwise, 0.int CheckNode ( int id ) #
Returns a value indicating if the node is in the loading queue or already loaded.Arguments
- int id - Loading operation identifier (see the LoadNode() method).
Return value
1 if the node is in the loading queue or already loaded; otherwise, 0.void clearCallbacks ( CALLBACK_INDEX callback ) #
Clears all added callbacks of the specified type. Callback functions can be used to determine actions to be performed when certain resources are loaded.Arguments
- CALLBACK_INDEX callback - Callback type. One of the CALLBACK_* variables.
int ForceFile ( int id ) #
Forces loading of the file. The specified file will be loaded right after the currently loading file (if any) is processed. All other file system operations are suspended until the forced file is loaded.Arguments
- int id - Loading operation identifier (see the LoadFile() method).
Return value
1 if the file is loaded successfully; otherwise, 0.int ForceImage ( int id ) #
Forces loading of the image. The specified image will be loaded right after the currently loading image (if any) is processed. All other file system operations are suspended until the forced image is loaded.Arguments
- int id - Loading operation identifier (see the LoadImage() method).
Return value
1 if the image is loaded successfully; otherwise, 0.int ForceMesh ( int id ) #
Forces loading of the mesh. The specified mesf will be loaded right after the currently loading mesh (if any) is processed. All other file system operations are suspended until the forced mesh is loaded.Arguments
- int id - Loading operation identifier (see the LoadMesh() method).
Return value
1 if the mesh is loaded successfully; otherwise, 0.int ForceNode ( int id ) #
Forces loading of the node. The specified node will be loaded right after the currently loading node (if any) is processed. All other file system operations are suspended until the forced node is loaded.Arguments
- int id - Loading operation identifier (see the LoadNode() method).
Return value
1 if the node is loaded successfully; otherwise, 0.int LoadFile ( string name, int group = 0, float weight = 0.0f ) #
Loads a given file with the specified group and priority to the thread.Arguments
- string name - Absolute or relative path to the file (including its name).
- int group - Priority group. Greater priority means faster loading. The default value is 0.
- float weight - Weight of the priority inside the group. Greater weight means faster loading inside the same priority group. The default value is 0.0f.
Return value
Loading operation identifier.int LoadImage ( string name, int group = 0, float weight = 0.0f ) #
Loads a given image file with the specified group and priority to the thread.Arguments
- string name - Absolute or relative path to the image file (including its name).
- int group - Priority group. Greater priority means faster loading. The default value is 0.
- float weight - Weight of the priority inside the group. Greater weight means faster loading inside the same priority group. The default value is 0.0f.
Return value
Loading operation identifier.int LoadMesh ( string name, int group = 0, float weight = 0.0f ) #
Loads a given mesh-file with the specified group and priority to the thread.Arguments
- string name - Absolute or relative path to the *.mesh file (including its name).
- int group - Priority group. Greater priority means faster loading. The default value is 0.
- float weight - Weight of the priority inside the group. Greater weight means faster loading inside the same priority group. The default value is 0.0f.
Return value
Loading operation identifier.int LoadNode ( string name, int group = 0, float weight = 0.0f ) #
Loads a given node-file with the specified group and priority to the thread.Arguments
- string name - Absolute or relative path to the *.node file (including its name).
- int group - Priority group. Greater priority means faster loading. The default value is 0.
- float weight - Weight of the priority inside the group. Greater weight means faster loading inside the same priority group. The default value is 0.0f.
Return value
Loading operation identifier.bool removeCallback ( CALLBACK_INDEX callback, IntPtr id ) #
Removes a callback with the given ID from the list of callbacks of the specified type. Callback functions can be used to determine actions to be performed when certain resources are loaded.Arguments
- CALLBACK_INDEX callback - Callback type. One of the CALLBACK_* variables.
- IntPtr id - Callback ID obtained when adding it.
Return value
True if the callback with the given ID was removed successfully; otherwise false.int RemoveFile ( int id ) #
Removes the given file from the loading queue or from the list of loaded files, if it was already loaded.Arguments
- int id - Loading operation identifier (see the LoadFile() method).
Return value
1 if the file is successfully removed; otherwise, 0.int RemoveImage ( int id ) #
Removes the given image from the loading queue or from the list of loaded images, if it was already loaded.Arguments
- int id - Loading operation identifier (see the LoadImage() method).
Return value
1 if the image is successfully removed; otherwise, 0.int RemoveMesh ( int id ) #
Removes the given mesh from the loading queue or from the list of loaded meshes, if it was already loaded.Arguments
- int id - Loading operation identifier (see the LoadMesh() method).
Return value
1 if the mesh is successfully removed; otherwise, 0.int RemoveNode ( int id ) #
Removes the given node from the loading queue or from the list of loaded nodes, if it was already loaded.Arguments
- int id - Loading operation identifier (see the LoadNode() method).
Return value
1 if the node is removed successfully; otherwise, 0.Image TakeImage ( int id ) #
Returns the image loaded by the specified operation and removes it from the list of loaded images.Arguments
- int id - Loading operation identifier (see the LoadImage() method).
Return value
Image instance.Mesh TakeMesh ( int id ) #
Returns the mesh loaded by the specified operation and removes it from the list of loaded meshes.Arguments
- int id - Loading operation identifier (see the LoadMesh() method).
Return value
Mesh instance.Node TakeNode ( int id ) #
Returns the node loaded by the specified operation and removes it from the list of loaded nodes.Arguments
- int id - Loading operation identifier (see the LoadNode() method).
Return value
Node instance.int TakeNodes ( int id, Node[] nodes ) #
Puts the nodes loaded by the specified operation to the specified array and removes them from the list of loaded nodes. If the node loaded by the specified operation consists of multiple objects, they are put into the array.Arguments
- int id - Loading operation identifier.
- Node[] nodes - Array of loaded nodes' instances.
Return value
1 if the operation is successful; otherwise, 0.int LoadImageInfo ( string name, int group = 0, float weight = 0.0f ) #
Loads file info (size format, etc.) for the specified image, group and priority to the thread.Arguments
- string name - Absolute or relative path to the image file (including its name).
- int group - Priority group. Greater priority means faster loading. The default value is 0.
- float weight - Weight of the priority inside the group. Greater weight means faster loading inside the same priority group. The default value is 0.0f.