2020-09-28 20:35:40 +08:00
|
|
|
#if !UNITY_2019_1_OR_NEWER
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using UnityEditor;
|
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
namespace Coffee.UIExtensions
|
2020-09-28 20:35:40 +08:00
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
internal static class ImportSampleMenu_UIParticle
|
2020-09-28 20:35:40 +08:00
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
private const string k_DisplayName = "UI Particle";
|
|
|
|
private const string k_JsonGuid = "823dc693d087a4b559c7e1547274cc7d";
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
[MenuItem("Assets/Samples/" + k_DisplayName + "/Demo")]
|
|
|
|
private static void ImportSample()
|
|
|
|
{
|
|
|
|
ImportSample(k_JsonGuid, "Demo");
|
|
|
|
}
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
[MenuItem("Assets/Samples/" + k_DisplayName + "/Cartoon FX & War FX Demo")]
|
|
|
|
private static void ImportSample_CFX()
|
2020-09-28 20:35:40 +08:00
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
ImportSample(k_JsonGuid, "Cartoon FX & War FX Demo");
|
|
|
|
}
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2022-06-14 12:39:13 +08:00
|
|
|
[MenuItem("Assets/Samples/" + k_DisplayName + "/Cartoon FX & War FX Demo")]
|
|
|
|
private static void ImportSample_PerformanceDemo()
|
|
|
|
{
|
|
|
|
ImportSample(k_JsonGuid, "Performance Demo");
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
private static void ImportSample(string jsonGuid, string sampleName)
|
|
|
|
{
|
|
|
|
var jsonPath = AssetDatabase.GUIDToAssetPath(jsonGuid);
|
2023-08-17 08:43:02 +08:00
|
|
|
if (string.IsNullOrEmpty(jsonPath)) return;
|
|
|
|
|
|
|
|
var jsonDir = Path.GetDirectoryName(jsonPath);
|
|
|
|
if (string.IsNullOrEmpty(jsonDir)) return;
|
|
|
|
|
|
|
|
var packageRoot = jsonDir.Replace('\\', '/');
|
2020-10-01 15:15:57 +08:00
|
|
|
var json = File.ReadAllText(jsonPath);
|
|
|
|
var version = Regex.Match(json, "\"version\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value;
|
2023-08-17 08:43:02 +08:00
|
|
|
var src = $"{packageRoot}/Samples~/{sampleName}";
|
|
|
|
var dst = $"Assets/Samples/{k_DisplayName}/{version}/{sampleName}";
|
2020-10-01 15:15:57 +08:00
|
|
|
var previousPath = GetPreviousSamplePath(k_DisplayName, sampleName);
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
// Remove the previous sample directory.
|
|
|
|
if (!string.IsNullOrEmpty(previousPath))
|
|
|
|
{
|
|
|
|
var msg = "A different version of the sample is already imported at\n\n"
|
|
|
|
+ previousPath
|
|
|
|
+ "\n\nIt will be deleted when you update. Are you sure you want to continue?";
|
|
|
|
if (!EditorUtility.DisplayDialog("Sample Importer", msg, "OK", "Cancel"))
|
2023-08-17 08:43:02 +08:00
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
return;
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
FileUtil.DeleteFileOrDirectory(previousPath);
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
var metaFile = previousPath + ".meta";
|
|
|
|
if (File.Exists(metaFile))
|
2023-08-17 08:43:02 +08:00
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
FileUtil.DeleteFileOrDirectory(metaFile);
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|
2020-10-01 15:15:57 +08:00
|
|
|
}
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
if (!Directory.Exists(dst))
|
2023-08-17 08:43:02 +08:00
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
FileUtil.DeleteFileOrDirectory(dst);
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
var dstDir = Path.GetDirectoryName(dst);
|
2023-08-17 08:43:02 +08:00
|
|
|
if (!string.IsNullOrEmpty(dstDir) && !Directory.Exists(dstDir))
|
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
Directory.CreateDirectory(dstDir);
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
if (Directory.Exists(src))
|
2023-08-17 08:43:02 +08:00
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
FileUtil.CopyFileOrDirectory(src, dst);
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|
2020-10-01 15:15:57 +08:00
|
|
|
else
|
2023-08-17 08:43:02 +08:00
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
throw new DirectoryNotFoundException(src);
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|
2020-10-01 15:15:57 +08:00
|
|
|
|
|
|
|
AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive);
|
|
|
|
}
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-10-01 15:15:57 +08:00
|
|
|
private static string GetPreviousSamplePath(string displayName, string sampleName)
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
var sampleRoot = $"Assets/Samples/{displayName}";
|
2020-10-01 15:15:57 +08:00
|
|
|
var sampleRootInfo = new DirectoryInfo(sampleRoot);
|
|
|
|
if (!sampleRootInfo.Exists) return null;
|
|
|
|
|
|
|
|
return sampleRootInfo.GetDirectories()
|
|
|
|
.Select(versionDir => Path.Combine(versionDir.ToString(), sampleName))
|
|
|
|
.FirstOrDefault(Directory.Exists);
|
|
|
|
}
|
2020-09-28 20:35:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|