1
\$\begingroup\$

I have some Images online that I download using WWW class and the use as UI elements.
So far all of them where working perfectly but now the one I added recently is acting up.
First you should know that all of the images are being downloaded by the same script and handled by that so there should be no difference between their materials and stuff AFAIK (if I'm missing something please tell me).
Second , the problem is that this particular image only shows up when it's taking more than one sixth of the screen, for example if I make it bigger by changing it's size in the inspector, or if I go to scene view and zoom in on it, it starts to fade in the larger it gets on the screen.
Also, I changed the picture to something that was working properly to make sure it's not from the image.


    public IEnumerator AddTools(Tool Tool)
    {
        Texture2D T = new Texture2D(4, 4, TextureFormat.DXT1, true);
        SpriteTool ST = new SpriteTool();
        ST.TheTool = new UnityEngine.Texture();
        ST.ID = Tool.ID;
        ST.ToolName = Tool.Name;
        ST.Type = Tool.Type;
        WWW wWw = new WWW(Tool.Url);
        while (!wWw.isDone)
        {
            yield return wWw.progress;
        }
        wWw.LoadImageIntoTexture(T);
        ST.TheTool = T;
        ST.TheTool.name = Tool.Name;
        SpriteTool PA = ObjectTools.SingleOrDefault(p => p.ID == ST.ID);
    }

The SpriteTool Type is a class I made for storing some of the information needed to build the UI element later.


    private GameObject CreateButton(SpriteTool item = null, float angle = 0)
    {
        Texture2D T2D = (Texture2D)item.TheTool;
        T2D.SetPixels(T2D.GetPixels(0, 0, T2D.width, T2D.height));
        T2D.Apply();
        GameObject Tool = new GameObject();
        Tool.transform.SetParent(GUICanvas.transform);
        //There was some code for positioning the element and resizing it that I  
        //did not include but I can see in the scene view that the size and the position  
        //are what they supposed to be so that's not the problem
        Tool.AddComponent();//The type is RawImage
        Tool.GetComponent().texture = T2D;//the type is RawImage
     }
\$\endgroup\$
  • \$\begingroup\$ Disappearing texture when scaled down sounds a lot like missing mipmaps. Have you generated mipmaps for the loaded texture? answers.unity3d.com/questions/10292/… \$\endgroup\$ – Lasse Jan 23 '17 at 6:53
1
\$\begingroup\$

Disappearing texture when scaled down sounds awfully lot like missing mip-levels. You probably need to generate mipmaps for the loaded texture on runtime. This is done by calling Texture2D.Apply() on your texture.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Start() {
        Texture2D texture = new Texture2D(128, 128);

        // Generate white texture
        for (int y = 0; y < texture.height; y++) {
            for (int x = 0; x < texture.width; x++) {
                texture.SetPixel(x, y, Color.white);
            }
        }

        texture.Apply(); // Generate mipmaps
    }
}

If using just the Apply() method does not work for your texture loaded with the WWW class, you need to use a little hack:

texture.SetPixels( texture.GetPixels(0,0,texture.width,texture.height) );
texture.Apply();

One option is to create a texture before loading the image from WWW with using WWW.LoadImageIntoTexture:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {

    public string url = "https://docs.unity3d.com/uploads/Main/ShadowIntro.png";
    public bool hasMipmaps = true;

    IEnumerator Start() {

        Texture2D tex;
        tex = new Texture2D(4, 4, TextureFormat.DXT1, hasMipmaps);
        WWW www = new WWW(url);
        yield return www;
        www.LoadImageIntoTexture(tex);
    }
}

In this last example, the data from the WWW class will replace the texture size and maybe also the format.

\$\endgroup\$
  • \$\begingroup\$ how can I make sure that the mip-maps where created. and does the type has to be Texture2D? \$\endgroup\$ – Henjin Jan 23 '17 at 7:19
  • \$\begingroup\$ You might try using Texture2D.GetPixels to detect if mipmaps were generated. UI is usually using Texture2D textures, so I do not see what other type you would be using. They can be converted to UI Sprites: answers.unity3d.com/questions/650552/… \$\endgroup\$ – Lasse Jan 23 '17 at 7:25
  • \$\begingroup\$ I'm getting the texture by Texture2D bu then I store it in a Texture type variable could that be the problem? \$\endgroup\$ – Henjin Jan 23 '17 at 7:27
  • \$\begingroup\$ That shouldn't be a problem. \$\endgroup\$ – Lasse Jan 23 '17 at 7:27
  • \$\begingroup\$ It's not woking, I do the last suggestion LoadImageIntoTexture and then I store the texture2d into a texture then I do the set pixel and apply thing and then assign it to the ui \$\endgroup\$ – Henjin Jan 23 '17 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.