Hi all! I’m loading images from at runtime and use them as textures:
prospect.meshRenderer.material.mainTexture = (Texture)Resources.Load (“image”); //image.png is loaded from web.
The problem is images lose dimensions because default setting using non power of 2 compressing algo is enabled. How to disable it by code?
That’s not a setting of the texture itself. It’s an import setting. You can create an AssetPostProcessor and use OnPreprocessTexture to change the import settings. Note that you need to reimport the texture. Also keep in mind that without any kind of selection logic the preprocessor will work on all textures.
You can use the asset path to determine whether the settings should be changed or not. For example place all GUI textures in a folder called GUI. In OnPreprocessTexture you can check the assetpath if it contains the folder name GUI and change the texture-import setting to GUI.
downloading image from web:
string fileName;
void DownloadTexture (WWW buffer)
{
fileName = textureFolder + Path.GetFileName (buffer.url);//textureFolder == Application.persistentDataPath + "/Resources/Textures"
if (buffer.error != null || buffer.bytes.Length == 0) {
//TODO: handle error
}
File.WriteAllBytes (fileName, buffer.bytes);
}
When i want change texture:
go.meshRenderer.material.texture = (Texture)Resources.Load(filename);