Files
ParticleEffectForUGUI/Scripts/Editor/UIParticleMenu.cs
semantic-release-bot eccb3ecb5e 3.0.0-preview.18
# [3.0.0-preview.18](https://github.com/mob-sakai/ParticleEffectForUGUI/compare/v3.0.0-preview.17...v3.0.0-preview.18) (2020-08-19)

### Bug Fixes

* AsmdefEx is no longer required ([50e749c](50e749c183))
* fix camera for baking mesh ([6395a4f](6395a4fa74))
* support .Net Framework 3.5 (again) ([23fcb06](23fcb06bf9))

### Features

* 3.0.0 updater ([f99292b](f99292b9a1))
* add menu to create UIParticle ([14f1c78](14f1c782ff))
* Combine baked meshes to improve performance ([633d058](633d058756))
* improve performance ([77c056a](77c056ad5f))
* optimization for vertices transforms and adding node for trails ([e070e8d](e070e8d5ee)), closes [#75](https://github.com/mob-sakai/ParticleEffectForUGUI/issues/75)
* option to ignoring canvas scaling ([fe85fed](fe85fed3c0))
* support 3d scaling ([42a84bc](42a84bc5e1))
* support custom simulation space ([a83e647](a83e64761c)), closes [#78](https://github.com/mob-sakai/ParticleEffectForUGUI/issues/78)
* support for particle systems including trail only ([f389d39](f389d39953)), closes [#61](https://github.com/mob-sakai/ParticleEffectForUGUI/issues/61)

### BREAKING CHANGES

* The bake-task has changed significantly. It may look different from previous versions.
2020-08-19 19:42:17 +00:00

109 lines
4.3 KiB
C#

using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
namespace Coffee.UIExtensions
{
public class UIParticleMenu
{
#if !UNITY_2019_1_OR_NEWER
static string GetPreviousSamplePath(string displayName, string sampleName)
{
string sampleRoot = string.Format("Assets/Samples/{0}", 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;
}
static void ImportSample(string packageName, string sampleName)
{
string jsonPath = string.Format("Packages/{0}/package.json", packageName);
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 = string.Format("{0}/Samples~/{1}", Path.GetDirectoryName(jsonPath), sampleName);
string dst = string.Format("Assets/Samples/{0}/{1}/{2}", displayName, version, sampleName);
string previous = GetPreviousSamplePath(displayName, sampleName);
if (!string.IsNullOrEmpty(previous))
{
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 + ".meta");
FileUtil.DeleteFileOrDirectory(previous);
string versionDir = Path.GetDirectoryName(previous);
if (Directory.GetFiles(versionDir, "*.meta", SearchOption.TopDirectoryOnly).Length == 0)
{
FileUtil.DeleteFileOrDirectory(versionDir + ".meta");
FileUtil.DeleteFileOrDirectory(versionDir);
}
}
Directory.CreateDirectory(Path.GetDirectoryName(dst));
FileUtil.CopyFileOrDirectory(src, dst);
AssetDatabase.ImportAsset(dst, ImportAssetOptions.ImportRecursive);
}
[MenuItem("Assets/Samples/Import UIParticle Sample")]
static void ImportSample()
{
ImportSample("com.coffee.ui-particle", "Demo");
}
#endif
[MenuItem("GameObject/UI/Particle System", false, 2019)]
public static void AddParticle(MenuCommand menuCommand)
{
// Create UI element.
EditorApplication.ExecuteMenuItem("GameObject/UI/Image");
var ui = Selection.activeGameObject;
// Create ParticleSystem.
EditorApplication.ExecuteMenuItem("GameObject/Effects/Particle System");
var ps = Selection.activeGameObject;
var transform = ps.transform;
var localRotation = transform.localRotation;
transform.SetParent(ui.transform.parent, true);
var pos = transform.localPosition;
pos.z = 0;
ps.transform.localPosition = pos;
ps.transform.localRotation = localRotation;
// Destroy UI elemant
Object.DestroyImmediate(ui);
// Assign default material.
var renderer = ps.GetComponent<ParticleSystemRenderer>();
var defaultMat = AssetDatabase.GetBuiltinExtraResource<Material>("Default-Particle.mat");
renderer.material = defaultMat ? defaultMat : renderer.material;
// Set to hierarchy mode
var particleSystem = ps.GetComponent<ParticleSystem>();
var main = particleSystem.main;
main.scalingMode = ParticleSystemScalingMode.Hierarchy;
// Add UIParticle.
var uiParticle = ps.AddComponent<UIParticle>();
uiParticle.ignoreCanvasScaler = true;
uiParticle.scale = 10;
}
}
}