Search Unity

Texture2D in UI turns gray when scaled down to 0.7 or less

Discussion in 'UGUI & TextMesh Pro' started by plangworthy, Aug 2, 2019.

  1. plangworthy

    plangworthy

    Joined:
    Jan 3, 2017
    Posts:
    13
    I am generating a mini map by writing pixels to a Texture2D. I then pass this Texture2D to UI elements to be displayed on screen.

    The process works great as long as the map is not too big. Once the "small display" has to scale down the mini map texture below a factor of .71, the display turns all gray.

    The Texture2D itself is fine. I use the same Texture for both a small and large view window, and the large vie window still appears normally.

    Size of the Texture2D is not a factor. I have tried on different sized maps (and thus different sized textures) and the result is always the same.

    I have also tried implementing the display code to use both Image and RawImage and the result is the same. At .71 scale it appears normally and at .7 scale it turns gray.

    Code looks like this:
    Code (CSharp):
    1.  
    2. private GameObject CreateMiniMapImageObject(string typeName, Texture2D texture, Vector2 containerSize, bool useImage)
    3.     {
    4.     if(texture == null)
    5.         {
    6.         Debug.Log("Error, Texture2D does not exist for type " + typeName);
    7.         return null;
    8.         }
    9.    
    10.     // Create new GameObject
    11.     GameObject imageGameObject = new GameObject(typeName);
    12.     imageGameObject.transform.SetParent(gameObject.transform);
    13.     imageGameObject.transform.localPosition = Vector3.zero;
    14.     imageGameObject.transform.localScale    = Vector3.one;
    15.    
    16.     // Calculate scaling factor based on ratio of containerSize to texture size
    17.     Vector2 textureSize = new Vector2(texture.width, texture.height);
    18.     Vector2 scale       = containerSize / textureSize;
    19.    
    20.     // if either the x or y component of "scale" is less than .71, the image appears gray
    21.     //scale = new Vector2(.70f, .70f);
    22.    
    23.     // Apply scaling factor to RectTransform
    24.     RectTransform rectTransform = imageGameObject.AddComponent<RectTransform>();
    25.     rectTransform.sizeDelta   = scale * textureSize;
    26.    
    27.     // Create Image or RawImage Component
    28.     if(useImage)
    29.         {
    30.         // Method A: Image via Sprite
    31.         Image image = imageGameObject.AddComponent<Image>();
    32.         image.sprite = Sprite.Create(texture, new Rect(0,0,texture.width,texture.height), new Vector2(.5f, .5f));
    33.         }
    34.     else
    35.         {
    36.         // Method B: RawImage
    37.         RawImage rawImage = imageGameObject.AddComponent<RawImage>();
    38.         rawImage.texture = texture;
    39.         }
    40.    
    41.     return imageGameObject;
    42.     }
    43.  
    I first discovered this when the small mini map in a window that is 384x256 tried to display a mini map for a map that was sized 576 x 384. (scale factor .66) My smaller maps appeared fine and the larger version of this mini map did too, but the smaller one was gray.

    a. Is this reproducible?
    b. Could there be something else in my project setup that is causing this?
    c. Is this a known bug?
    d. Is there a workaround?
     
  2. plangworthy

    plangworthy

    Joined:
    Jan 3, 2017
    Posts:
    13
    Just a quick update from OP. I still haven't solved this issue.

    I discovered recently while doing an actual build as opposed to using debug mode that ALL of the mini maps are gray in my MacOS build regardless of scaling factor. Makes me think it may have something to do with graphics or quality settings perhaps?

    Any help is appreciated.
     
  3. plangworthy

    plangworthy

    Joined:
    Jan 3, 2017
    Posts:
    13
    The issue with grey textures at sizes above .7 when viewing a build (as opposed to editor) turned out to be a Quality settings issue (had somehow set default quality to be "very low").

    However both editor and build still show gray textures when scaled below .7, same as I had originally presented the problem.

    To re-phrase the problem. If I have a texture that is (100x100), and I want to place it in a container that is (70x70), I calculate the ratio between them, which is (0.7,0.7), I then set the sizeDelta of the RectTransform for the gameObject that has the Image Component to (ratio * textureSize), or (70, 70).

    If a 100x100 texture is sizeDelta'd to 70x70, it turns gray, if it's sizeDelta'd to 72x72, it appears normally.