CreateExternalTexture crash

Dear all.

I am experiencing crash with this simple code:

var srcTex = new Texture2D(width, height, format, mipChain, linear);
var nativeTex = srcTex.GetNativeTexturePtr();
var dstTex = Texture2D.CreateExternalTexture(width, height, format, mipChain, linear, nativeTex);

I am using a library which requires a native pointer texture, and performs a Texture2D.CreateExternalTexture() with this native pointer.

I tryed to create a normal Texture2D, retrive the NativeTexturePtr, then transmit to the library, but the UnityEditor crash in CreateExternalTexture.

Does someone have a solution to copy the native pointer from a Texture2D to another ?

Sincerely

PS: I am using Unity 2019.2.3f1 on Windows 10.
PS: This script is only for Editor Debug, but possibly Windows, Mac and Linux

Your nativeTex will contain the info needed to access the native texture in your external library

you don’t need to use CreateExternalTexture. This method will do the reverse, if your library would create a native texture then you can make a Unity Texture2D out of it with this method.

It is indeed a bug that Unity crashes with this code though.

Hello! I also ran into this issue. Here’s the code that crashes my 2018.4.14f1 version of Unity:

    void Start()
    {
        int width = 200;
        int height = 200;
        byte[] data = new byte[width * height];
        GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
        Texture2D.CreateExternalTexture(width, height, TextureFormat.R8, false, true, handle.AddrOfPinnedObject());
    }

I know this shouldn’t probably work this way, but still… Maybe this will help