From 57ee21005e114fdf186b5db55ca2b77b7b7c441a Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Fri, 21 Feb 2020 21:14:35 +0900 Subject: [PATCH] fix: fix sample path --- Packages/UIParticle/Samples.meta | 8 +++ .../Scripts/Editor/UIParticleMenu.cs | 52 +++++++++++++------ 2 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 Packages/UIParticle/Samples.meta diff --git a/Packages/UIParticle/Samples.meta b/Packages/UIParticle/Samples.meta new file mode 100644 index 0000000..681586d --- /dev/null +++ b/Packages/UIParticle/Samples.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 004deb17291204f2fa672ff90092bb81 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/UIParticle/Scripts/Editor/UIParticleMenu.cs b/Packages/UIParticle/Scripts/Editor/UIParticleMenu.cs index 286614c..93d2880 100644 --- a/Packages/UIParticle/Scripts/Editor/UIParticleMenu.cs +++ b/Packages/UIParticle/Scripts/Editor/UIParticleMenu.cs @@ -2,41 +2,59 @@ #if !UNITY_2019_1_OR_NEWER using System.IO; using System.Text.RegularExpressions; -using System.Collections; -using System.Collections.Generic; -using UnityEngine; using UnityEditor; namespace Coffee.UIExtensions { public class UIParticleMenu { - [MenuItem("Assets/Samples/Import UIParticle Sample")] - static void ImportSample() + static string GetPreviousSamplePath(string displayName, string sampleName) { - const string sampleGuid = "dc0fe9e7fe61947fab1522ab29e2fc88"; - const string jsonGuid = "823dc693d087a4b559c7e1547274cc7d"; - const string SAMPLE_NAME = "Demo"; + string sampleRoot = $"Assets/Samples/{displayName}"; + var sampleRootInfo = new DirectoryInfo(sampleRoot); + if (!sampleRootInfo.Exists) return null; + + foreach (var versionDir in sampleRootInfo.GetDirectories()) + { + var samplePath = Path.Combine(versionDir.ToString(), sampleName); + if (Directory.Exists(samplePath)) + return samplePath; + } + return null; + } + - string jsonPath = AssetDatabase.GUIDToAssetPath(jsonGuid); + static void ImportSample(string packageName, string sampleName) + { + string jsonPath = $"Packages/{packageName}/package.json"; string json = File.ReadAllText(jsonPath); string version = Regex.Match(json, "\"version\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value; string displayName = Regex.Match(json, "\"displayName\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value; - string src = Path.GetDirectoryName(jsonPath) + "/Samples~/" + SAMPLE_NAME; - string dst = string.Format("Assets/Samples/{0}/{1}/{2}",displayName, version, SAMPLE_NAME); + string src = $"{Path.GetDirectoryName(jsonPath)}/Samples~/{sampleName}"; + string dst = $"Assets/Samples/{displayName}/{version}/{sampleName}"; + string previous = GetPreviousSamplePath(displayName, sampleName); - // Remove old samples - string samplePath = AssetDatabase.GUIDToAssetPath(sampleGuid); - if (samplePath.StartsWith("Assets/") && FileUtil.PathExists(samplePath)) + if (!string.IsNullOrEmpty(previous)) { - FileUtil.DeleteFileOrDirectory(samplePath); - FileUtil.DeleteFileOrDirectory(samplePath + ".meta"); + string msg = "A different version of the sample is already imported at\n\n" + + previous + + "\n\nIt will be deleted when you update. Are you sure you want to continue?"; + if (!EditorUtility.DisplayDialog("Sample Importer", msg, "OK", "Cancel")) + return; + + FileUtil.DeleteFileOrDirectory(previous); + FileUtil.DeleteFileOrDirectory(previous + ".meta"); } FileUtil.CopyDirectoryRecursive(src, dst); - FileUtil.CopyFileOrDirectory(src + ".meta", dst + ".meta"); AssetDatabase.ImportAsset(dst, ImportAssetOptions.ImportRecursive); } + + [MenuItem("Assets/Samples/Import UIParticle Sample")] + static void ImportSample() + { + ImportSample("com.coffee.ui-particle", "Demo"); + } } } #endif