This page has been translated automatically.
Видеоуроки
Interface
Essentials
Advanced
Подсказки и советы
Основы
Программирование на C#
Рендеринг
Принципы работы
Свойства (properties)
Компонентная Система
Рендер
Физика
Редактор UnigineEditor
Обзор интерфейса
Работа с ассетами
Настройки и предпочтения
Работа с проектами
Настройка параметров ноды
Setting Up Materials
Настройка свойств
Освещение
Landscape Tool
Sandworm
Использование инструментов редактора для конкретных задач
Extending Editor Functionality
Встроенные объекты
Ноды (Nodes)
Объекты (Objects)
Эффекты
Декали
Источники света
Geodetics
World Nodes
Звуковые объекты
Объекты поиска пути
Players
Программирование
Основы
Настройка среды разработки
Примеры использования
C++
UnigineScript
Унифицированный язык шейдеров UUSL
Плагины
File Formats
Rebuilding the Engine Tools
GUI
Двойная точность координат
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Работа с контентом
Оптимизация контента
Материалы
Art Samples
Tutorials

Package Class

The Package class is a data provider for the internal FileSystem class. You can load all the resources via this class. This article describes the sample located in the <UnigineSDK>/source/csharp/samples/Api/Systems/Package/ directory.

Notice
In the C# API you can inherit from the Package class only once.

See also#

  • An example can be found in the <UnigineSDK>/source/csharp/samples/Api/Systems/Package/ directory.
  • The C++ API Unigine::Package class and the Unigine::FileSystem class which have the same methods and behavior as in the C# API.

Package Class Usage Example#

C# Side#

To use the Unigine.Package class, you should create your own class and inherit it from the Unigine.Package class and override virtual methods.

Source code (C#)
using System;
using System.IO;
using File = System.IO.File;
using System.Runtime.InteropServices;
using Unigine;

/*
 */
class UnigineApp {
	
	/*
	 * Create a class and inherit if from Unigine.Package
	 */
	class MyPackage : Package {
		
		private FileStream file;
		
		// list of files
		public override int getNumFiles() {
			Log.Warning("MyPackage.getNumFiles(): called\n");
			return 2;
		}
		
		public override string getFileName(int num) {
			Log.Warning("MyPackage.getFileName({0}): called\n",num);
			if(num == 0) return "data/package.cpp";
			if(num == 1) return "data/package.world";
			return "";
		}
		
		// select file
		public override int selectFile(string name,out int size) {
			
			Log.Warning("MyPackage.selectFile(\"{0}\",): called\n",name);
			
			try {
				file = null;
				if(name == "data/package.cpp") file = File.OpenRead("my_data/package.cpp");
				if(name == "data/package.world") file = File.OpenRead("my_data/package.world");
			}
			catch(Exception e) {
				Log.Error("MyPackage.selectFile(): {0}\n",e.Message);
				size = 0;
				return 0;
			}
			
			size = (int)file.Length;
			
			return 1;
		}
		
		// read file
		public override int readFile(IntPtr data,int size) {
			
			Log.Warning("MyPackage.readFile(,{0}): called\n",size);
			
			try {
				byte[] src = new byte[size];
				int ret = file.Read(src,0,size);
				Marshal.Copy(src,0,data,ret);
			}
			catch(Exception e) {
				Log.Error("MyPackage.readFile(): {0}\n",e.Message);
				return 0;
			}
			
			return 1;
		}
	}
	
	/*
	 */
	[STAThread]
	static void Main(string[] args) {
		
		// create an instance of the MyPackage class and add it to the FileSystem
		MyPackage package = new MyPackage();
		FileSystem.preloadExternPackage(package);
		
		// initialize engine
		Engine engine = Engine.Init(Engine.VERSION,args);
		
		// enter main loop
		engine.Main();
		
		// shutdown engine
		Engine.Shutdown();
	}
}

The MyPackage class has overridden readFile(), selectFile(), getFileName() and getNumFiles() functions. The class operates with two files located in the my_data folder. In the Main() function we create an instance of the MyPackage class and add it to the FileSystem.

Unigine Script Side#

In the UnigineScript package.cpp file:

Source code (UnigineScript)
int init() {
	
	log.message("\n");
	
	log.message("Hello from My C# Package\n");
	engine.dialogMessage("Package");
	
	return 1;
}

Output#

The following result will be shown:

In the console:

Output
MyPackage.selectFile("data/package.cpp",): called
MyPackage.readFile(,866): called
MyPackage.selectFile("data/package.cpp",): called
MyPackage.readFile(,866): called
Loading "data/package.cpp" 4ms
MyPackage.selectFile("data/package.world",): called
MyPackage.readFile(,62): called
Loading "data/package.world" 0ms

Hello from My C# Package
Last update: 16.08.2019
Build: ()