Issue iterating through images using LoadImage

Discussion and help for Easy Save 3
Post Reply
syu15
Posts: 16
Joined: Thu May 13, 2021 1:05 am

Issue iterating through images using LoadImage

Post by syu15 »

Hello!
I am currently trying to save and load images using Easy Save 3's SaveImage/LoadImage. It seems to work as expected except when I'm trying to loop through GameObjects and load images using either a for loop or a coroutine. For some reason, all the GameObjects render the last image I was trying to load (screenshot attached). Not sure why that is. When I try to space them out using a coroutine/waitforseconds the images fail to render at all.

This is the code I'm using (condensed to the relevant parts)

Code: Select all

        for (int i = 0; i < PhotosData.AllPhotos[i].Count; i++)
        {
            GameObject CurrentItemGameObject = PhotosGameObjects[i];
	    //This file path is unique to each photo, I double checked this already
            string CurrentPhotoName = PhotosData.AllPhotos[i].PhotoFilePath;

            Texture2D Texture = ES3.LoadImage(CurrentPhotoName);

            Texture.filterMode = FilterMode.Point;
            Texture.wrapMode = TextureWrapMode.Clamp;

            CurrentItemGameObject.transform.Find("PhotoImage").GetComponent<Image>().material.mainTexture = Texture;

            CurrentItemGameObject.transform.Find("PhotoImage").gameObject.SetActive(true);
        }
Should I not be looping through and trying to load images? Is there some latency I need to account for? I have tried various solutions without success.
Attachments
Screenshot 2023-12-28 125216.png
Screenshot 2023-12-28 125216.png (74.42 KiB) Viewed 890 times
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Issue iterating through images using LoadImage

Post by Joel »

Hi there,

This issue will be your logic rather than at our end. You should check that CurrentPhotoName isn't referring to the same image, and that the images you're trying to load aren't all the same image when saved. You should also check that CurrentItemGameObject.transform.Find("PhotoImage").GetComponent<Image>().material isn't referring to the same Material for each of your objects. Otherwise changing the Material on one object will change the Material on all objects. However, I recommend asking on the Unity forums for more information as this isn't related to saving and loading.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
syu15
Posts: 16
Joined: Thu May 13, 2021 1:05 am

Re: Issue iterating through images using LoadImage

Post by syu15 »

I did double check that `CurrentPhotoName` was a unique file path hence my comment above that line of code :)

It was related to the UI image components sharing the same material by default. I was able to fix it like this in case anyone else runs into this problem:

Code: Select all

                Texture2D Texture = ES3.LoadImage(CurrentPhotoName);

                Texture.filterMode = FilterMode.Point;
                Texture.wrapMode = TextureWrapMode.Clamp;

                Material ImageMaterial = Instantiate(
                    CurrentItemGameObject.transform.Find("PhotoImage").GetComponent<Image>().material
                    );

                ImageMaterial.mainTexture = Texture;

                CurrentItemGameObject.transform.Find("PhotoImage").GetComponent<Image>().material = ImageMaterial;
Thanks!
Post Reply