创建C#应用程序
A Unigine-based application can be implemented by means of C# only, without using UnigineScript. This article describes how to create a new Unigine-based C# application on Windows platform.基于Unigine的应用程序只能通过C#来实现,而无需使用UnigineScript。本文介绍如何在Windows平台上创建一个新的基于Unigine的C#应用程序。
Implementation by using the C# language is very similar to C++. Read the Creating C++ Application article to get basic principles.使用C#语言的实现与C ++非常相似。阅读创建C ++应用程序文章以获取基本原理。
See Also也可以看看#
- Examples located in the <UnigineSDK>/source/csharp/samples/Api and <UnigineSDK>/source/csharp/samples/App folders.示例位于<UnigineSDK>/source/csharp/samples/Api和<UnigineSDK>/source/csharp/samples/App文件夹中。
- The article on Setting Up Development Environment to learn more on how to prepare the development environment.设置开发环境上的文章,以了解有关如何准备开发环境的更多信息。
Creating Empty C# Application创建空的C#应用程序#
It is very easy to start your own C# project by using UNIGINE SDK Browser:使用UNIGINE SDK浏览器来启动自己的C#项目非常容易:
- Open the UNIGINE SDK Browser.打开UNIGINE SDK浏览器。
- Go to the Projects tab and click CREATE NEW.
转到Projects选项卡,然后单击CREATE NEW。
- Specify the following parameters:
- Project name — specify the name of your project.Project name — specify the name of your project.
- Location — specify the path to your project folder.Location — specify the path to your project folder.
- SDK — choose the Unigine SDK.SDK — choose the Unigine SDK.
- API+IDE — choose C# (.NET 5).
- Precision — specify the precision. In this example we will use double precision.Precision — specify the precision. In this example we will use double precision.
注意Project name — specify the name of your project.Location — specify the path to your project folder.SDK — choose the Unigine SDK.Precision — specify the precision. In this example we will use double precision.Read more about these parameters in this article.指定以下参数:Read more about these parameters in this article.Read more about these parameters in this article.
- Project name — specify the name of your project.Project name-指定项目的名称。
- Location — specify the path to your project folder.Location-指定项目文件夹的路径。
- SDK — choose the Unigine SDK.SDK —选择Unigine SDK。
- Precision — specify the precision. In this example we will use double precision.Precision-指定精度。在此示例中,我们将使用双精度。
注意Read more about these parameters in this article.在本文中阅读有关这些参数的更多信息。 - Click the Create New Project button. The project will appear in the projects list.
单击Create New Project按钮。该项目将出现在项目列表中。
You can run your project by clicking the Run button.您可以通过单击Run按钮来运行项目。
Implementing C# Logic实施C#逻辑#
可以使用以下任一API来实现C#逻辑:
- C# (.NET Core) API — C#项目的推荐方法。它允许使用默认启用并集成到编辑器中的 C#组件系统:这样可以更轻松地在组件中实现应用程序逻辑并将它们分配给要执行的任何节点。组件可以根据需要在任意数量的节点上重复使用,而无需更改组件中的任何内容。如果节点被重命名或禁用,则为其分配的组件不需要任何更改。当您更改组件逻辑中的任何内容时,更改将应用于分配了该组件的所有节点。编辑器还提供了运行应用程序实例以立即检查结果的功能。
- C# (.NET Framework) API。代码是使用AppWorldLogic和AppSystemLogic全局类编写的-这种方法具有一些可用性限制。此外,既未启用组件系统,也未将其与编辑器集成:只能使用 properties 通过代码以手动模式使用组件。
In this section we will add logic to the empty C# application project and rotate the material ball that is created by default.在本节中,我们将向空的C#应用程序项目中添加逻辑,并旋转默认情况下创建的物料球。
C#(.NET Core)项目#
让我们使用C#组件系统使物料球旋转。
- 在UNIGINE SDK浏览器中,选择使用C# (.NET Core)选项作为API + IDE选择创建的C#项目,然后单击打开编辑器按钮。
- In UnigineEditor, create a new C# component via Asset Browser.
Let's name it rotator. 在UnigineEditor中,通过Asset Browser创建一个新的C#组件。
我们将其命名为rotator。 - By double-clicking a created asset rotator.cs, it will open in the default IDE. Add the following code to this file.
源代码 (C#)All saved changes of the component source code make the component update with no compilation required when the Editor window gets focus. 双击创建的资产rotator.cs,它将在默认IDE中打开。将以下代码添加到该文件。
public class rotator : Component { public float angle = 30.0f; void Update() { // write here code to be called before updating each render frame node.Rotate(0, 0, angle * Game.IFps); } }
源代码 (C#)编辑器窗口获得焦点时,所有已保存的组件源代码更改都将使组件更新,而无需编译。public class rotator : Component { public float angle = 30.0f; void Update() { // write here code to be called before updating each render frame node.Rotate(0, 0, angle * Game.IFps); } }
- Add this component to the material ball.将此组件添加到material ball。
- Run an instance of the application by clicking the Play button on the toolbar.
通过单击工具栏上的“播放”按钮来运行该应用程序的实例。
The component can be assigned to any node or nodes without changing anything in it.可以将组件分配给任何一个或多个节点,而无需更改其中的任何内容。
C#(.NET Framework)项目#
使用仅使用AppWorldLogic和AppSystemLogic全局类的方法,使material ball旋转如下。
- 在UNIGINE SDK浏览器中,选择您的C#项目,然后单击Open Code IDE按钮。
Visual Studio将打开。 - 在Visual Studio,将以下代码添加到项目的AppWorldLogic.cs文件中。
源代码 (C#)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Unigine; namespace UnigineApp { class AppWorldLogic : WorldLogic { // define pointer to node Node node; public override bool Init() { // find the material ball node by its name node = World.GetNodeByName("material_ball"); return true; } public override bool Update() { // set the transformation to the node float angle = 30.0f; node.Rotate(0, 0, angle * Game.IFps); return true; } } }
- 通过在Visual Studio中单击Build -> Build Solution来构建项目。
- 通过在Visual Studio中单击Debug -> Start以适当的模式启动项目。
- 如果节点名称已更改,则代码将无法工作。
- 如果您运行的世界不包含AppWorldLogic引用的节点,则应用程序将崩溃。
注意对于项目的每个世界,都调用一个相同的AppWorldLogic。不可能将单个AppWorldLogic链接到每个世界。
在C#(.NET Framework)项目中使用组件#
不像.NET Core,其中默认情况下启用了C#组件系统并将其集成到编辑器中,并与.Net Framework需要手动运行系统并通过属性创建组件,这些组件将代码与UnigineEditor中的节点连接。例如:
class AppSystemLogic : SystemLogic
{
public override bool Init()
{
// initialize C# Component System
ComponentSystem.Enabled = true;
// generate all property files inside /data folder for all components
ComponentSystem.SaveProperties();
return true;
}
}
如果您不想使用UnigineEditor,则可以直接通过代码将组件添加到节点,而无需调用SaveProperties():
ComponentSystem.AddComponent<MyComponent>(node);