Creating C# Application
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.
Implementation by using the C# language is very similar to C++. Read the Creating C++ Application article to get basic principles.
- C# (.NET Core) API is cross-platform (recommended).
- C# (.NET Framework) API is supported on Windows only.
See Also#
- Examples located in the <UnigineSDK>/source/csharp/samples/Api and <UnigineSDK>/source/csharp/samples/App folders.
- The article on Setting Up Development Environment to learn more on how to prepare the development environment.
Creating Empty C# Application#
It is very easy to start your own C# project by using UNIGINE SDK Browser:
- Open the UNIGINE SDK Browser.
- Go to the Projects tab and click CREATE NEW.
- Specify the following parameters:
- Project name — specify the name of your project.
- Location — specify the path to your project folder.
- SDK — choose the Unigine SDK.
- API+IDE — choose either of the two options: C# (.NET Core) or C# (.NET Framework) depending on how you plan to implement your logic.
Choose C# (.NET Core) to start working with the C# API with the Component System available.
- Architecture — specify the architecture of your target platform.
- Precision — specify the precision. In this example we will use double precision.
Read more about these parameters in this article. - Click the Create New Project button. The project will appear in the projects list.
You can run your project by clicking the Run button.
Implementing C# Logic#
C# logic can be implemented using either of the following APIs:
- C# (.NET Core) API — a recommended approach for C# projects. It allows using C# Component System enabled by default and integrated into the Editor: this way it is easier to implement your application logic in components and assign them to any node to be executed. A component can be reused for as many nodes as you need without changing anything in it. If a node is renamed or disabled, the component assigned to it does not require any changes due to that. When you change anything in your component's logic, the changes are applied to all nodes having this component assigned. The Editor also provides for running an instance of the application to check the result immediately.
- C# (.NET Framework) API. Code is written using the AppWorldLogic and AppSystemLogic global classes — this approach has some usability limitations. In addition, the Component System is neither enabled, nor integrated with the Editor: working with components is possible only in manual mode via code, using 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# (.NET Core) Projects#
Let's make the material ball rotate using C# Component System.
- In UNIGINE SDK Browser, choose your C# project created with the C# (.NET Core) option selected as API+IDE, and click the Open Editor button.
- In UnigineEditor, create a new C# component via Asset Browser.
Let's name it rotator. - By double-clicking a created asset rotator.cs, it will open in the default IDE. Add the following code to this file.
All saved changes of the component source code make the component update with no compilation required when the Editor window gets focus.
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.
- 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) projects#
With the approach using only the AppWorldLogic and AppSystemLogic global classes, making the material ball rotate is done as follows.
- In the UNIGINE SDK Browser, choose your C# project and click the Open Code IDE button.
Visual Studio will open. - In Visual Studio, add the following code in your project's AppWorldLogic.cs file.
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; } } }
- Build your project by clicking Build -> Build Solution in Visual Studio.
- Start your project by clicking Debug -> Start in a proper mode in Visual Studio.
This approach has some usability limitations:
- If a node name has been changed, code wouldn't work.
- If you run a world that does not contain a node AppWorldLogic refers to, the application will crash.
For each world of the project, one and the same AppWorldLogic is called. It is impossible to link an individual AppWorldLogic to each world.
Using Components in C# (.NET Framework) projects#
Unlike .NET Core, where C# Component System is enabled by default and integrated into the Editor, working with components in .Net Framework requires running the system manually and creating components via properties, which connect code with nodes in UnigineEditor. For example:
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;
}
}
If you don't want to use UnigineEditor, you can add components to nodes directly via code without calling SaveProperties():
ComponentSystem.AddComponent<MyComponent>(node);