※ 引述《riveranb (River)》之銘言:
: 標題: [程式] Unity scripting (native plugin)
:
: 以下是我的 sample codes
:
: ==== C++ native plugin部分 ====
: extern "c" declspec(dllexport) void cppfunc( char * tostring, int maxlen)
: {
: std::string source = .... // get texts from opened file
: if(source.length() < maxlen)
: {
: strcpy(tostring, source.c_str());
: }
: }
:
: ==== Unity C# script部分 ====
: [DllImport ("CppPlugin")]
: static extern void cppfunc(StringBuilder tostring, int maxlen);
:
: ......
:
:
: {
: {
: StringBuilder thestring = new StringBuilder(_maxlen); // maxlen = 64
: StringBuilder thestring = new StringBuilder(_maxlen); // maxlen = 64
: while( /** if more in file **/ )
: while( /** if more in file **/ )
: {
: #if METHOD1
: thestring = new StringBuilder(_maxlen); // method 1, always correct
: #else if METHOD2
: thestring.Length = 0; // method 2, get wrong strings after several calls
: #endif
: CppInterop.cppfunc(thestring, _maxlen);
: }
: }
:
: