• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by AwesomeX · Mar 15, 2014 at 04:51 PM · texturesizefilelimit

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _clewis_ · Mar 15, 2014 at 05:36 PM 0
Share

Have you considered using Texture2D.Resize() for images above a certain dimension before plugging the texture into the rest of your game?

avatar image AwesomeX · Mar 15, 2014 at 05:45 PM 0
Share

Hmm, that seems reasonable, I'm just not to sure how I'd use Resize for this.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
         }
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AwesomeX · Mar 15, 2014 at 07:21 PM 0
Share

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..

avatar image AwesomeX · Mar 15, 2014 at 08:56 PM 0
Share

Nevermind, 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!

avatar image _clewis_ · Mar 15, 2014 at 10:25 PM 0
Share

No problem! Shoulda tested it in editor first :P I'll modify the answer for other people.

avatar image AwesomeX · Mar 15, 2014 at 10:52 PM 0
Share

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:

avatar image _clewis_ · Mar 16, 2014 at 06:34 PM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Loading texture file from png/jpg file on disk 3 Answers

editor script - load texture from disk (not assets folder) 3 Answers

Edit FontSettings from script 0 Answers

Load Material Texture from File 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges