How can I create Image from image file on the hard disk and display the image in image ui ?

This is a screenshot of my main menu :

I want to add automatic images from the hard disk between the character and the black in square form like :




For example :

And in each image to display image from the hard disk.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ImagesTest : MonoBehaviour
{
    private Image img;

    // Start is called before the first frame update
    void Start()
    {
        var imagesToLoad = Directory.GetFiles(Application.dataPath + "/screenshots", "*.png");
      
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}

This script is attached to the canvas in my editor in the second screenshot.
In this case I have 2 png files in the imagesToLoad array.
But I’m not sure how and what to do next in the script.

So instead in the editor to create now two images manual and assign the images from the hard disk to the images I want to it automatic.
If I have 2 png file on the hard disk or 30 automatic create the images ui in the editor and add the images from the hard disk to them. And if there are too many images make a scrollbar effect.

This is how the formation should looks like :

The main goal is to create save game slots and the first step is to read the images from the hard disk and create ui images automatic in the editor in this formation in this positions between the character and the black thing and add the images to the images ui.

Depending on how many png files there are on the hard disk create the same amount of ui images in the editor and add the images the png files to the ui images.

The last screen show show the images on my hard disk :
I want to show them in the editor also with the name and date and time under each image.

The idea is to create saved games ui slots.

UI Image components display Sprites.

If you’ve already got the raw image data from the file, you can use Texture2D.LoadImage to convert PNG data into a Texture2D and then use Sprite.Create to make a Sprite from that Texture2D.

2 Likes

You can use System.IO calls (plain old C# calls) to load PNG files as arrays of bytes.

Then you can create a tiny texture and load it with those bytes, which will cause Unity to decompress the texture.

I believe the call for that is:

You can make a sprite out of that and stick it in your UI.Image or a sprite, or I think you can put it into a UI.RawImage too.

1 Like

Ok I used RiawImages the result is :

And the script I changed it to :

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class SavedGamesSlots : MonoBehaviour
{
    Texture2D thisTexture;
    byte[] bytes;
    string fileName;
    public GameObject[] ImageHolder = new GameObject[1];

    // Start is called before the first frame update
    void Start()
    {
        var imagesToLoad = Directory.GetFiles(Application.dataPath + "/screenshots", "*.png");
        for (int i = 0; i < imagesToLoad.Length; i++)
        {
            thisTexture = new Texture2D(100, 100); //NOW INSIDE THE FOR LOOP
            fileName = imagesToLoad[i];
            bytes = File.ReadAllBytes(fileName);
            thisTexture.LoadImage(bytes);
            thisTexture.name = fileName;
            ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;

        }
    }
}

but the images now are not looking good in the main menu. I need some other design or something I think.
Any ideas how to make the images more to look like a saved games slot and not images on main menu like this ?

I mean to make something that will exclude the images from the main menu background it looks like the images are part of the main menu background. maybe to add somehow a frame to each image ?

Any ideas please ?

1 Like

A frame is easy, just add an image behind the RawImage, make it whatever color you want for the frame, and make it a few pixels bigger in each dimension. No image file needed, just set the color. So you’d have an array of them that matches the ImageHolder array. Have them inactive until the other images are shown.

1 Like

Yes, very much this, what CJ says above.

Here’s a super-useful pattern:

Pre-make a prefab that you Instantiate into the scene (appropriately childed into your UI hierarchy… ALWAYS use the Instantiate that takes a second parent transform argument!!!) and populate that with your PNG file.

Give that Prefab a special little script with an anchor to the GameObject within the prefab that has your RawImage, so that way “injecting” it is easy.

From then on as long as your code just instantiates one of those and “injects” the picture into it, you never again have to write any code if you want to spiff up or improve the looks of your little frame prefab. Or use different prefabs for different game slots, for instance.

For the record this is 100% the way I do it and the way just about everybody I know in Unity does it. You can even hire an artist in the future and have them spiff up the frame, and you don’t have to change any code. That’s the magic of Unity3D!

1 Like

While you’re making that prefab, you may also want to leave some space for text to display other useful information, such as the date+time when the game was saved, play time, % completion, etc.

Looks like your pictures are also being stretched because the aspect ratio of the PNG doesn’t match the aspect ratio of the RawImage component.

Image components have an option to preserve aspect ratio (by letterboxing). RawImage components do not–although you can always change the size of the object to match the desired ratio.

1 Like

How do I make the Image to be behind the RawImage ? When I added Image even if changed it’s size and color it’s still over on the imageraw.

Canvas elements are drawn in the order in the Hierarchy, just drag the image/frame below the RawImage.

To elaborate on that a bit more:

  • If two objects are siblings, the last sibling is drawn on top
  • If one object is the child of another, the child is drawn on top
  • If your Canvas is in Screen Space - Overlay mode (which is the default), the Z coordinate of objects is ignored. But if you switch to Screen Space - Camera mode (and specify a camera), then Z coordinates are honored, and so then you can also control order that way (which trumps the above rules).
1 Like