This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Rendering
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
Version Control
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
C++
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Materials and Shaders
Rebuilding the Engine Tools
GUI
VR Development
Double Precision Coordinates
API
Animations-Related Classes
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
VR-Related Classes
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Package Class

The Package class is a data provider for the internal FileSystem class. You can load all 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
{
	class MyPackage : Package
	{
		private FileStream file;

		public MyPackage()
		{
			Log.Message("MyPackage.ctor(): called\n");
		}

		~MyPackage()
		{
			Log.Message("MyPackage.dtor(): called\n");
		}

		// list of files
		public override int GetNumFiles()
		{
			Log.Message("MyPackage.GetNumFiles(): called\n");
			return 2;
		}

		public override string GetFileName(int num)
		{
			Log.Message("MyPackage.GetFileName({0}): called\n", num);
			if (num == 0)
				return "data/package.usc";
			if (num == 1)
				return "data/package.world";
			return "";
		}

		// select file
		public override bool SelectFile(string name, out int size)
		{
			Log.Message("MyPackage.SelectFile(\"{0}\",): called\n", name);

			try
			{
				file = null;
				if (name == "data/package.usc")
					file = File.OpenRead("my_data/package.usc");
				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 false;
			}

			size = (int)file.Length;

			return true;
		}

		// read file
		public override bool ReadFile(IntPtr data, int size)
		{
			Log.Message("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 false;
			}

			return true;
		}
	}

	/*
	 */
	[STAThread]
	static void Main(string[] args)
	{
		// add package
		MyPackage package = new MyPackage();
		FileSystem.PreloadExternPackage(package);

		// initialize engine
		Engine.Init(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 before initializing the engine using the PreloadExternPackage() method.

Output#

The following result will be shown in the console:

Last update: 2023-12-19
Build: ()