UDKは.NETを使用します。では、どういうわけかUnrealScriptから.NETを使用することは可能でしょうか?
UnrealScriptでC#を使用するのは本当に素晴らしいことです。
確かに、 dllimportを使用する.NETとUnrealScriptの間で相互作用するC ++レイヤーを構築することはできますが、この質問の対象ではありません。
UDKは.NETを使用します。では、どういうわけかUnrealScriptから.NETを使用することは可能でしょうか?
UnrealScriptでC#を使用するのは本当に素晴らしいことです。
確かに、 dllimportを使用する.NETとUnrealScriptの間で相互作用するC ++レイヤーを構築することはできますが、この質問の対象ではありません。
したがって、UnrealScriptから直接.NETライブラリにアクセスする方法はないようですが、C#の[DllExport]拡張機能とUnrealScript相互運用システムを組み合わせて、中間のC++ラッパーなしで.NETと対話することができます。
C#でint、string、structureと交換し、UnrealScriptStringを埋める簡単な例を見てみましょう。
1 C#クラスを作成します
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
{
}
その後、関数を使用できます。
唯一のトリックは、FillStringメソッドに示されているようにUDK文字列を入力することです。文字列を固定長バッファとして渡すため、この文字列は初期化する必要があります。初期化された文字列の長さは、C#が機能する長さ以上である必要があります。
詳細については、こちらをご覧ください。