I have a texture that works fine when I render it using GUI.DrawTexture, however when I try applying it to a sprite through a script using:
var tile = new GameObject(“Tile” + x + y);
var sr = tile.AddComponent(SpriteRenderer);
sr.sprite = Sprite.Create(texture, Rect(0, 0, 64, 64), Vector2(0, 1), 64);
it creates a sprite of the correct size at the correct location, but the sprite renders completely solid green(the most common color in my texture) rather than rendering the actual texture. Does anybody know why this might be?
Had this same problem today with a Texture2D, disabling mipmaps worked for me. It seems like Sprite.Create was using a very low-res mipmap for some reason. If you can access your texture in the editor, you can disable mipmaps by going to the texture import settings, changing the Texture Type to “Advanced” and then unchecking “Generate Mip Maps”.
Or if you’re generating the texture in code, use a Texture2D constructor that lets you specify whether or not mipmaps are created, like this one (with whatever textureFormat you need):
Texture2D tex = new Texture2D(16, 16, TextureFormat.ARGB32, false);
The “false” flag in that constructor tells unity to not generate mipmaps for the texture.