ImportSampleMenu.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #if !UNITY_2019_1_OR_NEWER
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using UnityEditor;
  6. namespace Coffee.UIExtensions
  7. {
  8. internal static class ImportSampleMenu_UIParticle
  9. {
  10. private const string k_DisplayName = "UI Particle";
  11. private const string k_JsonGuid = "823dc693d087a4b559c7e1547274cc7d";
  12. [MenuItem("Assets/Samples/" + k_DisplayName + "/Demo")]
  13. private static void ImportSample()
  14. {
  15. ImportSample(k_JsonGuid, "Demo");
  16. }
  17. [MenuItem("Assets/Samples/" + k_DisplayName + "/Cartoon FX & War FX Demo")]
  18. private static void ImportSample_CFX()
  19. {
  20. ImportSample(k_JsonGuid, "Cartoon FX & War FX Demo");
  21. }
  22. private static void ImportSample(string jsonGuid, string sampleName)
  23. {
  24. var jsonPath = AssetDatabase.GUIDToAssetPath(jsonGuid);
  25. var packageRoot = Path.GetDirectoryName(jsonPath).Replace('\\', '/');
  26. var json = File.ReadAllText(jsonPath);
  27. var version = Regex.Match(json, "\"version\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value;
  28. var src = string.Format("{0}/Samples~/{1}", packageRoot, sampleName);
  29. var dst = string.Format("Assets/Samples/{0}/{1}/{2}", k_DisplayName, version, sampleName);
  30. var previousPath = GetPreviousSamplePath(k_DisplayName, sampleName);
  31. // Remove the previous sample directory.
  32. if (!string.IsNullOrEmpty(previousPath))
  33. {
  34. var msg = "A different version of the sample is already imported at\n\n"
  35. + previousPath
  36. + "\n\nIt will be deleted when you update. Are you sure you want to continue?";
  37. if (!EditorUtility.DisplayDialog("Sample Importer", msg, "OK", "Cancel"))
  38. return;
  39. FileUtil.DeleteFileOrDirectory(previousPath);
  40. var metaFile = previousPath + ".meta";
  41. if (File.Exists(metaFile))
  42. FileUtil.DeleteFileOrDirectory(metaFile);
  43. }
  44. if (!Directory.Exists(dst))
  45. FileUtil.DeleteFileOrDirectory(dst);
  46. var dstDir = Path.GetDirectoryName(dst);
  47. if (!Directory.Exists(dstDir))
  48. Directory.CreateDirectory(dstDir);
  49. if (Directory.Exists(src))
  50. FileUtil.CopyFileOrDirectory(src, dst);
  51. else
  52. throw new DirectoryNotFoundException(src);
  53. AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive);
  54. }
  55. private static string GetPreviousSamplePath(string displayName, string sampleName)
  56. {
  57. var sampleRoot = string.Format("Assets/Samples/{0}", displayName);
  58. var sampleRootInfo = new DirectoryInfo(sampleRoot);
  59. if (!sampleRootInfo.Exists) return null;
  60. return sampleRootInfo.GetDirectories()
  61. .Select(versionDir => Path.Combine(versionDir.ToString(), sampleName))
  62. .FirstOrDefault(Directory.Exists);
  63. }
  64. }
  65. }
  66. #endif