1+ using System ;
2+ #if ! UNITY_ANDROID
13using System . IO ;
2- using System . Linq ;
4+ #endif
35using UnityEngine ;
46using UnityEngine . Events ;
57
68public class MapScreenshotTaker : MonoBehaviour
79{
810 public UnityEvent < bool > takingScreenshotStateWasChangedEvent ;
9- public UnityEvent < string > screenshotWasTakenEvent ;
1011
1112 private MapScreenshotSceneCamera mapScreenshotSceneCamera ;
1213
1314 private static readonly string SCREENSHOT_NAME = "Screenshot" ;
1415 private static readonly string SCREENSHOT_EXTENSION = ".png" ;
15- #if UNITY_ANDROID
16- private static readonly string ANDROID_SCREENSHOTS_FOLDER_NAME = "Screenshots" ;
17- #endif
16+ private static readonly string SCREENSHOTS_FOLDER_NAME = "Screenshots" ;
1817 private static readonly int RENDER_TEXTURE_DEPTH = 24 ;
1918 private static readonly TextureFormat RENDER_TEXTURE_FORMAT = TextureFormat . RGB24 ;
2019
2120 public void TakeMapScreenshot ( )
2221 {
23- var directoryPathFolder = GetDirectoryPathFolder ( ) ;
24- var directoryPath = $ "{ directoryPathFolder } /{ GetDirectoryName ( ) } ";
25-
2622 takingScreenshotStateWasChangedEvent ? . Invoke ( true ) ;
27- EnsureExistanceOfDirectory ( directoryPath ) ;
28- TakeScreenshotInDirectory ( directoryPath , directoryPathFolder ) ;
23+ InitiateTakingScreenshot ( ) ;
2924 takingScreenshotStateWasChangedEvent ? . Invoke ( false ) ;
3025 }
3126
@@ -34,23 +29,36 @@ private void Awake()
3429 mapScreenshotSceneCamera = ObjectMethods . FindComponentOfType < MapScreenshotSceneCamera > ( ) ;
3530 }
3631
32+ private void InitiateTakingScreenshot ( )
33+ {
34+ #if ! UNITY_ANDROID
35+ EnsureExistanceOfDirectory ( GetDirectoryPath ( ) ) ;
36+ #endif
37+
38+ TakeScreenshotInDirectory ( ) ;
39+ }
40+
41+ #if ! UNITY_ANDROID
3742 private void EnsureExistanceOfDirectory ( string path )
3843 {
3944 if ( ! Directory . Exists ( path ) )
4045 {
4146 Directory . CreateDirectory ( path ) ;
4247 }
4348 }
49+ #endif
4450
45- private void TakeScreenshotInDirectory ( string path , string pathFolder )
51+ private void TakeScreenshotInDirectory ( )
4652 {
47- var numberOfExistingScreenshots = new DirectoryInfo ( path ) . GetFiles ( ) . Where ( file => file . Extension . Equals ( SCREENSHOT_EXTENSION ) ) . Count ( ) ;
48- var screenshotNumber = numberOfExistingScreenshots + 1 ;
49- var screenshotPath = $ "{ path } /{ SCREENSHOT_NAME } { screenshotNumber } { SCREENSHOT_EXTENSION } ";
53+ var directoryPath = GetDirectoryPath ( ) ;
54+ var screenshotPath = GetScreenshotPath ( ) ;
5055 var screenshotTexture = GetTextureForScreenshot ( ) ;
5156
52- File . WriteAllBytes ( screenshotPath , screenshotTexture . EncodeToPNG ( ) ) ;
53- screenshotWasTakenEvent ? . Invoke ( pathFolder ) ;
57+ #if ! UNITY_ANDROID
58+ File . WriteAllBytes ( $ "{ directoryPath } /{ screenshotPath } ", screenshotTexture . EncodeToPNG ( ) ) ;
59+ #else
60+ NativeGallery . SaveImageToGallery ( screenshotTexture , directoryPath , screenshotPath ) ;
61+ #endif
5462 }
5563
5664 private Texture2D GetTextureForScreenshot ( )
@@ -88,24 +96,22 @@ private void SetRenderTextureResult(RenderTexture renderTexture)
8896
8997 private string GetDirectoryPathFolder ( )
9098 {
91- #if UNITY_ANDROID
92- using var environment = new AndroidJavaClass ( AndroidJNINames . OS_ENVIRONMENT_CLASS_NAME ) ;
93- var directoryDCIM = environment . GetStatic < string > ( AndroidJNINames . DIRECTORY_DCIM_FIELD_NAME ) ;
94- using var externalStoragePublicDirectory = environment . CallStatic < AndroidJavaObject > ( AndroidJNINames . GET_EXTERNAL_STORAGE_PUBLIC_DIRECTORY_METHOD_NAME , directoryDCIM ) ;
95- var absolutePath = externalStoragePublicDirectory . Call < string > ( AndroidJNINames . GET_ABSOLUTE_PATH_METHOD_NAME ) ;
96-
97- return Path . Combine ( absolutePath , ANDROID_SCREENSHOTS_FOLDER_NAME ) ;
98- #else
99+ #if ! UNITY_ANDROID
99100 return Path . GetDirectoryName ( Application . dataPath ) ;
101+ #else
102+ return SCREENSHOTS_FOLDER_NAME ;
100103#endif
101104 }
102105
103106 private string GetDirectoryName ( )
104107 {
105- #if UNITY_ANDROID
106- return "AStar Visualiser" ;
108+ #if ! UNITY_ANDROID
109+ return SCREENSHOTS_FOLDER_NAME ;
107110#else
108- return "Screenshots " ;
111+ return "AStar Visualiser " ;
109112#endif
110113 }
114+
115+ private string GetDirectoryPath ( ) => $ "{ GetDirectoryPathFolder ( ) } /{ GetDirectoryName ( ) } ";
116+ private string GetScreenshotPath ( ) => $ "{ SCREENSHOT_NAME } _{ DateTime . Now : yyyyMMdd_HHmmss} _AStarVisualiser{ SCREENSHOT_EXTENSION } ";
111117}
0 commit comments