![](/img/trans.png)
[英]Send binary file from Java Server to C# Unity3d Client with Protocol Buffer
[英]Memory Mapped file between Java and C# Unity3d
我正在使用平台窗口和 Java(用於寫入文件)和 C# Unity3D 來讀取內存映射文件。
我將它用於 Java
File f = new File("c:\\tmp\\mapped.txt");
f.delete();
FileChannel fc = new RandomAccessFile(f, "rw").getChannel();
long bufferSize=8*1000;
MappedByteBuffer mem =fc.map(FileChannel.MapMode.READ_WRITE, 0, bufferSize);
int start = 0;
long counter=1;
long startT = System.currentTimeMillis();
long noOfMessage = 1000;
for(;;)
{
if(!mem.hasRemaining())
{
start+=mem.position();
mem =fc.map(FileChannel.MapMode.READ_WRITE, start, bufferSize);
}
mem.putLong(counter);
counter++;
if(counter > noOfMessage )
break;
Thread.sleep(400);
}
對於 C# Unity3D,我正在讀取內存中的文件
// Update is called once per frame
void Update()
{
using (MemoryMappedFile mappedFile = MemoryMappedFile.OpenExisting("C:\\tmp\\mapped.txt"))
{
using (var accessor = mappedFile.CreateViewAccessor())
{
accessor.Read(1, out int omegay);
Debug.Log("counter " + omegay.ToString());
}
}
}
當前的問題。
嘗試將訪問模式指定為只讀:
MemoryMappedFile.OpenExisting("C:\\tmp\\mapped.txt", MemoryMappedFileRights.Read))
將相同的內容也應用於視圖訪問器。
我還沒有嘗試過,但是如果 Java 沒有采用完全排他鎖,那么以只讀方式打開文件應該可以工作。 試一試 ;)
~皮諾
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.