使用.NET和UnrealScript

Maj*_*cRa 0 .net c# unrealscript

UDK使用.NET.那么也许有可能以某种方式使用UnrealScript中的.NET?
使用C#和UnrealScript非常棒.

当然可以构建C++层来在.NET和UnrealScript之间进行交互,这将使用dllimport,但它不是这个问题的主题.

Maj*_*cRa 5

所以似乎没有办法直接从UnrealScript访问.NET库,但是可以将C#和UnrealScript互操作系统的[DllExport]扩展组合在一起,以便在没有中间C++包装器的情况下与.NET进行交互.

让我们看一下用int,string,structure交换和在C#中填充UnrealScript String的简单示例.

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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
{
}
Run Code Online (Sandbox Code Playgroud)

然后可以使用这些功能.


唯一的技巧是填充UDK字符串,就像它在FillString方法中所示.由于我们将字符串作为固定长度缓冲区传递,因此必须初始化此字符串.初始化字符串的长度必须大于或等于C#的长度.


可以在这里找到进一步的阅读.