Taking a Screenshot and then displaying the Screenshot in game
Hi all!
I'm making a game that has photography mechanics and I figured the best way to do this would be to take a screenshot using Application.CaptureScreenshot and then reassigning the taken screenshot to an object as a Texture.
The problem I am running into is after I successfully take the screenshot I am unable to get the game to reload the new image onto the object during runtime. The image will successfully reload after restarting the editor or by pausing and unpausing in the editor. Also, I was able to get it to work by using the UnityEditor.AssetDatabase.Refresh and then telling Unity to assign the screenshot as a texture. While this last method works in the editor, it won't work in a build which is what I need.
Is there a way I can tell Unity every once in a while to refresh a folder so that way it will apply the changes that have been made in game?
Here is my code:
Screenshot.cs attatched to the Player
using UnityEngine;
using System.Collections;
using System.IO;
public class ScreenShot : MonoBehaviour {
public GameObject ui_CameraUI;
private bool startResetCameraUI;
private float resetCameraUI;
void Start(){
resetCameraUI = 0.0f;
}
void Update(){
if (Input.GetButtonDown ("Fire1")) {
ui_CameraUI.SetActive (false);
}
}
void LateUpdate() {
if (startResetCameraUI) {
resetCameraUI += Time.deltaTime;
}
if (resetCameraUI >= 0.000001f) {
ui_CameraUI.SetActive (true);
startResetCameraUI = false;
resetCameraUI = 0.0f;
}
if(Input.GetButtonDown("Fire1")){
if (Application.isEditor) {
Application.CaptureScreenshot ("Assets\\Resources\\Screenshot.bytes");
} else {
Application.CaptureScreenshot ("Resources\\Screenshot.bytes");
}
Debug.Log ("*Camera Sound*");
startResetCameraUI = true;
}
}
}
TextureReassign.cs attatched to a GameObject
using UnityEngine;
using System.Collections;
public class MaterialReassign : MonoBehaviour {
void Update(){
Texture newpic = Resources.Load("Screenshot", typeof(Texture)) as Texture;
gameObject.GetComponent<Renderer> ().material.mainTexture = newpic;
}
}
Your answer
Follow this Question
Related Questions
How to Get a JPG/ PNG Render from a Selected Camera ? 0 Answers
I can not see the title screen of my game 0 Answers
How to match position with Mini Map UI 0 Answers
Need help with an image path script 0 Answers
Why is there difference between android device resolution and webcamtexture resolution 0 Answers