[程式] Unity scripting (native plugin)
想請教 scripting 使用到 C++ native
plugin相關的問題
我想要從某個 native plugin中拿回處理過的字串內容
看過網路上的資訊知道應該用 StringBuilder
但因為我要重覆這個呼叫過程
所以我想試著使用共用的 StringBuilder 物件
而不是每次都重新 new StringBuilder()
但這樣的話在執行呼叫 native plugin API多次後(可能 20次以上)
我得到的 text內容就會有錯誤(比正確的要短少)
可是如果我每次呼叫 native plugin API都傳入全新的物件 ( 透過 new
StringBuilder() )
我所得到的字串內容就會全部都正確
因為我是第一次做 unmanaged / managed code之間溝通的 programming
所以這部份我不太熟
不知道是過程中哪部分記憶體有出錯
也很想要讓 StringBuilder 物件可保持單一共用就好
也很想要讓 StringBuilder 物件可保持單一共用就好
也很想要讓 StringBuilder 物件可保持單一共用就好
也很想要讓 StringBuilder 物件可保持單一共用就好
也很想要讓 StringBuilder 物件可保持單一共用就好
以下是我的 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);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.40.34.73
※ 文章網址: https://www.ptt.cc/bbs/GameDesign/M.1514209817.A.F64.html
※ 編輯: riveranb (114.40.34.73), 12/25/2017 21:59:58
※ 編輯: riveranb (114.40.34.73), 12/25/2017 22:00:18
推
12/25 23:51,
7年前
, 1F
12/25 23:51, 1F
推
12/25 23:51,
7年前
, 2F
12/25 23:51, 2F
→
12/25 23:51,
7年前
, 3F
12/25 23:51, 3F
→
12/25 23:51,
7年前
, 4F
12/25 23:51, 4F
→
12/25 23:51,
7年前
, 5F
12/25 23:51, 5F
→
12/25 23:51,
7年前
, 6F
12/25 23:51, 6F
→
12/25 23:51,
7年前
, 7F
12/25 23:51, 7F
→
12/25 23:51,
7年前
, 8F
12/25 23:51, 8F
→
12/25 23:51,
7年前
, 9F
12/25 23:51, 9F
→
12/25 23:51,
7年前
, 10F
12/25 23:51, 10F
~
※ 編輯: riveranb (42.77.58.61), 12/26/2017 07:14:29
※ 編輯: riveranb (42.77.58.61), 12/26/2017 07:15:02
→
12/26 07:16,
7年前
, 11F
12/26 07:16, 11F
→
12/26 07:17,
7年前
, 12F
12/26 07:17, 12F
→
12/26 07:18,
7年前
, 13F
12/26 07:18, 13F
→
12/26 07:18,
7年前
, 14F
12/26 07:18, 14F
→
12/26 07:19,
7年前
, 15F
12/26 07:19, 15F
→
12/26 07:20,
7年前
, 16F
12/26 07:20, 16F
→
12/26 07:20,
7年前
, 17F
12/26 07:20, 17F
→
12/26 07:21,
7年前
, 18F
12/26 07:21, 18F
※ 編輯: riveranb (42.77.58.61), 12/26/2017 07:23:39
→
12/26 07:26,
7年前
, 19F
12/26 07:26, 19F
→
12/26 07:26,
7年前
, 20F
12/26 07:26, 20F
→
12/26 07:26,
7年前
, 21F
12/26 07:26, 21F
→
12/26 07:26,
7年前
, 22F
12/26 07:26, 22F
※ 編輯: riveranb (42.77.58.61), 12/26/2017 07:28:16
→
12/26 07:32,
7年前
, 23F
12/26 07:32, 23F
推
12/26 07:39,
7年前
, 24F
12/26 07:39, 24F
→
12/26 07:39,
7年前
, 25F
12/26 07:39, 25F
→
12/26 07:40,
7年前
, 26F
12/26 07:40, 26F
→
12/26 07:40,
7年前
, 27F
12/26 07:40, 27F
→
12/26 07:41,
7年前
, 28F
12/26 07:41, 28F
→
12/26 07:41,
7年前
, 29F
12/26 07:41, 29F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
GameDesign 近期熱門文章
PTT遊戲區 即時熱門文章
135
507