UnityMol  0.9.6-875
UnityMol viewer / In developement
ScreenShot.cs
Go to the documentation of this file.
1 
66 // ****** Notice : It doesn't works in Wep Player environment. ******
67 // ****** It works in PC environment. ******
68 // Default method have some problem, when you take a Screen shot for your game.
69 // So add this script.
70 // CF Page : http://technology.blurst.com/unity-jpg-encoding-javascript/
71 // made by Jerry ( sdragoon@nate.com )
72 
73 using UnityEngine;
74 using System.Collections;
75 using System.IO;
76 using UI;
77 using System;
78 
79 public class ScreenShot : MonoBehaviour
80 {
81  public bool open=false;
82  public bool sequence=false;
83  public string directorypath="";
84  public int frameRate= 30;
85  public int idseq = 0;
86  public int upscaleFactor = 2;
87  public Camera mainCam;
88 
89  void Start(){
90  Time.captureFramerate = frameRate;
91  mainCam = Camera.main;
92  }
93 
94  void Update()
95  {
96  if (open){
97  directorypath = Application.dataPath + "/../Screenshots/" + GUIDisplay.id;
98 
99  if(sequence){
100  directorypath = Application.dataPath + "/../Screenshots/" + GUIDisplay.id+"-"+idseq.ToString()+"/";
101  if(!System.IO.Directory.Exists(directorypath)){
102  // Create the folder
103  System.IO.Directory.CreateDirectory(directorypath);
104  }
105  Application.CaptureScreenshot(directorypath+GUIDisplay.id+"-"+Time.frameCount.ToString()+".png",1);
106  }
107  else{
108 
109  if(!System.IO.Directory.Exists(directorypath)){
110  // Create the folder
111  System.IO.Directory.CreateDirectory(directorypath);
112  }
113 
114  directorypath += "/" + DateTime.Now.ToString ("yyyyMMdd-Hmmss");
115 
117  takeTransparentScreenshot(mainCam.pixelWidth,mainCam.pixelHeight,directorypath+".png",upscaleFactor);
118  else
119  Application.CaptureScreenshot(directorypath+".png", upscaleFactor);
120 
121  open = false;
122  }
123  }
124  }
125  void takeTransparentScreenshot(int width,int height,string path,int upscaleFactor){
126  RenderTexture rt = new RenderTexture(width*upscaleFactor, height*upscaleFactor, 32);
127  mainCam.targetTexture = rt;
128 
129 
130  Color saveCameraColor = mainCam.backgroundColor;
131  mainCam.backgroundColor = new Color(saveCameraColor.r,saveCameraColor.g,saveCameraColor.b,0f);
132 
133  Texture2D sshot = new Texture2D(width*upscaleFactor, height*upscaleFactor,TextureFormat.ARGB32, false);
134  mainCam.Render();
135  RenderTexture.active = rt;
136 
137  sshot.ReadPixels(new Rect(0, 0, width*upscaleFactor, height*upscaleFactor), 0, 0);
138  sshot.Apply();
139  byte[] pngShot = sshot.EncodeToPNG();
140  Destroy(sshot);
141 
142  File.WriteAllBytes(path, pngShot);
143 
144  mainCam.targetTexture = null;
145  RenderTexture.active = null; // JC: added to avoid errors
146  Destroy(rt);
147 
148  mainCam.backgroundColor = saveCameraColor;
149 
150  }
151 
152 
153 
154 
155 
156 }
string directorypath
Definition: ScreenShot.cs:83
static string id
Definition: GUIDisplay.cs:123
void Start()
Definition: ScreenShot.cs:89
int frameRate
Definition: ScreenShot.cs:84
bool sequence
Definition: ScreenShot.cs:82
static bool backGroundTrans
Definition: UIData.cs:130
Camera mainCam
Definition: ScreenShot.cs:87
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
bool open
Definition: ScreenShot.cs:81
!WiP manage GUI, and provide static strings for the GUI.
Definition: GUIDisplay.cs:94
void takeTransparentScreenshot(int width, int height, string path, int upscaleFactor)
Definition: ScreenShot.cs:125
int upscaleFactor
Definition: ScreenShot.cs:86
void Update()
Definition: ScreenShot.cs:94
Definition: GUIDisplay.cs:66