Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Having trouble with Resources.Load and it incorrectly compressing textures (Unity Free)

Discussion in 'Editor & General Support' started by ReverendWolf, Sep 28, 2014.

  1. ReverendWolf

    ReverendWolf

    Joined:
    Mar 28, 2010
    Posts:
    47
    So my current goal is trying to set up a folder that can be used by the end user to place some images in, and have them be loaded by the game at runtime to be used on game tokens. Resources.Load makes it very easy to load these images and place them into a texture array and drop them into the game.

    The hang up that I am having is that they are being compressed, and done so in what appears to be the ugliest way possible. I've included an image demonstrating the effect. With the images being stuck in the game's hierarchy, I can usually correct this by selecting "Automatic Truecolor" as the compression type. The images I am dealing with are very small, and by themselves will not take up a significant amount of memory, so they don't need to be compressed much at all.

    Looking through all of the documentation, there appears to be ways to create a texture in code, and even select the compression, however, this option is not available to Resources.Load() or LoadAll(), and in the documentation for Texture2D.LoadImage, it states: "After LoadImage, texture size and format might change."

    I tried using Resources.LoadAll to load them in as sprites, but for whatever reason the array would never populate. It only seemed to work when I loaded in the images as Texture2D, which may be a symptom of a different problem. Sprites appear to be the way to go since I believe they are uncompressed, but don't seem to work for me with the Resources option.

    There has to be a relatively non-painless way to do this though, right? I've been through every permutation of search terms that I can think of with different ways to try and solve this issue. As noted in the title, Asset bundles are out since I'm using Free. They'd be out anyway, because I'd rather make the process as idiot proof as possible on the user end, and just tell them what folder to drop their images on to. The only other suggestion I've gotten was to select the compression mode in the editor, but obviously that can't happen if the image isn't there yet.

    Thank you, I would appreciate any insight.


    uglycompression.png
    The compressed texture is on the left, here. You can see that large blocks of colors are blurred together creating a total mess. The Truecolor texture is on the right, keeping it's distinct features.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Resources.Load only works for objects in the build. You can't use it as an external folder; all objects in Resources are combined into a file. You can use System.IO functions or the WWW class to load in external files.

    --Eric
     
  3. ReverendWolf

    ReverendWolf

    Joined:
    Mar 28, 2010
    Posts:
    47
    Well Eric has commented on a post of mine! That means I'm officially a part of the community now!

    Well that takes care of one issue that I hadn't even tested yet, thank you. The Texture2D constructor takes a width,height,and format variable. Based on their documentation examples of creating a 4x4 texture and loading an asset into it with LoadTexture, I can assume that the texture size will update appropriately. Since the Texture Format property is read only, is there any way to make sure that gets locked down?

    Alternatively, I tried creating sprites after loading them in as Textures, but they still were ugly and compressed. I don't suppose there's a way to "reset" the compression before turning my textures into sprites?
     
  4. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    If you are dynamically creating textures and want them to look like pixel art, make sure the filter mode is set to point.
     
  5. ReverendWolf

    ReverendWolf

    Joined:
    Mar 28, 2010
    Posts:
    47
    In case anyone else has this issue, and is searching for this far into the future, this is how I was able to rectify this issue.

    you need these includes to run all the code below:
    using System.Collections.Generic;
    using System.IO;

    IEnumerator LoadSprites()
    {
    Debug.Log("Loading Sprites");
    string _path = UnityEngine.Application.persistentDataPath + "/Tokens"; //this makes sure to load from the same system-prescribed place every time

    if(Directory.Exists(_path))
    {
    string[] _files = Directory.GetFiles(_path,"*.png"); //this grabs all the images from our directory. we can't use resources.load because that doesn't allow outside resources

    List<Texture2D> _t2d = new List<Texture2D>();

    foreach(string s in _files)
    {
    Texture2D t = new Texture2D(4,4,TextureFormat.RGBA32, false); //this creates a texture with the format we want (truecolor). the size is of no consequence since we'll be reading it from byte data
    byte[] f = System.IO.File.ReadAllBytes(s); //this reads the file into a byte array that we use to recreate the texture
    t.LoadImage(f); //this loads the image from the byte array, then we set the point mode
    t.filterMode = FilterMode.Point;
    t.wrapMode = TextureWrapMode.Clamp;
    _t2d.Add(t); //add the texture to a list
    yield return null;
    }

    text2ds = new Texture2D[_t2d.Count]; //create a static array because we don't need a dynamic list

    for(int i = 0; i < text2ds.Length; i++)
    {
    text2ds = _t2d;
    }

    _t2d.Clear(); //clear that list

    }
    else
    {
    Directory.CreateDirectory(_path);
    }

    }
     
  6. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  7. silverfire330

    silverfire330

    Joined:
    Jul 17, 2015
    Posts:
    7
    True or false: It is not possible to change/disable compression for textures when using Resources.Load ?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    If you have your texture compressed in the import settings then it's compressed. If not, then it's not. Resources.Load has nothing to do with it.

    --Eric