Таким образом, похоже, нет никакого способа получить доступ к библиотекам .NET напрямую из UnrealScript, но можно комбинировать Расширение [DllExport] для C# и система взаимодействия UnrealScript для взаимодействия с .NET без промежуточной оболочки C++.
Давайте посмотрим на простой пример обмена с int, string, структурой и заполнением UnrealScript String в C#.
1 Создать класс С#
using System;
using System.Runtime.InteropServices;
using RGiesecke.DllExport;
namespace UDKManagedTestDLL
{
struct TestStruct
{
public int Value;
}
public static class UnmanagedExports
{
// Get string from C#
// returned strings are copied by UnrealScript interop system so one
// shouldn't worry about allocation\deallocation problem
[DllExport("GetString", CallingConvention = CallingConvention.StdCall]
[return: MarshalAs(UnmanagedType.LPWStr)]
static string GetString()
{
return "Hello UnrealScript from C#!";
}
//This function takes int, squares it and return a structure
[DllExport("GetStructure", CallingConvention = CallingConvention.StdCall]
static TestStructure GetStructure(int x)
{
return new TestStructure{Value=x*x};
}
//This function fills UnrealScript string
//(!) warning (!) the string should be initialized (memory allocated) in UnrealScript
// see example of usage below
[DllExport("FillString", CallingConvention = CallingConvention.StdCall]
static void FillString([MarshalAs(UnmanagedType.LPWStr)] StringBuilder str)
{
str.Clear(); //set position to the beginning of the string
str.Append("ha ha ha");
}
}
}
2 Скомпилируйте код C#, скажем, как UDKManagedTest.dll и поместите в [\Binaries\Win32\UserCode] (или Win64)
3 На стороне UnrealScript следует разместить объявления функций:
class TestManagedDLL extends Object
DLLBind(UDKManagedTest);
struct TestStruct
{
int Value;
}
dllimport final function string GetString();
dllimport final function TestStruct GetStructure();
dllimport final function FillString(out string str);
DefaultProperties
{
}
Затем можно использовать функции.
Единственный трюк — это заполнение строки UDK, как это показано в методе FillString. Поскольку мы передаем строку как буфер фиксированной длины, эта строка ДОЛЖНА быть инициализирована. Длина инициализированной строки ДОЛЖНА быть больше или равна длине, с которой будет работать C#.
Дальнейшее чтение можно найти здесь.
person
MajesticRa
schedule
06.02.2012