| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using System.Runtime.InteropServices; |
| | 5 | |
|
| | 6 | | public class WebGLFileSaver |
| | 7 | | { |
| | 8 | |
|
| | 9 | | [DllImport("__Internal")] |
| | 10 | | private static extern void UNITY_SAVE(string content, string name, string MIMEType); |
| | 11 | |
|
| | 12 | | [DllImport ("__Internal")] |
| | 13 | | private static extern void UNITY_SAVE_BYTEARRAY(byte[] array, int byteLength, string name, string MIMEType); |
| | 14 | |
|
| | 15 | | [DllImport("__Internal")] |
| | 16 | | private static extern void init(); |
| | 17 | |
|
| | 18 | | [DllImport("__Internal")] |
| | 19 | | private static extern bool UNITY_IS_SUPPORTED(); |
| | 20 | |
|
| | 21 | | static bool hasinit = false; |
| | 22 | |
|
| | 23 | | public static void SaveFile(string content, string fileName, string MIMEType = "text/plain;charset=utf-8") |
| | 24 | | { |
| 0 | 25 | | if (!CheckSupportAndInit()) return; |
| | 26 | |
|
| 0 | 27 | | UNITY_SAVE (content, fileName, MIMEType); |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | public static void SaveFile(byte[] content, string fileName, string MIMEType = "text/plain;charset=utf-8") |
| | 31 | | { |
| 0 | 32 | | if (content == null) |
| | 33 | | { |
| 0 | 34 | | Debug.LogError("null parameter passed for content byte array"); |
| 0 | 35 | | return; |
| | 36 | | } |
| 0 | 37 | | if (!CheckSupportAndInit()) return; |
| | 38 | |
|
| 0 | 39 | | UNITY_SAVE_BYTEARRAY (content, content.Length, fileName, MIMEType); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | static bool CheckSupportAndInit() |
| | 43 | | { |
| 0 | 44 | | if (Application.isEditor) |
| | 45 | | { |
| 0 | 46 | | Debug.Log("Saving will not work in editor."); |
| 0 | 47 | | return false; |
| | 48 | | } |
| 0 | 49 | | if (Application.platform != RuntimePlatform.WebGLPlayer) |
| | 50 | | { |
| 0 | 51 | | Debug.Log("Saving must be on a WebGL build."); |
| 0 | 52 | | return false; |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | CheckInit(); |
| | 56 | |
|
| 0 | 57 | | if (!IsSavingSupported()) |
| | 58 | | { |
| 0 | 59 | | Debug.LogWarning("Saving is not supported on this device."); |
| 0 | 60 | | return false; |
| | 61 | | } |
| 0 | 62 | | return true; |
| | 63 | | } |
| | 64 | |
|
| | 65 | | static void CheckInit() |
| | 66 | | { |
| 0 | 67 | | if (!hasinit) |
| | 68 | | { |
| 0 | 69 | | init(); |
| 0 | 70 | | hasinit = true; |
| | 71 | | } |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public static bool IsSavingSupported() |
| | 75 | | { |
| 0 | 76 | | if (Application.isEditor) |
| | 77 | | { |
| 0 | 78 | | Debug.Log("Saving will not work in editor."); |
| 0 | 79 | | return false; |
| | 80 | | } |
| 0 | 81 | | if (Application.platform != RuntimePlatform.WebGLPlayer) |
| | 82 | | { |
| 0 | 83 | | Debug.Log("Saving must be on a WebGL build."); |
| 0 | 84 | | return false; |
| | 85 | | } |
| 0 | 86 | | CheckInit(); |
| 0 | 87 | | return UNITY_IS_SUPPORTED(); |
| | 88 | | } |
| | 89 | | } |