11using UnityEngine ;
22using System . IO ;
33
4+ public enum QualityCam
5+ {
6+ PNG , JPG
7+ }
48[ RequireComponent ( typeof ( Camera ) ) ]
5- public class VideoRecorder : MonoBehaviour {
6-
7-
8- public int frameRate = 30 ;
9+ public class VideoRecorder : MonoBehaviour
10+ {
11+ public QualityCam QualityOfCam ;
912 public RenderTexture VideoTexture ;
1013 Camera ScrnCam ;
1114 float Width = Screen . width ;
1215 float Height = Screen . height ;
1316 private string dataPath ;
17+ public bool UseScreenSize = true ;
18+
1419 #region VideoCapt
20+ public int frameRate = 30 ;
1521 bool isCapturing = false ;
1622 string path ;
1723 int folderIndex = 0 ;
1824 int imgIndex = 0 ;
1925 float localDeltaTime , prevTime , fixedDeltaTimeCache ;
20-
2126 #endregion
2227
2328 private void Awake ( )
@@ -27,10 +32,19 @@ private void Awake()
2732 ScrnCam = GetComponent < Camera > ( ) ;
2833 if ( VideoTexture == null )
2934 {
30- Debug . Log ( "Создайте текстуру для рендера!" ) ;
35+ Debug . Log ( "Создайте текстуру для рендера! \n Create a texture for the render! " ) ;
3136 }
32- Width = VideoTexture . width ;
33- Height = VideoTexture . height ;
37+ if ( ! UseScreenSize )
38+ {
39+ Width = VideoTexture . width ;
40+ Height = VideoTexture . height ;
41+ }
42+ else
43+ {
44+ VideoTexture . width = ( int ) Width ;
45+ VideoTexture . height = ( int ) Height ;
46+ }
47+
3448 ScrnCam . gameObject . SetActive ( false ) ;
3549 //если нет директории сохранения файла
3650 bool exists = System . IO . Directory . Exists ( dataPath ) ;
@@ -63,12 +77,23 @@ void LateUpdate () {
6377
6478 if ( ScrnCam . gameObject . activeInHierarchy ) {
6579 localDeltaTime = Time . realtimeSinceStartup - prevTime ;
66- prevTime = Time . realtimeSinceStartup ;
80+ prevTime = Time . realtimeSinceStartup ;
81+ Texture2D Shot = ToTexture2D ( VideoTexture ) ;
6782 //перевод текстуры в картинку
68- Texture2D Shot = ToTexture2D ( VideoTexture ) ;
69- byte [ ] bytes = Shot . EncodeToPNG ( ) ;
70- string filename = path + imgIndex . ToString ( "D8" ) + ".png" ;
71- File . WriteAllBytes ( filename , bytes ) ;
83+ if ( QualityOfCam == QualityCam . PNG )
84+ {
85+ byte [ ] bytes = Shot . EncodeToPNG ( ) ;
86+ string filename = path + imgIndex . ToString ( "D8" ) + ".png" ;
87+ File . WriteAllBytes ( filename , bytes ) ;
88+ }
89+ else
90+ {
91+ byte [ ] bytes = Shot . EncodeToJPG ( ) ;
92+ string filename = path + imgIndex . ToString ( "D8" ) + ".jpg" ;
93+ File . WriteAllBytes ( filename , bytes ) ;
94+ }
95+ //декодируем в png, для наивышего качества
96+
7297 imgIndex += 1 ;
7398 Time . timeScale = 1.0f / localDeltaTime / frameRate ;
7499 //Time.fixedDeltaTime = fixedDeltaTimeCache / Time.timeScale;
0 commit comments