0
\$\begingroup\$

I'm trying to work out why the png files I am trying to load from streaming assets are only coming up as the ? error symbol, and having no luck. The files are png files created with an external program (found here, as well as below) that I made, then the relevant ones are copied over and imported with the below code.

This import code works correctly with another png file I have on my machine, but not for any of the png files created by my external program.

Can anyone point me to what I'm doing wrong?

My in-game import code is:

/// <summary>
/// Loads a sprite. 
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Sprite LoadSprite(string name)
{
    //assume bitmap if not referenced. 
    if (!Path.HasExtension(name))
        name += ".png";
    Texture2D texture = new Texture2D(16, 16, TextureFormat.ARGB32, false); 
    WWW www = new WWW("file://" + System.IO.Path.Combine(Application.streamingAssetsPath, name));

    if (!string.IsNullOrEmpty(www.error))
    {
        Debug.LogError("Unable to load sprite: " + name + ". Error: " + www.error);
        return null;

    }
    www.LoadImageIntoTexture(texture); 

    while (!www.isDone) { }

    return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 16);
}

Program used to create png files:

class Program
{
    static Color background = Color.Transparent;
    static Color foreground = Color.White;
    static void Main(string[] args)
    {
        Bitmap unifont = new Bitmap("unifont-10.0.07.bmp");

        Dictionary<string, string> hexes = new Dictionary<string, string>(); 

        using (var reader = File.OpenText(@"unifont-10.0.07\font\plane00\unifont-base.hex"))
        {
            string str; 
            while ((str = reader.ReadLine()) != null)
            {
                var split = str.Split(':');
                hexes.Add(split[0], split[1]); 
            }
        }

        string directory = @"D:\Users\thegreatpl\Desktop\personal projects\Unicode images";// $"{Directory.GetCurrentDirectory()}/output";

        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory); 

        //31, 63. 
        int x = 0; 
        for (int xdx = 32; xdx < unifont.Width; xdx += 16)
        {
            int y = 0; 
            for (int ydx = 64; ydx < unifont.Height; ydx += 16)
            {
                var name = $"{y.ToString("X2")}{x.ToString("X2")}";
                Console.WriteLine(name); 
                var character = unifont.Clone(new Rectangle(xdx, ydx, 16, 16), unifont.PixelFormat);
                //var pallete = character.Palette;
                //pallete.Entries[0] = foreground;
                //pallete.Entries[1] = background;
                //character.Palette = pallete;
                character = ConvertColours(character); 

                if (hexes.ContainsKey(name) && hexes[name].Length < 33)
                {
                    character = Move16x8ToCenter(character); 
                }



                character.Save($"{directory}/U+{name}.png", System.Drawing.Imaging.ImageFormat.Png);
                y++; 
            }
            x++; 
        }
    }

    public static Bitmap Move16x8ToCenter(Bitmap original)
    {
        var newbitmap = new Bitmap(16, 16); 
       // newbitmap.Palette = original.Palette; 
        for (int xdx = 0; xdx <=8; xdx++)
        {
            for (int ydx = 0; ydx < original.Height; ydx++)
            {
                newbitmap.SetPixel(xdx + 4, ydx, original.GetPixel(xdx, ydx)); 
            }
        }

        return newbitmap; 
    }


    public static Bitmap ConvertColours(Bitmap oldbitmap)
    {
        var bitmap = new Bitmap(16, 16);
        for (int xdx = 0; xdx < oldbitmap.Width; xdx++)
        {
            for (int ydx = 0; ydx < oldbitmap.Height; ydx++)
            {
                var oldpix = oldbitmap.GetPixel(xdx, ydx);
                var white = Color.White; 
                if (oldpix.Name == "ff000000")
                    bitmap.SetPixel(xdx, ydx, foreground);
                else
                    bitmap.SetPixel(xdx, ydx, background); 
            }
        }


        return bitmap; 
    }
}
\$\endgroup\$
  • \$\begingroup\$ Hi, I'm curious as to why you use WWW to load the images, I'll admit I've never used it before but had you considered: using System.IO.File.ReadAllBytes(string path), to load the file into a byte array and then if I remember correctly there is a Unity method to convert png byte arrays to textures. \$\endgroup\$ – Thomas Mathieson Aug 10 '18 at 7:58
  • \$\begingroup\$ I was hoping to eventually have a web version loading from a server, and was under the impression WWW was needed for that sort of thing. Still, you can get the bytes from WWW so will try that when I'm not stuck at work. \$\endgroup\$ – Imperial Justinian Aug 10 '18 at 8:08
  • \$\begingroup\$ It looks like you're waiting for the initial file load to complete after trying to import it into the texture. Have you tried yielding until the png file loads before importing it into the texture? \$\endgroup\$ – DMGregory Aug 10 '18 at 12:34
  • \$\begingroup\$ That's... an excellent point. How did I miss that? \$\endgroup\$ – Imperial Justinian Aug 10 '18 at 12:47
  • \$\begingroup\$ I see that you’re running a while loop waiting for www.isDone, which is just not going to work. You’ll need to do that in a coroutine, or something similar. \$\endgroup\$ – Ed Marty Aug 10 '18 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.