应用逻辑系统
In UNIGINE, you can implement application logic in the following ways:在 UNIGINE 中,您可以通过以下方式实现应用程序逻辑:
- in C++ or C#在C++或者C#
- combining C++ or C# with UnigineScript — in this case, the execution sequence for each implemented method, corresponding to a certain stage of the application's life cycle (i.e., init(), update(), postUpdate(), etc.), is the following:
- UnigineScript functionUnigineScript function
- C++/C# methodC++/C# method
- UnigineScript functionUnigineScript 函数
- C++/C# methodC++/C# 方法
The application logic has three basic components that have different lifetimes: 应用程序逻辑具有三个具有不同生命周期的基本组件:
- World logic. It takes effect only when the world is loaded. For projects that use UnigineScript only, the logic is written to the world script (each particular world can have its own script). A default script is created as a *.usc file named after your project at the project creation. Such files are stored in the data/ folder.
For applications that use C++ AppWorldLogic.cpp is created, and for C# applications — AppWorldLogic.cs. These files are stored in the <your_project_folder>/source/ folder and stay loaded during the whole engine runtime. They have implemented methods to put your logic code inside. WorldLogic in C++/C# (unlike UnigineScript) works with every loaded world, so you can't assign a specific WorldLogic to a certain world. World logic。只有在加载世界时才会生效。对于仅使用 UnigineScript 的项目,逻辑将写入世界脚本(每个特定世界可以有自己的脚本)。 默认脚本创建为 *.usc 文件,在项目创建时以您的项目命名。 此类文件存储在 data/ 文件夹中。
对于使用 C++ 的应用程序,创建了 AppWorldLogic.cpp,对于 C# 应用程序,创建了 AppWorldLogic.cs。该文件存储在 <your_project_folder>/source/ 文件夹中,并在整个引擎运行时保持加载状态。它已经实现了将逻辑代码放入其中的方法。 - System logic. It exists during the application life cycle. The logic is implemented in the default system script named unigine.usc and stored in the data/ folder of your project as the core.ung archive. You can put your logic in this file if you use UnigineScript only. You can also inherit SystemLogic class and implement your logic in C++ or C#. System logic。它存在于应用程序生命周期中。该逻辑在名为 unigine.usc 的默认系统脚本中实现,并作为 core.ung 存档存储在项目的 data/ 文件夹中。如果您只使用 UnigineScript,您可以将您的逻辑放在这个文件中。您还可以继承 SystemLogic 类并在 C++ 或 C# 中实现您的逻辑。 C++/C# 中的 WorldLogic(与 UnigineScript 不同)适用于每个加载的世界,因此您不能将特定的 WorldLogic 分配给某个世界。
- Editor logic. This component is to be used in case you need to implement your own Editor. It has more implemented methods providing you with clear understanding of the current Engine events (a node has been created, a property has been deleted, a material has been changed, etc.). You can inherit EditorLogic class and implement your logic in C++ or C#. Or you can create a UnigineScript file and add it using the -editor_script path_to_file.usc command-line argument.
When you create a new C++ / C# project, it has already inherited World Logic class (AppWorldLogic), System Logic class (AppSystemLogic), and Editor Logic class (AppEditorLogic) with implemented methods to put your logic code inside. Editor logic。如果您需要实现自己的编辑器,将使用此组件。 它有更多的实现方法,让您清楚地了解当前的引擎事件(节点已创建、属性已删除、材质已更改等)。你可以继承EditorLogic 类并在 C++ 或 C# 中实现您的逻辑。 或者您可以创建一个 UnigineScript 文件并使用"-editor_script path_to_file.usc"命令行参数。When you create a new C++ / C# project, it has already inherited World Logic class (AppWorldLogic), System Logic class (AppSystemLogic), and Editor Logic class (AppEditorLogic) with implemented methods to put your logic code inside. When you create a new C++ / C# project, it has already inherited World Logic class (AppWorldLogic), System Logic class (AppSystemLogic), and Editor Logic class (AppEditorLogic) with implemented methods to put your logic code inside.When you create a new C++ / C# project, it has already inherited World Logic class (AppWorldLogic), System Logic class (AppSystemLogic), and Editor Logic class (AppEditorLogic) with implemented methods to put your logic code inside. 当您创建一个新的 C++/C# 项目时,它已经继承了 World Logic 类(AppWorldLogic)、系统逻辑类(AppSystemLogic)和编辑器逻辑类(AppEditorLogic) 和已实现的方法将您的逻辑代码放入其中。
If a project uses UnigineScript only, its execution sequence is strictly defined in scripts and cannot be modified.
If a project is created using C++ or C#, a programmer can control the execution sequence by manually calling Engine's init(), update(), postUpdate(), swap(), and shutdown() methods when required and add logic anywhere between these blocks, as shown in the C++ sample <UnigineSDK>/source/samples/Api/Render/Screenshot/Screenshot.cpp.如果一个项目只使用 UnigineScript,它的执行顺序
如果项目是使用C++或C#创建的,程序员可以在需要时手动调用Engine的init(), update(), postUpdate(), swap()和shutdown()方法来控制执行顺序并添加逻辑这些块之间的任何位置,如 C++ 示例 <UnigineSDK>/source/samples/Api/Render/Screenshot/Screenshot.cpp 中所示。
World Logic世界逻辑(World Logic)#
If the World Logic is implemented in UnigineScript, it is loaded or unloaded at the same time as the world itself is loaded or unloaded. The World Logic implemented in C++/C# stays loaded during the whole UNIGINE engine runtime. The World Logic may use the following methods, which are available for both UnigineScript and C++/C#: 如果世界逻辑是在 UnigineScript 中实现的,它会在加载或卸载世界本身的同时加载或卸载。用 C++/C# 实现的世界逻辑在整个 UNIGINE 引擎运行时保持加载状态。 World Logic 可以使用以下方法,这些方法可用于 UnigineScript 和 C++/C#:
#include "AppWorldLogic.h"
AppWorldLogic::AppWorldLogic() {
}
AppWorldLogic::~AppWorldLogic() {
}
int AppWorldLogic::init() {
return 1;
}
int AppWorldLogic::update() {
return 1;
}
int AppWorldLogic::postUpdate() {
return 1;
}
int AppWorldLogic::updatePhysics() {
return 1;
}
int AppWorldLogic::shutdown() {
return 1;
}
int AppWorldLogic::save(const Unigine::StreamPtr &stream) {
UNIGINE_UNUSED(stream);
return 1;
}
int AppWorldLogic::restore(const Unigine::StreamPtr &stream) {
UNIGINE_UNUSED(stream);
return 1;
}
init() functioninit() function#
Engine calls this function on the world initialization. The code inside should initialize resources for your world scene during the world start.引擎在世界初始化时调用这个函数。里面的代码应该在世界开始时为你的世界场景初始化资源。
updateAsyncThread() functionupdateAsyncThread() function#
In the updateAsyncThread() function, you can specify all logic functions you want to be called every frame independently of the rendering thread. This function does not block the Main Thread.在 updateAsyncThread() 函数中,您可以独立于渲染线程指定每帧要调用的所有逻辑函数。该函数不会阻塞主线程。
updateSyncThread() functionupdateSyncThread() function#
In the updateSyncThread() function, you can specify any parallel logic functions to be executed before the Update(). This function blocks the Main Thread until all calls are completed.在 updateSyncThread() 函数中,您可以指定要在 Update() 之前执行的任何并行逻辑函数。该函数会阻塞主线程,直到所有调用完成。
update() functionupdate() function#
In the update() function, you can specify all logic-related functions you want to be called every frame while your application is executed. Engine calls this function before updating each render frame.在 update() 函数中,您可以指定在执行应用程序时每帧要调用的所有逻辑相关函数。引擎在更新每个渲染帧之前调用此函数。
postUpdate() functionpostUpdate() function#
This is an additional function used to correct behavior after the state of the node has been updated. Engine calls this function before rendering each render frame.这是一个附加功能,用于在节点状态更新后纠正行为。引擎在渲染每个渲染帧之前调用此函数。
updatePhysics() functionupdatePhysics() function#
This function is used to control physics in your application. Engine calls this function before updating each physics frame.此函数用于控制应用程序中的物理。引擎在更新每个物理帧之前调用此函数。
swap() functionswap() function#
This function is designed to operate with the results of the updateAsyncThread() function — all other methods (threads) have already been performed and are idle. After this function, only two actions occur:此函数旨在使用 updateAsyncThread() 函数的结果进行操作——所有其他方法(线程)都已执行且处于空闲状态。在这个函数之后,只发生两个动作:
- All objects that are queued for deletion are deleted.删除排队等待删除的所有对象。
- Profiler is updated.探查器已更新。
shutdown() functionshutdown() function#
The function is called when the world is unloaded, and is used to delete resources that were created during world script execution to avoid memory leaks. Engine calls this function on the world shutdown. Inside this function, you should delete all pointers to created objects.该函数在世界卸载时调用,用于删除世界脚本执行期间创建的资源,以避免内存泄漏。引擎在世界关闭时调用此函数。在这个函数中,你应该删除所有指向创建对象的指针。
save() and restore() functionssave() and restore() functions#
The save() function saves custom user data to a file. Engine calls this function when the world is saving its state. To restore data from a file, you need to use the restore() function. Engine calls this function when the world is restoring its state.save() 函数将自定义用户数据保存到文件中。当世界保存它的 state 时,引擎会调用这个函数。要从文件恢复数据,您需要使用 restore() 函数。当世界正在恢复它的 state 时,引擎会调用这个函数。
System Logic系统逻辑(System Logic)#
The System Logic stays loaded during the whole UNIGINE Engine runtime. It may use the following methods, which are available for both UnigineScript and C++/C#: 系统逻辑在整个 UNIGINE Engine 运行时保持加载状态。它可以使用以下方法,这些方法可用于 UnigineScript 和 C++/C#:
#include "AppSystemLogic.h"
AppSystemLogic::AppSystemLogic() {
}
AppSystemLogic::~AppSystemLogic() {
}
int AppSystemLogic::init() {
return 1;
}
int AppSystemLogic::update() {
return 1;
}
int AppSystemLogic::postUpdate() {
return 1;
}
int AppSystemLogic::shutdown() {
return 1;
}
int AppSystemLogic::destroyRenderResources() {
return 1;
}
init() functioninit() function#
Engine calls this function on initialization. Here you should put the code that you want to be executed during the system script initialization.引擎在初始化时调用此函数。在这里你应该把你想在系统脚本初始化期间执行的代码放在这里。
update() functionupdate() function#
Engine calls this function before updating each render frame. Here you should put the code that you want to be updated each frame. The logic should be related to the engine job. These are the examples of operations that are performed by a default system script:引擎在更新每个渲染帧之前调用此函数。在这里,您应该放置要每帧更新的代码。逻辑应该与引擎作业相关。这些是由默认系统脚本执行的操作示例:
- The system script handles the mouse. It controls whether the mouse is grabbed when clicked (by default), the mouse cursor disappears when not moved for some time, or not handled by the system.系统脚本处理鼠标。它控制点击时鼠标是否被抓取(默认情况下),鼠标光标在一段时间不移动时消失,或者不被系统处理。
- Main menu logic is updated.主菜单逻辑已更新。
- Other system-related user input is handled.处理其他与系统相关的用户输入。
postUpdate() functionpostUpdate() function#
Engine calls this function before rendering each render frame. It can access the updated data on node states and correct the behavior accordingly in the same frame.引擎在渲染每个渲染帧之前调用此函数。它可以访问节点状态的更新数据,并在同一帧中相应地纠正行为。
shutdown() functionshutdown() function#
Engine calls this function on application shutdown. You should release all the resources that you created for system script.引擎在应用程序关闭时调用此函数。您应该释放为系统脚本创建的所有资源。
destroyRenderResources() functiondestroyRenderResources() function#
Engine calls this function when the video mode is changed or application is restarted (i.e. video_restart is called). It is used to reinitialize the graphics context if application directly uses any OpenGL or DirectX commands).当视频模式改变或应用程序重新启动(即调用 video_restart)时,引擎调用此函数。如果应用程序直接使用任何 OpenGL 或 DirectX 命令,则它用于重新初始化图形上下文。
Editor Logic编辑逻辑(Editor Logic)#
This component is to be used in case you need to implement your own Editor. It has more implemented methods providing you with clear understanding of the current Engine events (a node has been created, a property has been deleted, a material has been changed, etc.).如果您需要实现自己的编辑器,将使用此组件。 它有更多的实现方法,让您清楚地了解当前的引擎事件(节点已创建、属性已删除、材质已更改等)。
The Editor Logic may use the following methods, which are available for both UnigineScript and C++/C#: 编辑器逻辑可以使用以下方法,这些方法可用于 UnigineScript 和 C++/C#:
#include "AppEditorLogic.h"
AppEditorLogic::AppEditorLogic() {
}
AppEditorLogic::~AppEditorLogic() {
}
int AppEditorLogic::init() {
return 1;
}
int AppEditorLogic::update() {
return 1;
}
int AppEditorLogic::postUpdate() {
return 1;
}
int AppEditorLogic::shutdown() {
return 1;
}
int AppEditorLogic::destroyRenderResources() {
return 1;
}
int AppEditorLogic::worldInit() {
return 1;
}
int AppEditorLogic::worldShutdown() {
return 1;
}
int AppEditorLogic::worldSave() {
return 1;
}
init() functioninit() function#
The engine calls this function on the world initialization. It initializes all belongings of UnigineEditor.引擎在世界初始化时调用这个函数。它初始化 UnigineEditor 的所有资产。
update() functionupdate() function#
If the editor is loaded, the engine calls this function before updating each render frame. Here you can put some editor-related logic that should be updated each frame.如果加载了编辑器,引擎会在更新每个渲染帧之前调用此函数。在这里你可以放一些应该每帧更新的与编辑器相关的逻辑。
postUpdate() functionpostUpdate() function#
If the editor is loaded, the engine calls this function before rendering each render frame.如果加载了编辑器,引擎会在渲染每个渲染帧之前调用此函数。
shutdown() functionshutdown() function#
The engine calls this function on editor shutdown. Engine applications may continue running.引擎在编辑器关闭时调用此函数。引擎应用程序可以继续运行。
destroyRenderResources() functiondestroyRenderResources() function#
The engine calls this function when the video mode is changed or the application is restarted (i.e. video_restart is called). The function is used to reinitialize the graphics context (if the application directly uses any OpenGL or DirectX commands)当视频模式改变或应用程序重新启动(即调用 video_restart)时,引擎会调用此函数。该函数用于重新初始化图形上下文(如果应用程序直接使用任何 OpenGL 或 DirectX 命令)
worldInit() functionworldInit() function#
If the editor is loaded, the Engine calls this function on the world initialization. Use this function, if you want to initialize something before the World Logic starts its init() function. 如果编辑器被加载,引擎在世界初始化时调用这个函数。如果您想在 World Logic 启动其 init() 函数之前初始化某些内容,请使用此函数。
worldShutdown() functionworldShutdown() function#
If the editor is loaded, the engine calls this function on the world shutdown.如果编辑器已加载,引擎会在世界关闭时调用此函数。
worldSave() functionworldSave() function#
If the editor is loaded, the Engine calls this function on saving the world.如果加载了编辑器,引擎会在拯救世界时调用此函数。