Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Showcase how to convert byte array to nativearray<byte> and vice versa ?

Discussion in 'Unity Transport' started by mohammadrezah, Jul 19, 2021.

  1. mohammadrezah

    mohammadrezah

    Joined:
    Jul 12, 2021
    Posts:
    3
    hello
    I want to send byte array from server to client with unity transport . how do I do this?



    Code (CSharp):
    1.          NetworkPipeline pipline = ServerCode._UnreliablePipline;
    2.             if (ServerCode.m_serverDrive.BeginSend(pipline, clientlistener.m_connections, out var sendData) == 0)
    3.             {
    4.                 sendData.WriteBytes(bytearray);
    5.                 ServerCode.m_serverDrive.EndSend(sendData);
    6.  
    7.             }

    Error :


    [I]Argument 1: cannot convert from 'byte[]' to 'Unity.Collections.NativeArray<byte>'[/I]
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    7,624
  3. mohammadrezah

    mohammadrezah

    Joined:
    Jul 12, 2021
    Posts:
    3
    I tried it and I faced with this error

    Code (CSharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <24599fe2776145d58ab771516c063d56>:0)
    4. Unity.Collections.NativeList`1[T].get_Length () (at Library/PackageCache/com.unity.collections@0.17.0-preview.18/Unity.Collections/NativeList.cs:223)
    5.  
     
  4. mohammadrezah

    mohammadrezah

    Joined:
    Jul 12, 2021
    Posts:
    3
    I solved my problem with this line code. but i got another error from this line.

    Code (CSharp):
    1.  
    2.  
    3. NativeArray<byte> NativebyteArray = new NativeArray<byte>(bytearray, Allocator.Persistent);
    4.  
    5.  
    6.    NetworkPipeline pipline = ServerCode._UnreliablePipline;
    7.             if (ServerCode.m_serverDrive.BeginSend(pipline, ServerCode.m_connections[i], out var sendData) == 0)
    8.             {
    9.                 sendData.WriteBytes(NativebyteArray);
    10.                 ServerCode.m_serverDrive.EndSend(sendData);
    11.      
    12.             }
    13.  
    14.  
    error:


    Code (CSharp):
    1. A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
    2. Unity.Collections.NativeArray`1:.ctor(Byte[], Allocator)
     
    Last edited: Jul 19, 2021
  5. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    After `EndSend` call `Dispose` on the native array. Also you probably want to use `Allocator.Temp` here instead of `Persistent`.
     
  6. andrews_unity

    andrews_unity

    Unity Technologies

    Joined:
    Dec 11, 2015
    Posts:
    264
    Also Allocator.Temp is faster here so I would as @luke-unity mention recommend using it but it should be kept around for longer than a frame.
     
unityunity