简体   繁体   中英

List of Texture2D

I am creating a simple game that need to capture a photo from the webcam every time the user click on some object. To avoid lag issues because of the task of convert the Texture2D to PNG and write the image on the disk, I am trying to store one List of Texture2D from the captures, and after the game ends, write all on the disk.

The problem is, when I capture one Texture2D texture from the webcam and try to do a List.Add(texture) , all the elements of the list are updated because it store the reference of texture , not the Texture itself. Can anyone please, suggest me one alternative to store all the textures?

Edit: inserting code.

public class GetPhoto : MonoBehaviour
{
WebCamTexture webcam;
Texture2D photo;
List<Texture2D> photos;

IEnumerator TakePhoto()
{
    //run when user click on object.
    yield return new WaitForEndOfFrame();
    photo.SetPixels(webcam.GetPixels());
    photo.Apply();
    photos.Add(photo);
}

}

As said rather use a new Texture2D instance for every press like eg

IEnumerator TakePhoto()
{
    //run when user click on object.
    yield return new WaitForEndOfFrame();
    var photo = new Texture2D(yourTextureWidth, yourTextureHeight);
    photo.SetPixels(webcam.GetPixels());
    photo.Apply();
    photos.Add(photo);
}

otherwise every entry in your list refers to the exact same Texture2D instance => everytime you overwrite the content of that instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM