- Home /
Limit WWW.texture file size?
I'm using WWW.texture to assign a custom texture to a player, from a url.
The problem is, I need a way to limit the player from, lets say using a massive texture, such as 2024 x 1650 or something like that.
Since each player can enter in its own URL that leads to a texture, I need some way to regulate/limit it.
Have you considered using Texture2D.Resize()
for images above a certain dimension before plugging the texture into the rest of your game?
Hmm, that seems reasonable, I'm just not to sure how I'd use Resize for this.
Answer by _clewis_ · Mar 15, 2014 at 06:00 PM
Here's an example modified from Unity's WWW.texture docs:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public string url = "http://carillon-beach.com/images/plates/large/beach-1.jpg";
public int maxResolutionX = 512;
public int maxResolutionY = 512;
IEnumerator Start() {
// load the texture
WWW www = new WWW(url);
yield return www;
Texture2D texture = www.texture;
Debug.Log(string.Format("{0}, {1}", texture.width, texture.height));
// resize the texture to fit in the desired bounds
if (texture.width > maxResolutionX || texture.height > maxResolutionY) {
float widthRatio = (float)maxResolutionX / texture.width;
float heightRatio = (float)maxResolutionY / texture.height;
if (widthRatio < heightRatio) {
texture = Resize(texture, (int)(texture.width * widthRatio), (int)(texture.height * widthRatio));
} else {
texture = Resize(texture, (int)(texture.width * heightRatio), (int)(texture.height * heightRatio));
}
}
Debug.Log(string.Format("{0}, {1}", texture.width, texture.height));
// apply the texture to the renderer
renderer.material.mainTexture = texture;
}
// returns a texture resized to the given dimensions
public Texture2D Resize(Texture2D source, int width, int height) {
// initialize render texture with target width and height
RenderTexture rt = new RenderTexture(width, height, 32);
// set as the active render texture
RenderTexture.active = rt;
// render source texture to the render texture
GL.PushMatrix();
GL.LoadPixelMatrix(0, width, height, 0);
Graphics.DrawTexture(new Rect(0, 0, width, height), source);
GL.PopMatrix();
// initialize destination texture with target width and height
Texture2D dest = new Texture2D(width, height);
// copy render texture to the destination texture
dest.ReadPixels(new Rect(0, 0, width, height), 0, 0);
dest.Apply();
// resume rendering to the main window
RenderTexture.active = null;
return dest;
}
}
When run, this example should log 900, 507
, the original dimensions of the image, and 512, 288
, the dimensions of the scaled image.
It's using RenderTextures, which requires Unity Pro. If that's an issue, your best bet would be to check the width and height of the texture that's loaded, and if it's out of bounds choose a default texture instead and discard what they loaded. Example:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public string url = "http://carillon-beach.com/images/plates/large/beach-1.jpg";
public int maxResolutionX;
public int maxResolutionY;
public Texture2D defaultTexture;
IEnumerator Start() {
// load the texture
WWW www = new WWW(url);
yield return www;
// set material to default texture if texture is out of bounds
if (www.texture.width > maxResolutionX || www.texture.height > maxResolutionY) {
renderer.material.mainTexture = defaultTexture;
} else {
renderer.material.mainTexture = www.texture;
}
}
}
Using that code I get 4 errors.
error CS1502: The best overloaded method match for `UnityEngine.Texture2D.Resize(int, int)' has some in
error CS1503: Argument `#1' cannot convert `float' expression to type `int'
error CS1502: The best overloaded method match for `UnityEngine.Texture2D.Resize(int, int)' has some invalid arguments
error CS1503: Argument `#1' cannot convert `float' expression to type `int'
Just so you know, lol.
I sorta understand, but need a bit of clarification, and working code..
Never$$anonymous$$d, mate!
Changed the Floats, to ints and hooked it up with some code changes to fit my needs, and got it all working. Thanks a lot!
No problem! Shoulda tested it in editor first :P I'll modify the answer for other people.
Bad news, I seem to be getting an error now, I changed nothing.
Its not resizing images larger then the max set size. And it gives me this error.
Texture has out of range width / height
UnityEngine.Texture2D:Resize(Int32, Int32)
<URL>c__Iterator9:$$anonymous$$oveNext() (at Assets/Scripts/PlayerColor.cs:140)
D:
$$anonymous$$an, this is really embarrassing. Turns out I had the calculations for the width/height ratios flipped. On top of that, it turns out the Texture2D.Resize()
function clears the pixels of the image while resizing, so you have to use render textures ins$$anonymous$$d. I updated the answer with an actually working solution, and a fallback in case you don't have Unity Pro.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140321im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Texture size 2 Answers
Why does assetbundle load change texture size? 1 Answer
Limit GUI Rotation? 2 Answers
issue regarding image file sizes 0 Answers