This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
专业(SIM)
UnigineEditor
界面概述
资源工作流程
版本控制
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

Unigine::AsyncQueue Class

Header: #include <UnigineAsyncQueue.h>

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:

  1. 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.
  2. 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:

To get a loaded resource (image, mesh, or node) the following 2 groups of methods can be used:

For example:

AsyncSample.cpp
// specify relative paths to resources to be loaded to a queue
static const char *meshes[] = {
	"meshes/box.mesh",
	"meshes/capsule.mesh",
	"meshes/cbox.mesh",
};

#define ARRAY_LENGTH(array) static_cast<int>(sizeof(array) / sizeof(array[0]))

struct AsyncLoadRequest
{
	String name;
	int id;
};

Vector<AsyncLoadRequest> mesh_load_requests;

void AsyncSample::init()
{

	// enqueue meshes to load
	for (int i = 0; i < ARRAY_LENGTH(meshes); ++i)
	{
		const char *name = meshes[i];
		AsyncLoadRequest request;
		request.name = name;
		request.id = AsyncQueue::loadMesh(name);
		mesh_load_requests.append(request);
	}

}

void AsyncSample::update()
{
	// check mesh load requests
	for (int i = 0; i < mesh_load_requests.size(); ++i)
	{
		const auto &r = mesh_load_requests[i];

		if (!AsyncQueue::checkMesh(r.id))
			continue;

		// remove the mesh from the list of the loaded meshes and print a message
		AsyncQueue::removeMesh(r.id);
		Log::message("Loaded mesh \"%s\"\n", r.name.get());

		mesh_load_requests.remove(i--);
	}
}

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.

Notice
Do not call take-methods inside a callback function unless you are absolutely sure, that the resource is yours, otherwise internal engine managers will be forced to load it again and again every time. In such cases it is recommended to use get-methods.
AsyncSample.cpp
// specify relative paths to resources to be loaded to a queue
static const char *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",
};

#define ARRAY_LENGTH(array) static_cast<int>(sizeof(array) / sizeof(array[0]))

void AsyncSample::init()
{

	// enqueue textures to load
	for (int i = 0; i < ARRAY_LENGTH(textures); ++i)
		AsyncQueue::loadImage(textures[i]);
	// add an image loaded callback 
	AsyncQueue::addCallback(AsyncQueue::CALLBACK_IMAGE_LOADED, MakeCallback(this, &AsyncSample::image_loaded));

}

// an image loaded callback function
void AsyncSample::image_loaded(const char *name, int id)
{
	AsyncQueue::removeImage(id);
	Log::message("Image \"%s\" loaded, ID: %d\n", name, id);
}
Notice
This class is a singleton.

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#

Name说明/描 述
CALLBACK_BEGIN = 0Beginning of the callbacks range.
CALLBACK_FILE_LOADED = 0File loaded callback.
CALLBACK_IMAGE_LOADED = 1Image loaded callback.
CALLBACK_MESH_LOADED = 2Mesh loaded callback.
CALLBACK_NODE_LOADED = 3Node loaded callback.
CALLBACK_END = 3End of the callbacks range.
NUM_CALLBACKS = 4Callback counter.

Members


void * addCallback ( CALLBACK_INDEX callback, Unigine::CallbackBase2< const char *, int > * func ) #

Adds a callback of the specified type. Callback functions can be used to determine actions to be performed when certain resources are loaded.
Notice
Do not call take-methods inside a callback function unless you are absolutely sure, that the resource is yours, otherwise internal engine managers will be forced to load it again and again every time. In such cases it is recommended to use get-methods.

