Skip to content

Commit

Permalink
fix: fix sample path
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai authored and mob-sakai committed Mar 2, 2020
1 parent 49d8f3f commit 57ee210
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
8 changes: 8 additions & 0 deletions Packages/UIParticle/Samples.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 35 additions & 17 deletions Packages/UIParticle/Scripts/Editor/UIParticleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57ee210

Please sign in to comment.