IEnumerator SaveAdsImg(string wwwPath, string path, Image img)
{
WWW www = new WWW(wwwPath);
yield return www;
if (www.error == null)
{
if (!WebFileExists(path))
{
// Create a texture in DXT1 format
Texture2D texture = new Texture2D(www.texture.width,
www.texture.height, TextureFormat.RGB24, false) as Texture2D;
// assign the downloaded image to sprite
www.LoadImageIntoTexture(texture);
Rect rec = new Rect(0, 0, texture.width, texture.height);
Sprite itemBGSprite = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 100);
if (img != null)
img.sprite = itemBGSprite;
Texture2D itemBGTex = itemBGSprite.texture;
byte[] itemBGBytes = itemBGTex.EncodeToPNG();
File.WriteAllBytes(path, itemBGBytes);
www.Dispose();
www = null;
}
else
{
// Create a texture in DXT1 format
Texture2D texture = new Texture2D(www.texture.width,
www.texture.height, TextureFormat.RGB24, false) as Texture2D;
// assign the downloaded image to sprite
www.LoadImageIntoTexture(texture);
Rect rec = new Rect(0, 0, texture.width, texture.height);
Sprite itemBGSprite = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 100);
if (img != null)
img.sprite = itemBGSprite;
}
}
}