Arguments

  • CALLBACK_INDEX callback - Callback type. One of the CALLBACK_* variables.
  • Unigine::CallbackBase2< const char *, int > * func - Callback pointer. Callback function must have the following signature: void Callback(const char * 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.

Ptr<Image> getImage ( int id ) #

Returns the image loaded by the specified operation.
Notice
This method does not remove the image from the list of loaded ones. If you want the image to be removed from this list right after retrieving it, use the takeImage() method.

Arguments

  • int id - Loading operation identifier (see the loadImage() method).

Return value

Image smart pointer.

Ptr<Mesh> getMesh ( int id ) #

Returns the mesh loaded by the specified operation.
Notice
This method does not remove the mesh from the list of loaded ones. If you want the mesh to be removed from this list right after retrieving it, use the takeMesh() method.

Arguments

  • int id - Loading operation identifier (see the loadMesh() method).

Return value

Mesh smart pointer.

Ptr<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.
Notice
This method does not remove the node from the list of loaded ones. If you want the node to be removed from this list right after retrieving it, use the takeNode() method.

Arguments

  • int id - Loading operation identifier (see the loadNode() method).

Return value

Node smart pointer.

int getNodes ( int id, Vector< Ptr<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.
Notice
This method does not remove the nodes from the list of loaded ones. If you want the nodes to be removed from this list right after retrieving them, use the takeNodes() method.

Arguments

  • int id - Loading operation identifier.
  • Vector< Ptr<Node> > & nodes - Array of loaded nodes' smart pointers.

Return value

1 if the operation is successful; otherwise, 0.

int getNumLoadedData ( ) #

Returns the total number of loaded data segments.

Return value

Total number of loaded data segments.

int getNumLoadedFiles ( ) #

Returns the total number of loaded files.

Return value

Total number of loaded files.

int getNumLoadedImages ( ) #

Returns the total number of queued loaded.

Return value

Total number of queued loaded.

int getNumLoadedMeshes ( ) #

Returns the total number of loaded meshes.

Return value

Total number of loaded meshes.

int getNumLoadedNodes ( ) #

Returns the total number of loaded nodes.

Return value

Total number of loaded nodes including currently processed ones.

int getNumLoadedResources ( ) #

Returns the total number of currently loaded resources. The return value represents the sum of currently loaded data, files, images and meshes.

Return value

Total number of loaded resources.

int getNumQueuedData ( ) #

Returns the total number of queued data segments waiting for the background loading. The return value also includes currently processed ones.

Return value

Total number of queued data segments including currently processed ones.

int getNumQueuedFiles ( ) #

Returns the total number of queued files waiting for the background loading. The return value also includes currently processed ones.

Return value

Total number of queued files including currently processed ones.

int getNumQueuedImages ( ) #

Returns the total number of queued images waiting for the background loading. The return value also includes currently processed ones.

Return value

Total number of queued images including currently processed ones.

int getNumQueuedMeshes ( ) #

Returns the total number of queued meshes waiting for the background loading. The return value also includes currently processed ones.

Return value

Total number of queued meshes including currently processed ones.

int getNumQueuedNodes ( ) #

Returns the total number of queued nodes waiting for the background loading. The return value also includes currently processed ones.

Return value

Total number of queued nodes including currently processed ones.

int getNumQueuedResources ( ) #

Returns the total number of queued resources waiting for background loading. The return value represents the sum of queued and currently processed data, files, images and meshes.

Return value

Total number of queued resources including currently processed ones.

float getTotalTime ( ) #

Returns the total time it took to process the loading queue.

Return value

Total processing time, in milliseconds.

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

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.
Notice
The file won't be loaded immediately after calling the method as it can be large.

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.
Notice
The image won't be loaded immediately after calling the method as it can be large.

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.
Notice
The mesh won't be loaded immediately after calling the method as it can be large.

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.
Notice
The node won't be loaded immediately after calling the method as it can be large.

Arguments

  • int id - Loading operation identifier (see the loadNode() method).

Return value

1 if the node is loaded successfully; otherwise, 0.

int loadFile ( const char * name, int group = 0, float weight = 0.0f ) #

Loads a given file with the specified group and priority to the thread.

Arguments

  • const char * 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 ( const char * name, int group = 0, float weight = 0.0f ) #

Loads a given image file with the specified group and priority to the thread.

Arguments

  • const char * 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 ( const char * name, int group = 0, float weight = 0.0f ) #

Loads a given mesh-file with the specified group and priority to the thread.

Arguments

  • const char * 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 ( const char * name, int group = 0, float weight = 0.0f ) #

Loads a given node-file with the specified group and priority to the thread.
Notice
In order to display such asynchronously loaded node, the updateEnabled() method should be called for it from the main thread.

Arguments

  • const char * 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, void * 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

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.
Notice
If the specified file is currently loading, it will be removed after the loading operation is completed.

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.
Notice
If the specified image is currently loading, it will be removed after the loading operation is completed.

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.
Notice
If the specified mesh is currently loading, it will be removed after the loading operation is completed.

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.
Notice
If the specified node is currently loading, it will be removed after the loading operation is completed. Nodes are removed with all their hierarchy.

Arguments

  • int id - Loading operation identifier (see the loadNode() method).

Return value

1 if the node is removed successfully; otherwise, 0.

Ptr<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 smart pointer.

Ptr<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 smart pointer.

Ptr<Node> takeNode ( int id ) #

Returns the node loaded by the specified operation and removes it from the list of loaded nodes.
Notice
Nodes are removed with all their hierarchy.

Arguments

  • int id - Loading operation identifier (see the loadNode() method).

Return value

Node smart pointer.

int takeNodes ( int id, Vector< Ptr<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.
Notice
Nodes are removed with all their hierarchy.

Arguments

  • int id - Loading operation identifier.
  • Vector< Ptr<Node> > & nodes - Array of loaded nodes' smart pointers.

Return value

1 if the operation is successful; otherwise, 0.

int loadImageInfo ( const char * 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

  • const char * 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.
Last update: 2023-06-23
Build: ()