11using UnityEngine ;
22using System . IO ;
3- using UnityEngine . Serialization ;
43
54public enum QualityCam
65{
@@ -13,7 +12,8 @@ public class VideoRecorder : MonoBehaviour
1312 public QualityCam QualityOfCam ;
1413 public bool UseScreenSize = true ;
1514 public bool MakeFreezingForCinema = true ;
16- public bool RecordAlwaysOn ;
15+ public bool RecordAlwaysOn = false ;
16+ public bool RecordOnlyMainScreen = false ;
1717 #endregion
1818 private string dataPath ;
1919 Camera ScrnCam ;
@@ -48,30 +48,23 @@ private void Awake()
4848 VideoTexture . width = ( int ) Width ;
4949 VideoTexture . height = ( int ) Height ;
5050 }
51-
52- if ( ! RecordAlwaysOn )
53- {
54- ScrnCam . gameObject . SetActive ( false ) ;
55- }
5651 //if there is no file save directory
5752 //we will make it
5853 bool exists = System . IO . Directory . Exists ( dataPath ) ;
5954 if ( ! exists )
6055 System . IO . Directory . CreateDirectory ( dataPath ) ;
6156 }
62- void Start ( ) {
57+ void Start ( ) {
6358 // for rendering, after changing the scene
64- DontDestroyOnLoad ( this . gameObject ) ;
59+ // DontDestroyOnLoad(this.gameObject);
6560 fixedDeltaTimeCache = Time . fixedDeltaTime ;
66- if ( ! RecordAlwaysOn )
61+ if ( RecordAlwaysOn )
6762 MakeVideo ( ) ;
6863 }
6964
7065 public void MakeVideo ( )
7166 {
72- if ( ! RecordAlwaysOn )
73- {
74- isCapturing = ! isCapturing ;
67+ isCapturing = ! isCapturing ;
7568 if ( isCapturing )
7669 {
7770 ScrnCam . gameObject . SetActive ( true ) ;
@@ -84,7 +77,6 @@ public void MakeVideo()
8477 }
8578 else
8679 {
87-
8880 ScrnCam . gameObject . SetActive ( false ) ;
8981 if ( MakeFreezingForCinema )
9082 {
@@ -93,32 +85,42 @@ public void MakeVideo()
9385 }
9486
9587 }
96- }
88+
9789 }
9890 void LateUpdate ( ) {
99- if ( ScrnCam . gameObject . activeInHierarchy ) {
91+ if ( isCapturing )
92+ {
10093 localDeltaTime = Time . realtimeSinceStartup - prevTime ;
101- prevTime = Time . realtimeSinceStartup ;
102- Texture2D Shot = ToTexture2D ( VideoTexture ) ;
103- //transform Texture Into Picture
104- if ( QualityOfCam == QualityCam . PNG )
94+ prevTime = Time . realtimeSinceStartup ;
95+ if ( ! RecordOnlyMainScreen )
10596 {
106- byte [ ] bytes = Shot . EncodeToPNG ( ) ;
107- string filename = path + imgIndex . ToString ( "D8" ) + ".png" ;
108- File . WriteAllBytes ( filename , bytes ) ;
97+ Texture2D Shot = ToTexture2D ( VideoTexture ) ;
98+ //transform Texture Into Picture
99+ if ( QualityOfCam == QualityCam . PNG )
100+ {
101+ byte [ ] bytes = Shot . EncodeToPNG ( ) ;
102+ string filename = path + imgIndex . ToString ( "D8" ) + ".png" ;
103+ File . WriteAllBytes ( filename , bytes ) ;
104+ }
105+ else
106+ {
107+ byte [ ] bytes = Shot . EncodeToJPG ( ) ;
108+ string filename = path + imgIndex . ToString ( "D8" ) + ".jpg" ;
109+ File . WriteAllBytes ( filename , bytes ) ;
110+ }
109111 }
112+
110113 else
111114 {
112- byte [ ] bytes = Shot . EncodeToJPG ( ) ;
113- string filename = path + imgIndex . ToString ( "D8" ) + ".jpg" ;
114- File . WriteAllBytes ( filename , bytes ) ;
115- }
116- imgIndex += 1 ;
117- if ( MakeFreezingForCinema )
118- {
119- Time . timeScale = 1.0f / localDeltaTime / frameRate ;
120- //Time.fixedDeltaTime = fixedDeltaTimeCache / Time.timeScale;
115+ ScreenCapture . CaptureScreenshot ( path + imgIndex . ToString ( "D8" ) + ".png" ) ;
121116 }
117+ imgIndex += 1 ;
118+ if ( MakeFreezingForCinema )
119+ {
120+ Time . timeScale = 1.0f / localDeltaTime / frameRate ;
121+ //Time.fixedDeltaTime = fixedDeltaTimeCache / Time.timeScale;
122+ }
123+
122124 }
123125 }
124126 //from RenderTexture to Texture2D
0 commit comments