This page has been translated automatically.
UNIGINE 基础课程
1. 简介
2. 虚拟世界管理
3. 3D模型准备
4. 材质
5. 摄像机和光照系统
7. 制作过场动画与动画序列
8. 准备发布项目
9. 物理系统
10. 优化基础
11. 项目2:第一人称射击游戏
12. PROJECT3: Third-Person Cross-Country Arcade Racing Game
13. PROJECT4: VR Application With Simple Interaction

基础概念

So, as we have already said, logic (or program code) is what breathes life into the content and makes it perform the task at hand.正如前文所述,程序逻辑(或代码)是为内容注入生命力并使其执行目标任务的核心要素。

To implement the project logic in UNIGINE, you can use C# or C++ (if you need the best performance and effective integration with the existing source code). In most cases C# is the optimal option, as it is more widespread and provides the best ratio of speed and ease of use. If you want, you can combine programming languages (for example, you can use C++ for performance consuming operations as a plugin and then access it from C# code).在UNIGINE中实现项目逻辑时,你可以选择使用C#或C++(当需要最佳性能或与现有代码库深度集成时)。多数情况下C#是最优选择,因其应用更广泛且在开发效率与运行速度之间取得了最佳平衡。你也可以混合使用编程语言(例如用C++编写高性能运算插件,再通过C#代码调用)。

Every UNIGINE-based application has its life cycle, that consists of certain stages, some of them are performed once, others are repeated each frame. In short, these stages are as follows:每个基于UNIGINE的应用程序都有其生命周期,包含若干特定阶段:有些仅执行一次,有些则逐帧循环执行。简而言之,这些阶段包括:

UNIGINE has three main logic components, each of them has a set of functions (named init(), update(), render(), etc.) that contain actions to be performed at corresponding stages of the Engine's working cycle. These components are:UNIGINE包含三大核心逻辑组件,每个组件都具备一系列函数(如init()、update()、render()等),这些函数包含引擎工作周期各阶段需执行的操作:

  • System Logic is the code that is run during the whole application life cycle (its scope exists even when switching between worlds).系统逻辑System Logic)贯穿整个应用程序生命周期的代码(即使切换世界场景时依然有效)。

    • For applications that use C++ AppSystemLogic.cpp is created, and for C# applications — AppSystemLogic.cs. This file is stored in the source/ folder of your project. It has implemented methods to put your logic code inside.对于使用C++的项目会创建AppSystemLogic.cpp文件,而C#项目则生成AppSystemLogic.cs文件。该文件存储在项目的source/文件夹中,其中已实现的方法可供你填入逻辑代码。
  • World Logic is the logic of the virtual world. The logic takes effect only when the world is loaded.世界逻辑World Logic)虚拟世界的专属逻辑,仅在加载对应世界时生效。

    • For applications that use C++ AppWorldLogic.cpp is created, and for C# applications — AppWorldLogic.cs. This file is stored in the source/ folder of your project and stays loaded during the whole engine runtime. It has implemented methods to put your logic code inside.对于采用C++的项目会生成AppWorldLogic.cpp文件,而使用C#的项目则创建AppWorldLogic.cs文件。该文件存放于项目的source/目录下,在引擎整个运行期间持续保持加载状态,其内置的方法可供你插入逻辑代码实现。
  • 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.).编辑器逻辑Editor Logic)需自定义编辑器时使用的组件。该组件提供更多已实现的方法,可精准感知引擎事件(如节点创建、属性删除、材质变更等)。

The files described above are useful for writing code referring to the application itself (for example, if you need to perform some actions before starting the main loop or when switching between worlds) or to a world (for example, selecting the loading screen depending on which world is being loaded), but the most common approach is to bind logic to objects. For this purpose, UNIGINE has the Component System, which allows you to implement the application logic using a set of building blocks — components, and assign these components to nodes, thus extending their basic functionality (for example, an ordinary static mesh can be made interactive — reacting to a mouse click in a certain way). By combining these small and simple blocks, you can create a very complex logical system.上述文件适用于编写与应用程序本身相关的代码(例如在主循环开始前执行操作,或切换世界时的处理),或编写世界相关逻辑(例如根据加载的世界选择加载画面)。但更常见的做法是将逻辑绑定到具体对象,为此UNIGINE提供组件系统(Component System):通过组合可复用的构建块(组件)来实现应用逻辑,并将这些组件分配给节点以扩展其基础功能(例如让静态网格具备交互性,响应鼠标点击等操作)。通过组合这些简单模块,可构建出极其复杂的逻辑系统。

