Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Windows Build can't create Texture2D on Unity 2019.4 (LTS).

Discussion in 'Editor & General Support' started by Roiw, Aug 28, 2020.

  1. Roiw


    Jan 15, 2013
    Hi everybody thanks for reading!

    I have a project that runs perfectly on the Editor. However, when I make a build it crashes with the error:

    UnityEngine.Texture2D:Internal_CreateImpl (From the stack trace)

    By adding logs on the project I discvered that it happens when I am calling:
    Code (CSharp):
    1. Texture2D tex = new Texture2D(1,1);
    Does anyone knows why creating a texture could cause the build to crash?

    Thank you so much for the help!

    Here is the fuction that calls the line above:

    Code (CSharp):
    1. private async Task<(int, Texture2D)> LoadTextureAsync(string path, int id, CancellationToken ct)
    2.         {
    3.             UnityWebRequest TextureFileRequest = null;
    4.             try
    5.             {
    6.                 Texture2D tex = new Texture2D(1,1);// Decrypt the source file and write it to the destination file.
    7.                 MemoryStream destinationStream = new MemoryStream();
    9.                 Debug.Log(path);
    10.                 using (var sourceStream = File.OpenRead(path))
    11.                 using (var provider = new AesCryptoServiceProvider())
    12.                 {
    13.                     var IV = new byte[Auxiliary.Config.EncryptionIV.Length];
    14.                     sourceStream.Read(IV, 0, IV.Length);
    15.                     using (var cryptoTransform = provider.CreateDecryptor(Auxiliary.Config.EncryptionKey, IV))
    16.                     using (var cryptoStream = new CryptoStream(sourceStream, cryptoTransform, CryptoStreamMode.Read))
    17.                     {
    18.                         cryptoStream.CopyTo(destinationStream);
    19.                         tex.LoadImage(destinationStream.ToArray());
    20.                         return (id, tex);
    21.                     }
    22.                 }
    23.             }
    24.             catch (Exception e)
    25.             {
    26.                 Debug.LogError("Error loading media" + e);
    27.                 if (TextureFileRequest != null)
    28.                     TextureFileRequest.Dispose();
    29.                 return (id, null);
    30.             }
    31.         }
    Here is part of the stack trace:

    ========== OUTPUTTING STACK TRACE ==================

    0x00007FFC58B9C159 (UnityPlayer) UnityMain
    0x00007FFC58B927EF (UnityPlayer) UnityMain
    0x00007FFC58B7011E (UnityPlayer) UnityMain
    0x00007FFC58E8340A (UnityPlayer) UnityMain
    0x000001FBF46DC7A0 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Texture2D:Internal_CreateImpl (UnityEngine.Texture2D,int,int,int,UnityEngine.Experimental.Rendering.GraphicsFormat,UnityEngine.Experimental.Rendering.TextureCreationFlags,intptr)
    0x000001FBF46DC603 (Mono JIT Code) UnityEngine.Texture2D:Internal_Create (UnityEngine.Texture2D,int,int,int,UnityEngine.Experimental.Rendering.GraphicsFormat,UnityEngine.Experimental.Rendering.TextureCreationFlags,intptr)
    0x000001FBF46DA3AB (Mono JIT Code) UnityEngine.Texture2D:.ctor (int,int,UnityEngine.TextureFormat,int,bool,intptr)
    0x000001FBF46DA283 (Mono JIT Code) UnityEngine.Texture2D:.ctor (int,int)
    0x000001FBF46D9AF3 (Mono JIT Code) VIS.Data.Persistent.DataVault/<LoadTextureAsync>d__16:MoveNext ()
    0x000001FBF46D998B (Mono JIT Code) System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<System.ValueTuple`2<int, UnityEngine.Texture2D>>:Start<VIS.Data.Persistent.DataVault/<LoadTextureAsync>d__16> (VIS.Data.Persistent.DataVault/<LoadTextureAsync>d__16&)
    0x000001FBF46D927B (Mono JIT Code) VIS.Data.Persistent.DataVault:LoadTextureAsync (string,int,System.Threading.CancellationToken)
    0x000001FBF463B93B (Mono JIT Code) VIS.Data.Persistent.DataVault/<LoadMenuThumbnails>d__13:MoveNext ()
    0x000001FBF463ACC3 (Mono JIT Code) System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<bool>:Start<VIS.Data.Persistent.DataVault/<LoadMenuThumbnails>d__13> (VIS.Data.Persistent.DataVault/<LoadMenuThumbnails>d__13&)
    0x000001FBF463AB6B (Mono JIT Code) VIS.Data.Persistent.DataVault:LoadMenuThumbnails (System.Threading.CancellationToken)
    0x000001FBF463A843 (Mono JIT Code) VIS.Data.DataManager/<LoadHomeThumbnailsAsync>d__27:MoveNext ()
    0x000001FBF463A733 (Mono JIT Code) System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<bool>:Start<VIS.Data.DataManager/<LoadHomeThumbnailsAsync>d__27> (VIS.Data.DataManager/<LoadHomeThumbnailsAsync>d__27&)
    0x000001FBF463A5F3 (Mono JIT Code) VIS.Data.DataManager:LoadHomeThumbnailsAsync (System.Threading.CancellationToken)
    0x000001FBF4639C1B (Mono JIT Code) VIS.Core.AppManager/<DisplayAvailableClasses>d__41:MoveNext ()
  2. PraetorBlue


    Dec 13, 2012
    Is this code running on a thread other than the main thread? I would guess this might be one of those "Unity doesn't like most Unity objects being touched outside the main thread" situations.
  3. Roiw


    Jan 15, 2013
    Thanks for the reply PraetorBlue.

    I had the same guess.

    It is called from the main thread, but it is a Task and per regular C# code it could run on a separate thread.

    Weird thing is that it works when I build for Android and for iOS ( I had this running in production for a while now).

    I did a small separate test building just a function with an Async Task that creates textures but couldn't replicate the error (I called it several times).

    Keep thinking something is not getting clean or disposed. So I tried calling Dispose on every Task after completed and than a GC.Collect() but no luck.

    To be honest I don't know exactly how Unity handle Tasks under the hood. :(
  4. omar_5


    Feb 8, 2020
    For anyone still having that issue:
    I had that issue for some days, I was trying to get an image from FireBase storage via byte and transfering it to a texture2D; however I had the the same problem creating the texture2D. running the code on main thread resolved it (