Skip to content

Commit 8236678

Browse files
Rework taking screenshots of the map on Android platform
1 parent c9cb849 commit 8236678

File tree

6 files changed

+41
-130
lines changed

6 files changed

+41
-130
lines changed

Assets/Project/Scenes/VisualiserScene.unity

Lines changed: 0 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Project/Scripts/Game Objects/MapScreenshotTaker.cs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
1+
using System;
2+
#if !UNITY_ANDROID
13
using System.IO;
2-
using System.Linq;
4+
#endif
35
using UnityEngine;
46
using UnityEngine.Events;
57

68
public 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
}

Assets/Project/Scripts/Game Objects/Platform Specific/Android/AndroidTakenMapScreenshotScanLauncher.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.

Assets/Project/Scripts/Game Objects/Platform Specific/Android/AndroidTakenMapScreenshotScanLauncher.cs.meta

Lines changed: 0 additions & 2 deletions
This file was deleted.

Packages/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"com.unity.toolchain.win-x86_64-linux-x86_64": "2.0.11",
1515
"com.unity.ugui": "2.0.0",
1616
"com.unity.visualscripting": "1.9.9",
17+
"com.yasirkula.nativegallery": "https://github.com/yasirkula/UnityNativeGallery.git",
1718
"com.unity.modules.accessibility": "1.0.0",
1819
"com.unity.modules.ai": "1.0.0",
1920
"com.unity.modules.androidjni": "1.0.0",

Packages/packages-lock.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@
401401
},
402402
"url": "https://packages.unity.com"
403403
},
404+
"com.yasirkula.nativegallery": {
405+
"version": "https://github.com/yasirkula/UnityNativeGallery.git",
406+
"depth": 0,
407+
"source": "git",
408+
"dependencies": {},
409+
"hash": "e05fc5a14fe582e4938d2597fc1401ca3fceafb0"
410+
},
404411
"com.unity.modules.accessibility": {
405412
"version": "1.0.0",
406413
"depth": 0,

0 commit comments

Comments
 (0)