A logic component integrates a node, a C++ class, containing logic implementation (actions to be performed), and a property, defining a set of additional parameters to be used. The list of parameters as well as their types are the same for both the component and the corresponding property. Component is assigned to a node via a property.Now, once everything is installed, you can create a project and start writing code!逻辑组件整合了节点、包含逻辑实现(需执行的操作)的C++类,以及定义额外参数集的属性(property)。组件与对应属性具有相同的参数列表及类型。组件通过属性分配给节点。

Logic
逻辑#

The component logic is implemented using a set of methods that are called by the corresponding World Logic functions:组件逻辑通过一系列方法实现,这些方法由对应的世界逻辑(World Logic)函数调用:

  • init() — all necessary resources are created and initialized.init():创建并初始化所有必要资源。
  • updateAsyncThread() — used for specifying all logic functions you want to be called every frame independently of the rendering thread.updateAsyncThread():用于指定需要每帧调用的逻辑函数(独立于渲染线程)。

    注意
    This function does not have protection locks, so it is not recommended to modify other components inside this function, unless you are absolutely sure, that these components won't be modified or removed elsewhere.此函数没有保护锁,除非你完全确定其他组件不会被修改或删除,否则不建议在此函数内修改其他组件。
  • updateSyncThread() — used for specifying any parallel logic functions to be executed before the update(). This method is used to perform resource-consuming calculations such as pathfinding, procedural texture generation, etc.updateSyncThread():用于指定在update()之前执行的并行逻辑函数,适用于执行资源密集型计算(如路径查找、程序化纹理生成等)。

    注意
    This method should be used to call only those API methods that apply to the current node: the node itself, its materials and properties.此方法应仅调用适用于当前节点的API方法(节点本身、其材质和属性)。
  • update() — used for specifying all logic-related functions you want to be called every frame.update():用于指定需要每帧调用的所有逻辑相关函数。
  • postUpdate() — used to adjust the behavior according to the updated states of the nodes in the same frame.postUpdate():根据同一帧中节点的更新状态调整行为。
  • updatePhysics() — physics simulation is performed: continuous operations (moving the car forward depending on the engine speed, simulating constant wind, performing immediate reactions to a collision, etc.).updatePhysics():执行物理模拟(如根据引擎速度持续移动车辆、模拟恒定风力、对碰撞做出即时反应等)。
  • swap()— operating with the results of the updateAsyncThread() method — all other methods (threads) have already been performed and are idle. After this function, only two actions occur:swap():处理updateSyncThread()方法的结果(此时其他方法/线程已完成执行并处于空闲状态)。此函数执行后仅发生两个操作:

    • All objects that are queued for deletion are deleted.删除队列中待删除的所有对象
    • Profiler is updated.更新性能分析器
  • shutdown() — cleanup is performed when the component is shutdown.shutdown():组件关闭时执行清理操作。

The most frequently used functions are init() and update().其中最常用的函数是init()update()

注意
You can set multiple methods for each stage (e.g. multiple update() methods or just a single init()).你可以为每个阶段设置多个方法(例如多个update()方法或仅单个init()方法)。

The logic of a certain component is applied only when the property and the corresponding node are enabled. Thus, you can enable/disable the logic of each specific component at runtime, if necessary.特定组件的逻辑仅在对应属性 (property)和节点启用时才会生效。因此,你可以在运行时根据需要启用/禁用每个具体组件的逻辑功能。

