- Home /
Image is streched after downloading from server
Hello.
I have C# code that donwloads pictures from server and shows them:
IEnumerator downloadImg (string url)
{
Texture2D texture = new Texture2D(1, 1, TextureFormat.DXT1, false);
WWW www = new WWW(url);
yield return www;
www.LoadImageIntoTexture(texture);
Sprite image = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
renderer.sprite = image;
}
In my editor it shows the result like this:
But when I build to web, it shows this like that:
(Don't mind background) Looks like it streching image. However the size of images remains same, I do not understand why.
What I need to add in my C# code to show it exacly like in Editor?
P.S Max resolution of images are 263 x 163
Answer by Xumbu · May 20, 2014 at 06:03 PM
instead of sprite you could use OnGUI() and GUI.DrawTexture()
GUI.DrawTexture(rect, image, ScaleMode.ScaleToFit, true);
Is there some kind of scale mode for Sprite?
no i don't think so. actually Sprites should "scale to fit" by default.
I don't no why it does not work with your images. Seems to be a bug
I have used your advice and wrote this:
void OnGUI() {
GUI.depth = 10;
if (isDownloaded) {
renderer.sprite = null;
Vector3 myScreenPosition = Camera.main.WorldToScreenPoint(this.transform.position);
Vector2 myPosition = new Vector2();
myPosition.x = myScreenPosition.x;
myPosition.y = myScreenPosition.y;
//guiPosition.x - 100, guiPosition.y - 350
//Vector2 guiPosition = GUIUtility.ScreenToGUIPoint(myPosition);
GUI.DrawTexture(new Rect(myScreenPosition.x - 100, myScreenPosition.y - 240, 263, 163), texture, Scale$$anonymous$$ode.ScaleToFit, true);
}
}
Now that is worked pretty fine, thank you.