Skip to content

Commit

Permalink
Merge pull request #2 from Netherlands3D/feature/gebruik-filebrowser-…
Browse files Browse the repository at this point in the history
…in-snapshot-en-periodicsnapshot-package

Feature/gebruik filebrowser in snapshot en periodicsnapshot package
  • Loading branch information
mvriel authored Oct 2, 2024
2 parents 65ef688 + 34e61f3 commit 67cb2b0
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 83 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2024-09-23

### Added

- Dependency filebrowser package
- Now using the filebrowser for saving files

## [1.0.0] - 2023-07-31

### Added
Expand Down
3 changes: 0 additions & 3 deletions Runtime/Plugins.meta

This file was deleted.

3 changes: 0 additions & 3 deletions Runtime/Plugins/Jslib.meta

This file was deleted.

20 changes: 0 additions & 20 deletions Runtime/Plugins/Jslib/DownloadSnapshot.jslib

This file was deleted.

3 changes: 0 additions & 3 deletions Runtime/Plugins/Jslib/DownloadSnapshot.jslib.meta

This file was deleted.

68 changes: 16 additions & 52 deletions Runtime/Scripts/Snapshots.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* permissions and limitations under the License.
*/

using SFB;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -30,8 +31,9 @@ namespace Netherlands3D.Snapshots
{
public class Snapshots : MonoBehaviour
{
//import this from filebrowser package which includes tghe download functions in its jslib
[DllImport("__Internal")]
private static extern void DownloadSnapshot(byte[] array, int byteLength, string fileName);
private static extern void DownloadFile(string gameObjectName, string methodName, string filename, byte[] byteArray, int byteArraySize);

[Tooltip("Optional source camera (Defaults to Camera.main)")]
[SerializeField] private Camera sourceCamera;
Expand All @@ -44,6 +46,8 @@ public class Snapshots : MonoBehaviour
[SerializeField] private SnapshotFileType fileType = SnapshotFileType.png;
[SerializeField] private LayerMask snapshotLayers;

public UnityEvent<string> DownloadSnapshotComplete = new();

public int Width
{
get => width;
Expand Down Expand Up @@ -85,28 +89,9 @@ private void Start()
{
if (!sourceCamera) sourceCamera = Camera.main;
}

[Obsolete("Use the Width property instead")]
public void SetImageWidth(string width) => Width = int.Parse(width);

[Obsolete("Use the Width property instead")]
public void SetImageWidth(int width) => Width = width;

[Obsolete("Use the Height property instead")]
public void SetImageHeight(string height) => Height = int.Parse(height);

[Obsolete("Use the Height property instead")]
public void SetImageHeight(int height) => Height = height;


public void UseViewSize(bool useViewSize) => this.useViewSize = useViewSize;

/// <summary>
/// Set the layermask for the snapshot camera ( if you want to exclude, include specific layers in the image )
/// </summary>
/// <param name="snapshotLayers">Include these layers in the snapshot</param>
[Obsolete("Use the SnapshotLayers property instead")]
public void SetLayerMask(LayerMask snapshotLayers) => SnapshotLayers = snapshotLayers;

public void TakeSnapshot()
{
var snapshotWidth = (useViewSize) ? Screen.width : width;
Expand All @@ -116,30 +101,16 @@ public void TakeSnapshot()

var path = DetermineSaveLocation();

//Use the jslib DownloadSnapshot to download the bytes as a file in WebGL/Browser
#if UNITY_WEBGL && !UNITY_EDITOR
DownloadSnapshot(bytes, bytes.Length, Path.GetFileName(path));
DownloadFile(gameObject.name, "OnSnapshotDownloadComplete", Path.GetFileName(path), bytes, bytes.Length);
#else
File.WriteAllBytes(path, bytes);
#endif
}

[Obsolete("Use Snapshot.ToImageBytes instead")]
public static byte[] SnapshotToImageBytes(
int imageWidth,
int imageHeight,
string fileType = "png",
Camera sourceCamera = null,
LayerMask snapshotLayers = default
) {
if (!sourceCamera) sourceCamera = Camera.main;

if (Enum.TryParse(fileType, out SnapshotFileType fileTypeAsEnum) == false)
{
fileTypeAsEnum = SnapshotFileType.png;
}

return Snapshot.ToImageBytes(imageWidth, imageHeight, sourceCamera, snapshotLayers, fileTypeAsEnum);
public void OnSnapshotDownloadComplete(string message)
{
DownloadSnapshotComplete.Invoke(message);
}

private string DetermineSaveLocation()
Expand All @@ -149,24 +120,17 @@ private string DetermineSaveLocation()
{
outputFileName = $"Snapshot_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}";
}
outputFileName = $"{outputFileName}.{FileType}";
string location = Application.persistentDataPath;

string location = Application.persistentDataPath
+ Path.DirectorySeparatorChar
#if UNITY_WEBGL && !UNITY_EDITOR
outputFileName = $"{outputFileName}.{FileType}";
location += Path.DirectorySeparatorChar
+ targetPath
+ Path.DirectorySeparatorChar
+ outputFileName;

#if UNITY_EDITOR
// Window for user to input desired path/name/filetype
location = EditorUtility.SaveFilePanel(
"Save texture as file",
"",
outputFileName,
fileType.ToString()
);
#else
StandaloneFileBrowser.SaveFilePanel("Save texture as file", "", outputFileName, FileType.ToString());
#endif

return location;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/eu.netherlands3d.snapshots.Runtime.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "eu.netherlands3d.snapshots.Runtime",
"rootNamespace": "Netherlands3D.Snapshots",
"references": [
"eu.netherlands3d.sun.Runtime"
"eu.netherlands3d.filebrowser.Runtime"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eu.netherlands3d.snapshots",
"displayName": "Netherlands3D - Snapshots",
"version": "1.0.0",
"version": "2.0.0",
"unity": "2022.2",
"description": "This package provides the means to do screen captures / snapshots for Netherlands3D",
"keywords": [
Expand All @@ -17,5 +17,8 @@
"author": {
"name": "Netherlands3D",
"url": "https://netherlands3d.eu"
},
"dependencies": {
"eu.netherlands3d.filebrowser": "2.1.0"
}
}

0 comments on commit 67cb2b0

Please sign in to comment.