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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Performance, Marshal.Copy and NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray

Discussion in 'WebGL' started by huadongchuanmei, Oct 19, 2022.

  1. huadongchuanmei

    huadongchuanmei

    Joined:
    Jan 27, 2022
    Posts:
    6
    Hi,

    Our project shows remote webrtc video in unity Texture2D. The Video image data is captured from C++ native code.
    Currently, we copy the native image data to c# byte[], I think we should have a better way to avoid marshalling costs. (Remark: After I copy the data, native c++ will delete the data buffer, I can't control it)

    I hava two questions:
    1. Is NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray with Allocator.Persistent is better than Marshal.Copy to c# byte[]?
    2. Will the garbage collector free the NativeArray aotumaically? Will it call NativeArray.dispose()? Because another main thread will read the NativeArray to Texture2D, I can't free it in the backgound thread.

    Thanks!
     
  2. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    555
    Use the GetNativeTexturePtr, and WebGL texsubimage2d to draw the texture on the JS side
     
  3. huadongchuanmei

    huadongchuanmei

    Joined:
    Jan 27, 2022
    Posts:
    6
    Sorry,Seems I post in wrong topic.Our app runs in Android、iOS、Windows...
    Thanks!
     
  4. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    555