Instantiate screenshots from camera *Realtime*
Hi there! I am working on a VR film-making app. What I am trying to do: on "K" key press, save a png from a RenderTexture (camera view) in the Resources folder and instantiate it on the scene so the user can view the shot he just took. The user can take several shots. For the moment, I have this code that kinda works. But I have a problem with the first shot: on the scene, it first appears purple (missing material), and when I press K again (and again), it always take the texture of the last shot...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class ScreenShots : MonoBehaviour {
public int resWidth = 1080;
public int resHeight = 720;
public RenderTexture Preview;
public List<Image> allImages;
public int imageIndex = 0;
public GameObject Parent;
void Update () {
if (Input.GetKeyDown("k")) {
//Destroy all existing 'vignettes'
GameObject[] vignettes = GameObject.FindGameObjectsWithTag("vignette");
foreach(GameObject vignette in vignettes)
GameObject.Destroy(vignette);
//RenderTexture to PNG in Resources folder
Texture2D screenShot = new Texture2D(resWidth, resHeight,
TextureFormat.RGB24, false);
RenderTexture.active = Preview;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
RenderTexture.active = null;
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Resources/screen.png"
+ System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png"
;
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
StartCoroutine(LoadAllScreens());
}
}
IEnumerator LoadAllScreens()
{
Debug.Log ("LoadAllScreens");
//Load all pngs in Resources folder
string[] imageFilePaths = Directory.GetFiles("F://master/Assets/Resources", "*.png");
for (int i = 0; i < imageFilePaths.Length; i++)
{
Texture2D SavedScreenShot;
SavedScreenShot = new Texture2D (resWidth, resHeight,
TextureFormat.RGB24, false);
WWW diskImageDir = new WWW("file://" + imageFilePaths[i]);
//Wait for file finish loading
while (!diskImageDir.isDone)
{
yield return null;
}
diskImageDir.LoadImageIntoTexture(SavedScreenShot);
var ParentX = Parent.transform.position.x;
var ParentY = Parent.transform.position.y;
var ParentZ = Parent.transform.position.z;
//Instantiate a screen Prefab with new material with main texture = SavedScreenShot
GameObject cube = Resources.Load ("Cube") as GameObject;
var newCubes = Instantiate (cube, new Vector3(ParentX + 2*i, ParentY, ParentZ), Quaternion.identity);
newCubes.transform.parent = Parent.transform;
Renderer rend;
rend = cube.GetComponent<Renderer> ();
rend.material = new Material (Shader.Find("Specular"));
rend.sharedMaterial.SetTexture ("_MainTex", SavedScreenShot);
Debug.Log ("Instantiate Cube");
}
}
}
Everything else is working fine but I can't really figure out where is the problem with the first "K" press. Is it the "for" that is not correct? I can provide screenshots if needed. Thanks for your help :)
Your answer
Follow this Question
Related Questions
Seemingly random behaviour when Instantiating numerous Prefabs 1 Answer
How to turn everything black except from certain objects in the scene in a first person camera view 1 Answer
How to attach camera to the instantiated gameobjects sequentially in a for loop using C# script? 0 Answers
How to do a wall jump? i am very novice 0 Answers