This page has been translated automatically.
Getting Started
Migrating to UNIGINE 2.0
C++ API Migration
UnigineScript
The Language
Core Library
Engine Library
Node-Related Classes
GUI-Related Classes
Plugins Library
High-Level Systems
Samples
Usage Examples
C++ API
API Reference
Integration Samples
Usage Examples
C++ Plugins
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

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 in Visual Studio on Windows platform.

Implementation by using the C# language is very similar to C++. Read the Creating C++ Application article to get basic principles.

Code written in C# is the same for all supported platforms: Windows and Linux, OS X does not support compilation of the Unigine applications written by using the C# language, because Mono developers provide only x86 binaries for OS X. C# is available under OS X with custom Mono builds on the client side.

Notice
To work with C# API by using Mono on the Linux platform, the Mono should be installed this way.

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:

  1. Open the UNIGINE SDK Browser.
  2. Go to the Projects tab and click CREATE NEW.

  3. Specify the following parameters:

    • Project - specify the name of your project.
    • Project Path - specify the path to your project folder.
    • SDK - choose the Unigine SDK.
    • API - here choose C# (Visual Studio) to start working with the C# API.
    • Architecture - specify the architecture of your target platform.
    • Precision - specify the precision. In this example we will use double precision.
    Notice
    Read more about these parameters in this article
  4. Click the Create New Project button. The project will appear in the projects list.

You can run your project by clicking the Run button.

Notice
By default, in the world script file a WorldLight and a PlayerSpectator are created. You can leave functions of the world script empty, and create your own lights and players by using C#.

Implementing C# Logic

In this section we will add logic to the empty C# application project.

The following example shows how to add a primitive box mesh to the world and rotate it.

  1. In the Unigine SDK Browser, choose your C# project and click the Edit Content button.

  2. In the Unigine Editor that will be opened, add the primitive box as described in this section. Also name the primitive as in the example.

  3. Save the World, by clicking File -> Save World on the Menu bar or press CTRL + S and close Unigine Editor.
  4. In the Unigine SDK Browser, choose your C# project and click the Edit Code button.

    Visual Studio will be opened.
  5. In Visual Studio, write the following code in your project's .cs file.
    Source code (C#)
    using System;
    using System.Collections.Generic;
    using Unigine;
    
    #if UNIGINE_DOUBLE
    using Vec3 = Unigine.dvec3;
    using Mat4 = Unigine.dmat4;
    #else
    using Vec3 = Unigine.vec3;
    using Mat4 = Unigine.mat4;
    #endif
    
    class UnigineApp
    {
    	[STAThread]
    	static void Main(string[] args)
    	{
    		// initialize the wrapper
    		Wrapper.init();
    
    		// initialize the engine
    		Engine engine = Engine.init(Engine.VERSION, args);
    
    		// load the world by using console
    		Unigine.Console console = Unigine.Console.get();
    		console.run("world_load cs_project/cs_project");
    		console.flush();
    
    		// create the instance of custom class
    		My_class app = new My_class();
    
    		// application initialization
    		app.my_init();
    
    		// enter the main loop 
    		while (!engine.isDone())
    		{
    			// update in the main loop
    			engine.update();
    			// application update 
    			app.my_update();
    
    			// render in the main loop
    			engine.render();
    			// application render
    			app.my_render();
    
    			// swap in the main loop
    			engine.swap();
    			// application physics swap
    			app.my_swap();
    		}
    
    		// application shutdown
    		app.my_shutdown();
    
    		Engine.shutdown();
    	}
    }
    
    // class with custom function
    class My_class
    {
    	Node node;
    
    	public void my_update()
    	{
    		// custom update
    		Game game = Game.get();
    
    		// set the transformation to the node
    		float ifps = game.getIFps();
    		double angle = ifps * 90.0f;
    		Mat4 transform = node.getTransform() * MathLib.rotateZ(angle);
    		node.setTransform(transform);
    	}
    
    	public void my_render()
    	{
    		// custom render
    	}
    
    	public void my_swap()
    	{
    		// custom swap
    	}
    
    	public void my_init()
    	{
    		// find the node by its name
    		Editor e = Editor.get();
    		node = e.getNodeByName("box");
    	
    
    	public void my_shutdown()
    	{
    		// clear nodes
    		node.clearPtr();
    	}
    }
    					
    Notice
    In the code above we load the cs_project/cs_project.cpp world script file. You should specify the name of your project's world script file.
  6. Build your project by clicking Build -> Build Solution in Visual Studio or press F7.

  7. Start your project by clicking Debug -> Start in a proper mode in Visual Studio.

Notice
To run your project from SDK Browser, specify the path to the .exe file of your C# project in SDK Browser by customizing Run Project
Last update: 2017-07-03
Build: ()