Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. 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

Bug 2021.2.0a21 Texture Instantiation Issue (+Repro)

Discussion in '2021.2 Beta' started by adamgolden, Jun 18, 2021.

  1. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Instantiating textures is broken in the latest alpha. Here's what's happening:

    2021_2_0a21_texture_instantiation_issue.jpg

    And here's the class for testing.

    Code (CSharp):
    1. using UnityEngine;
    2. public class Repro_2021_2_0a21_Texture_Instantiation : MonoBehaviour
    3. {
    4.   [Header("Select a readable texture to test instantiation:")]
    5.   public Texture2D originalTexture;
    6.   [Header("After instantiation, double-click to view result:")]
    7.   public Texture2D instantiatedTexture;
    8.  
    9.   void OnValidate()
    10.   {
    11.     Dispose();
    12.     if (originalTexture != null)
    13.     {
    14.       if (originalTexture.isReadable)
    15.       {
    16.         instantiatedTexture = Instantiate(originalTexture);
    17.       } else
    18.       {
    19.         Debug.LogError("You must first enable Read/Write in texture import settings.");
    20.       }
    21.     }
    22.   }
    23.   void OnDestroy()
    24.   {
    25.     Dispose();
    26.   }
    27.   void Dispose()
    28.   {
    29.     if (instantiatedTexture != null) DestroyImmediate(instantiatedTexture);
    30.     instantiatedTexture = null;
    31.   }
    32. }
    Drag-drop or select any texture which has 'read/write' enabled into the top property, result will appear in the bottom one.

    Edit: Note this only happens in the Editor - both Edit Mode and Play Mode - but NOT in builds.
     
    Last edited: Jun 18, 2021
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    I've found a solution - in 0a21 call .Apply then it's fine (or appears to be.. I haven't looked any deeper than the thumbnails matching in the repro):
    Code (CSharp):
    1. instantiatedTexture = Instantiate(originalTexture);
    2. #if UNITY_EDITOR
    3. instantiatedTexture.Apply();
    4. #endif
    Edit: Added the #if/#endif to skip the call in builds.
     
    Last edited: Jun 19, 2021