- Home /
Sprite rendering texture as one solid color
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?
Try messing around with the Texture / $$anonymous$$aterial settings in the inspector window -- like offsets and tiling if it's a $$anonymous$$aterial--this looks like an issue with the texture itself. You could try upping the size of the texture in the inspector window or fiddling with the texture type.
I don't think it's an issue with the texture because it looks fine when I render it using GUI.DrawTexture(); I'll try messing around with the settings of the texture more though
try messing with the Vector2(0,1) pivot point -- I don't use Sprites so I'm not completely sure how it's handling the UV's etc. I started my project before Unity streamlined all of this. The docs don't define what the pivot point is or what it does--but I think changing to values enough times might show a difference--It may be 'pivoting' the sprite or offsetting it far enough to where only the green transparent pixels are showing. Try #s 1-64 and 0.1-0.9 in both the x and y in various combos.
Answer by Wuffles · Sep 16, 2014 at 04:51 AM
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.