Page 1 of 1

Issue iterating through images using LoadImage

Posted: Thu Dec 28, 2023 9:03 pm
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.

Re: Issue iterating through images using LoadImage

Posted: Fri Dec 29, 2023 10:13 am
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

Re: Issue iterating through images using LoadImage

Posted: Sat Dec 30, 2023 9:55 pm
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!