Making VR-screenshot on Android
Hi there!
I was taking over a project using EasyMovieTexture to watch 360° videos on Android / GearVR. Being still new to Unity and C# my task is now to specify an area and to take a screenshot of the actual view.
The first problem is that the screenshot is always being taken from the starting view (e.g. the front although looking to the right)...
The second problem is that the screenshot seems to be taken from a time in the video about 2-3 seconds ago - althought the video is paused for the time of the process. (The idea is to specify the size of the screenshot, but that's for later.)
My actual code looks like this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using VRStandardAssets.Utils;
using System;
using System.IO;
[RequireComponent(typeof(VRInteractiveItem))]
public class DemoController : MonoBehaviour
{
public MediaPlayerCtrl MediaPlayer;
public VRInteractiveItem StartButton;
public Camera cam;
public Texture2D snapshot;
public bool ready = false; //ready for taking screenshots
public bool shot = false; // took screenshot
void Start()
{
this.StartButton.OnClick += this.StartButtonClicked;
}
void StartButtonClicked()
{
this.StartButton.gameObject.SetActive (false);
this.MediaPlayer.Play ();
ready = true;
}
public IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
GameObject go = new GameObject ();
go.AddComponent (typeof(Camera));
cam = go.GetComponent<Camera> ();
int resolution = (int)(Screen.width);
RenderTexture rt = new RenderTexture (resolution, resolution, 24);
cam.targetTexture = rt;
Texture2D screenshot = new Texture2D (resolution, resolution, TextureFormat.RGB24, false);
cam.Render ();
RenderTexture.active = rt;
screenshot.ReadPixels (new Rect (0, 0, resolution, resolution), 0, 0);
cam.targetTexture = null;
RenderTexture.active = null;
Destroy (rt);
Destroy (go);
byte[] bytes = screenshot.EncodeToJPG ();
File.WriteAllBytes ("/sdcard/DCIM/Camera/SavedScreen.jpg", bytes);
}
void Update()
{
if (ready)
{
//taking screenshot
if (!shot && Input.GetMouseButton(0))
{
this.MediaPlayer.Pause();
shot = true;
}
if (shot && Input.GetMouseButtonUp(0))
{
StartCoroutine (TakeScreenshot());
shot = false;
this.MediaPlayer.Play();
}
}
}
}
I found the screenshot-method by googling as other methods result in black pictures which seems to be a common problem... Many people asks that question, but I've done everything they were told to do...
My scene is set up like this:
Hope someone may help me! Thanks! :-)