- Home /
Updating Taken Pictures in Game
Hey all! I am using the following code to create usable .png files in my resources folder which are being called to a texture inside of my game.
using UnityEngine; using System.Collections; using System.IO;
public class SnappitPictureTake : MonoBehaviour {
private int count = 0;
void Update()
{
if (Input.GetKeyDown("k"))
StartCoroutine(ScreenshotEncode());
}
IEnumerator ScreenshotEncode()
{
// wait for graphics to render
yield return new WaitForEndOfFrame();
// create a texture to pass to encoding
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// put buffer into texture
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
// split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
yield return 0;
byte[] bytes = texture.EncodeToPNG();
// save our test image (could also upload to WWW)
File.WriteAllBytes(Application.dataPath + "/../Assets/Resources/testscreen-" + count + ".png", bytes);
count++;
// Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
DestroyObject( texture );
Debug.Log( Application.dataPath + "/../Assets/Resources/testscreen-" + count + ".png" );
}
}
The issue, however, is that these pictures are supposed to update in game over each other when a picture overwrites a previous one. They do eventually do such, but only if you exit the unity menu and return to the game, or otherwise restart the game.
Any way that I can force the game to call upon the newly updated file, rather than wait for a refresh? I'd like to allow the photos to be viewed in game and be the actual photos to be viewed, rather than otherwise.
Answer by unimechanic · Dec 03, 2014 at 02:38 PM
The description of the problem is pretty vague, but if you have the script you can modify it to fit your needs:
https://unity3d.com/learn/tutorials/modules/beginner/scripting