You can assign as many components to a node as you need. The sequence in which their logic is executed is determined by the order value specified for the corresponding methods. In the example below, the Component1::init() method will be executed first, followed by the Component2::init() method:你可以为节点分配任意数量的组件。这些组件逻辑的执行顺序由对应方法中指定的order值决定。如下例所示,Component1::init()方法将优先执行,随后执行Component2::init()方法:

源代码 (C++)
// Component1
COMPONENT_INIT(init, 2);

// ...

// Component2
COMPONENT_INIT(init, 3);

// ...

If the order values are the same or not specified, the execution sequence of methods with the same name will be determined by the order of the properties in the Properties window.order值相同或未指定时,同名方法的执行顺序将由组件在属性窗口(Properties)中的排列顺序决定。

Components can interact with other components and nodes.组件之间可实现交互,也能与其他节点进行联动。

As an example, you can use components to implement the logic of chasing the player by enemies in your game: regardless of their size, shape, or speed, they will all check the player's position and try to find the path to get to him the as soon as possible. The code will be mostly the same, only the parameters (speed, meshes and probably sounds) may differ, so you can add all these parameters to the component (to easily change them at any time) and write code in the methods of the corresponding class (e.g. add enemies to the world in the init() method, and implement chasing the player in the update() method).举例说明,你可以通过组件实现游戏中敌人追击玩家的逻辑:无论敌人的体型、外观或移动速度如何差异,它们都会检测玩家位置并尝试寻找最短追击路径。虽然核心代码逻辑基本相同,但参数配置(如移动速度、模型网格和音效等)可能各不相同。因此,你可以将这些参数全部集成到组件中(便于随时调整),并在对应类的方法中编写具体实现(例如:在init()方法中将敌人实例添加至场景,在update()方法中实现追击玩家的行为逻辑)。

Then you just need to assign a component to all enemy objects and customize the parameters (define meshes, sounds, etc.). The component system will do the rest: execute your code at the corresponding stages of the Engine's main loop for all enemy objects, using their specific parameters. If you later want to change the code (such as to improve the chase algorithm), you'll need to do that in one place only — the component class.之后只需为所有敌人对象分配该组件并配置参数(定义模型、音效等)。组件系统将自动完成后续工作:在引擎主循环的对应阶段,使用各敌人的特定参数执行你的代码。如需修改代码(如优化追击算法),仅需在组件类这一处进行变更。

So, let's sum up – using components provides more flexibility in logic implementation, allowing you to:综上所述,使用组件系统能为逻辑实现提供更高灵活性,使你能够:

  • Define which parts of code (implemented in component methods) should be executed and which should not.精确控制哪些代码段(组件方法实现)需要执行
  • Control the execution order of these code parts.调控代码段的执行顺序
  • Reuse parts of code as many times as you need for any number of objects without any changes. If you need to change your code, you'll have to modify only one source (similar to working with NodeReference, if we are talking about content).无需修改即可将代码复用至任意数量的对象。代码变更只需修改单一源文件(类似于使用节点引用(NodeReference)管理内容资源)
  • Combine specific pieces of code that will be executed for specific objects. You can build a very complex system out of many small and simple blocks (similar to using NodeReference to create a large and complex structure out of many simple elements).组合特定代码段使其针对特定对象执行。通过简单模块可构建复杂系统(如同用节点引用将简单元素组合成复杂结构)

Installing the Required Software
安装必备软件#

To work with C++ components we recommend using MS Visual Studio environment, you can download it from the official site (https://visualstudio.microsoft.com/) and install.为开发C++组件,我们推荐使用MS Visual Studio集成开发环境,你可从官网(https://visualstudio.microsoft.com/)下载并安装该软件。

A logic component integrates a node, a C++ class, containing logic implementation (actions to be performed), and a property, defining a set of additional parameters to be used. The list of parameters as well as their types are the same for both the component and the corresponding property. Component is assigned to a node via a property.Now, once everything is installed, you can create a project and start writing code!完成上述安装后,你即可创建项目并开始编写代码!

本页面上的信息适用于 UNIGINE 2.20 SDK.

最新更新: 2025-06-09
Build: ()