This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
UnigineEditor
界面概述
资产工作流程
设置和首选项
项目开发
调整节点参数
Setting Up Materials
Setting Up Properties
照明
Landscape Tool
Sandworm (Experimental)
使用编辑器工具执行特定任务
Extending Editor Functionality
嵌入式节点类型
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Objects
Sound Objects
Pathfinding Objects
Players
编程
搭建开发环境
Usage Examples
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
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
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

事件处理回调

Callback is a function wrapper representing a pointer to static and member functions which are expected to be executed with specified parameters at a certain moment. Callback can be passed as an argument to other function.回调是一个函数包装,表示指向静态和成员函数的指针,这些函数预期在特定时刻使用指定参数执行。回调可以作为参数传递给其他函数。

注意
Callbacks are guaranteed to be reentrant and provide safe multi-threaded execution.确保回调是可重入的,并提供安全的多线程执行。

Unigine C# API lets you specify function delegates as callbacks to handle events. Unigine C#API使您可以将函数委托指定为处理事件的回调。

源代码 (C#)
void node_property_added(Node n, Property property)
{
	/* .. */
}
node.AddCallback(Node.CALLBACK_PROPERTY_NODE_ADD, node_property_added);

You can also use lambda expressions for the same event handling purpose:您也可以将lambda表达式用于相同的事件处理目的:

源代码 (C#)
widget_button.AddCallback(Gui.CLICKED, () => Log.Message("Button pressed\n"));

Practical Use实际使用#

Callbacks are widely used in event handling. A number of Unigine API members have several predefined events which can be handled by using callbacks in certain cases.回调在事件处理中被广泛使用。许多Unigine API成员具有几个预定义的事件,在某些情况下可以使用回调来处理。

Triggers扳机#

Triggers are used to detect changes in nodes position or state. Unigine offers three types of built-in triggers:触发器用于检测节点位置或状态的变化。 Unigine提供了三种类型的内置触发器:

Here is a simple WorldTrigger usage example:这是一个简单的WorldTrigger用法示例:

源代码 (C#)
using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{	
		// implement the enter callback
        void enter_callback(Node node)
        {
			Log.Message("\nA node named {0} has entered the trigger\n", node.Name);
        }
		// implement the leave callback
        void leave_callback(Node node)
        {
			Log.Message("\nA node named {0} has left the trigger\n", node.Name);
        }

		WorldTrigger trigger;
		int enter_callback_id;
		
        public override bool Init()
        {
            // create a world trigger
            trigger = new WorldTrigger(new vec3(3.0f));
            // add the enter callback to be fired when a node enters the world trigger
			//and keep its id to be used to remove the callback when necessary
            enter_callback_id = trigger.AddEnterCallback(enter_callback);
			// add the leave callback to be fired when a node leaves the world trigger
            trigger.AddLeaveCallback(leave_callback);

            return 1;
        }
	}
}

To remove the callbacks use the following code:要删除回调,请使用以下代码:

源代码 (C#)
// remove the callback by using its id
trigger.RemoveEnterCallback(enter_callback_id);
// clear all leave callbacks
trigger.ClearLeaveCallbacks();
也可以看看
  • C++ API示例 ( <UnigineSDK>/source/csharp/samples/Api/Nodes/NodeTrigger ).
  • C++ API示例 ( <UnigineSDK>/source/csharp/samples/Api/Nodes/WorldTrigger ).
  • C++ API示例 ( <UnigineSDK>/source/csharp/samples/Api/Nodes/PhysicalTrigger ).

Widgets小部件#

The widgets base class Widget allows registering callbacks for events defined in the GUI class. The following example shows how to create a WidgetButton and register a callback function for the CLICKED event:小部件基类Widget允许注册GUI类中定义的事件的回调。下面的示例演示如何创建WidgetButton并注册CLICKED事件的回调函数:

源代码 (C#)
// getting the system GUI
Gui gui = Gui.Get();

// creating a button widget and setting its caption
WidgetButton widget_button = new WidgetButton(gui, "Press me");

// rearranging button size
widget_button.Arrange();

// setting button position
widget_button.SetPosition(10,10);

// setting a lambda function to handle CLICKED event
widget_button.AddCallback(Gui.CLICKED, () => Log.Message("Button pressed\n"));

// adding the created button widget to the system GUI
gui.AddChild(widget_button, Gui.ALIGN_OVERLAP | Gui.ALIGN_FIXED);
也可以看看

C# API samples located in the <UnigineSDK>/source/csharp/samples/Api/Widgets folder.位于<UnigineSDK>/source/csharp/samples/Api/Widgets文件夹中的C#API示例。

Physics物理#

You can track certain events of the physics-related Bodies and Joints:您可以跟踪与物理学相关的实体关节的某些事件:

The following sample shows the way of registering callbacks for a BodyRigid and change the color of a mesh depending on its state:下面的示例显示了为BodyRigid注册回调并根据其状态更改网格颜色的方法:

源代码 (C#)
// create a box static mesh
Mesh mesh = new Mesh();
mesh.AddBoxSurface("box_0", new vec3(1.0f));
ObjectMeshStatic node = new ObjectMeshStatic(mesh);
node.SetMaterial("mesh_base", "*");
node.Position = new dvec3(0, 0, 5.0f);

// add a rigid body
BodyRigid body = new BodyRigid(node);

// register callbacks for events by using lambdas
body.AddFrozenCallback(b => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0));
body.AddPositionCallback(b => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(0.0f, 0.0f, 1.0f, 1.0f), 0));
body.AddContactEnterCallback((b, num) => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(1.0f, 1.0f, 0.0f, 1.0f), 0));

// add a shape to the body
ShapeBox shape = new ShapeBox(body, new vec3(1.0f));
注意
Physics-based callbacks are executed in the main tread, as they are mainly used for creation, destruction or modification of other objects.基于物理的回调在主菜单中执行,因为它们主要用于创建,销毁或修改其他对象。
也可以看看

A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Physics/BodyCallbacks folder.位于<UnigineSDK>/source/csharp/samples/Api/Physics/BodyCallbacks文件夹中的C#API示例。

Properties物产#

Callback functions can be used to determine actions to be performed when adding or removing node and surface properties as well as when swapping node properties. Here is an example demonstrating how to track adding a node property via callbacks:回调函数可用于确定在添加或删除节点和曲面属性以及交换节点属性时要执行的操作。这是一个演示如何通过回调跟踪添加节点属性的示例:

源代码 (C#)
using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{	
		// implement the enabled callback
        void node_property_added(Node n, Property property)
        {
			Log.Message("Property \"{0}\" was added to the node named \"{1}\".\n", property.Name, n.Name);
			// ...
        }

		Node node;
		
        public override bool Init()
        {
			// somewhere in the code

			Properties properties = Engine.properties;

			// inheriting a new property named "my_prop" from the base property "node_base"
			properties.FindManualProperty("node_base").Inherit("my_prop");

			// setting our callback function on adding a node property
			node.AddCallback(Node.CALLBACK_PROPERTY_NODE_ADD, node_property_added);

			// adding the property named "my_prop" to the node
			node.AddProperty("my_prop");

            return true;
        }
	}
}

You can add callbacks to track any changes made to a property and its parameters and perform certain actions.您可以添加回调以跟踪对属性及其参数所做的任何更改并执行某些操作。

The example below shows how to add a callback to track changes of property parameters and report the name of the property and the changed parameter (suppose we have a manual property named my_prop with an integer parameter named my_int_param).下面的示例演示如何添加回调以跟踪属性参数的更改,并报告属性的名称和更改后的参数(假设我们有一个名为my_prop的手动属性,其整数参数为my_int_param)。

源代码 (C#)
using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{	
		// implement the parameter changed callback
        void parameter_changed(Property property, int num)
        {
			Log.Message("Parameter \"{0}\" of the property \"{1}\" has changed its value.\n", property.GetParameterPtr(num).Name, property.Name);
			// ...
        }

        public override bool Init()
        {
			// somewhere in the code

			// getting a manual property named "my_prop" via the Property Manager
			Property property = Properties.FindManualProperty("my_prop");

			// setting our callback function on parameter change
			property.AddCallback(Property.CALLBACK_PARAMETER_CHANGED, parameter_changed);

			// changing the value of the "my_int_param" parameter
			property.SetParameterInt(property.FindParameter("my_int_param"), 3);

            return true;
        }
	}
}

You can also add callbacks to the Properties manager to track any changes made to any property and perform certain actions:您还可以将回调添加到Properties管理器,以跟踪对任何属性所做的任何更改并执行某些操作:

源代码 (C#)
public void property_removed(Property property)
{
	Log.Message("Property \"{0}\" was removed.\n", property.getName());
    // ...
}

// somewhere in the code

Properties properties = Engine.properties;

// inheriting a new property named "my_prop" from the base property "surface_base"
Property property = properties.FindManualProperty("surface_base").Inherit("my_prop");

// setting our callback function on property removal
properties.AddCallback(Properties.CALLBACK_REMOVED, property_removed);

// removing the property named "my_prop"
properties.RemoveProperty(properties.FindProperty("my_prop").GetGUID());

See Also也可以看看#

These are not all usage examples of event handling callbacks. The following list contains more API members supporting event handling:这些并不是事件处理回调的所有用法示例。以下列表包含更多支持事件处理的API成员:

  • UserInterface - for handling events from widgets created by loading a UI file. UserInterface-用于处理通过加载UI文件创建的小部件中的事件。
  • Render - callback functions can be used to get access to buffers and matrices at intermediate stages of the rendering sequence. Render-回调函数可用于在渲染序列的中间阶段访问缓冲区和矩阵。
  • Console supports adding a callback function that will be executed when a text is output to the console. Console支持添加在文本输出到控制台时将执行的回调函数。
  • EditorLogic - a set of editor callback functions can be overridden for certain purposes. EditorLogic-出于某些目的,可以覆盖一组编辑器回调函数。
  • ComponentBase - there may be performed certain actions on destroying a component. ComponentBase-在销毁组件时可能会执行某些操作。
  • AsyncQueue - сallback functions can be used to determine actions to be performed when certain resources are loaded. AsyncQueue-所有版本的函数都可以用来确定在加载某些资源时要执行的操作。
  • WorldSplineGraph provides a set of callbacks for handling actions on editing points and segments. WorldSplineGraph提供了一组回调,用于处理对编辑点和线段的操作。
  • Viewport - callback functions can be used to get access to buffers and matrices at intermediate stages of the rendering sequence. Viewport-回调函数可用于在渲染序列的中间阶段访问缓冲区和矩阵。

Plugins:插件:

最新更新: 2020-11-24
Build: ()