mmorpg/Assets/HeroEditor4D/Common/Scripts/EditorScripts/StandaloneFilePicker.cs
2024-08-23 16:17:24 +05:30

87 lines
3.1 KiB
C#
Executable File

using System;
using System.Collections;
namespace Assets.HeroEditor4D.Common.Scripts.Common
{
public static class StandaloneFilePicker
{
#if UNITY_EDITOR
public static IEnumerator OpenFile(string title, string directory, string extension, Action<bool, string, byte[]> callback)
{
var path = UnityEditor.EditorUtility.OpenFilePanel(title, directory, extension.Replace(".", null));
if (!string.IsNullOrEmpty(path))
{
var bytes = System.IO.File.ReadAllBytes(path);
callback(true, path, bytes);
}
else
{
callback(false, null, null);
}
yield break;
}
public static IEnumerator SaveFile(string title, string directory, string defaultName, string extension, byte[] bytes, Action<bool, string> callback)
{
var path = UnityEditor.EditorUtility.SaveFilePanel(title, directory, defaultName, extension.Replace(".", null));
if (!string.IsNullOrEmpty(path))
{
System.IO.File.WriteAllBytes(path, bytes);
callback(true, path);
}
else
{
callback(false, null);
}
yield break;
}
#elif UNITY_STANDALONE_WIN || UNITY_WSA
public static IEnumerator OpenFile(string title, string directory, string extension, Action<bool, string, byte[]> callback)
{
throw new NotImplementedException("Import [Simple File Browser For Windows]: http://u3d.as/2QLg");
//yield return SimpleFileBrowserForWindows.WindowsFileBrowser.OpenFile(title, directory, "File", new[] { extension }, callback);
}
public static IEnumerator SaveFile(string title, string directory, string defaultName, string extension, byte[] bytes, Action<bool, string> callback)
{
throw new NotImplementedException("Import [Simple File Browser For Windows]: http://u3d.as/2QLg");
//yield return SimpleFileBrowserForWindows.WindowsFileBrowser.SaveFile(title, directory, defaultName, "Prefab", extension, bytes, callback);
}
#elif UNITY_WEBGL
public static IEnumerator OpenFile(string title, string directory, string extension, Action<bool, string, byte[]> callback)
{
throw new NotImplementedException("Import [Simple File Browser for WebGL]: http://u3d.as/2W52");
//SimpleFileBrowserForWebGL.WebFileBrowser.Upload((fileName, mime, bytes) => callback(true, null, bytes), extension);
}
public static IEnumerator SaveFile(string title, string directory, string defaultName, string extension, byte[] bytes, Action<bool, string> callback)
{
throw new NotImplementedException("Import [Simple File Browser for WebGL]: http://u3d.as/2W52");
//SimpleFileBrowserForWebGL.WebFileBrowser.Download(defaultName, bytes);
}
#else
public static IEnumerator OpenFile(string title, string directory, string extension, Action<bool, string, byte[]> callback)
{
throw new NotSupportedException();
}
public static IEnumerator SaveFile(string title, string directory, string defaultName, string extension, byte[] bytes, Action<bool, string> callback)
{
throw new NotSupportedException();
}
#endif
}
}