Skip to content

Commit

Permalink
Utilize scriptable importer for better workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
WeAthFoLD committed Apr 27, 2021
1 parent 88a5f34 commit e67bbfd
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 15 deletions.
27 changes: 21 additions & 6 deletions Assets/Plugins/MetaSprite/Editor/ASEImporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
using System.Collections;
#if UNITY_2020_2_OR_NEWER
#define SCRIPTABLE_IMPORTERS
using UnityEditor.AssetImporters;
#elif UNITY_2019_4_OR_NEWER
#define SCRIPTABLE_IMPORTERS
using UnityEditor.Experimental.AssetImporters;
#endif
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
Expand Down Expand Up @@ -35,7 +42,18 @@ public class ImportContext {

}

public static class ASEImporter {
#if SCRIPTABLE_IMPORTERS
[ScriptedImporter(1, new[] { "ase", "aseprite" })]
public class ASEImporter : ScriptedImporter {

public ImportSettings settings;

public override void OnImportAsset(AssetImportContext ctx) {
}
}
#endif

public static class ASEImportProcess {

static readonly Dictionary<string, MetaLayerProcessor> layerProcessors = new Dictionary<string, MetaLayerProcessor>();

Expand Down Expand Up @@ -88,10 +106,7 @@ struct LayerAndProcessor {
}


public static void Import(DefaultAsset defaultAsset, ImportSettings settings) {

var path = AssetDatabase.GetAssetPath(defaultAsset);

public static void Import(string path, ImportSettings settings) {
var context = new ImportContext {
// file = file,
settings = settings,
Expand Down
13 changes: 9 additions & 4 deletions Assets/Plugins/MetaSprite/Editor/ImportMenu.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections;
#if UNITY_2019_4_OR_NEWER
#define SCRIPTABLE_IMPORTERS
#endif
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Expand All @@ -14,11 +17,12 @@

namespace MetaSprite {

#if !SCRIPTABLE_IMPORTERS
public static class ImportMenu {

[MenuItem("Assets/Aseprite/Import", priority = 60)]
static void MenuClicked() {
ASEImporter.Startup();
ASEImportProcess.Startup();
DoImport(GetSelectedAseprites());
}

Expand Down Expand Up @@ -75,7 +79,7 @@ static void DoImport(DefaultAsset[] assets) {
foreach (var asset in assets) {
var reference = ImportUtil.LoadImportSettings(asset);
if (reference)
ASEImporter.Import(asset, reference.settings);
ASEImportProcess.Import(AssetDatabase.GetAssetPath(asset), reference.settings);
else
Debug.LogWarning("File " + asset.name + " has empty import settings, it is ignored.");
}
Expand All @@ -99,7 +103,7 @@ static void CreateSettingsThenImport(DefaultAsset[] assets) {
var paths = assets.Select(it => ImportUtil.GetImportSettingsPath(it)).ToList();

window._Init(paths, settings => { foreach (var asset in assets) {
ASEImporter.Import(asset, settings);
ASEImportProcess.Import(AssetDatabase.GetAssetPath(asset), settings);
} });

window.ShowPopup();
Expand Down Expand Up @@ -171,5 +175,6 @@ static bool CenteredButton(string content) {
}

}
#endif

}
34 changes: 34 additions & 0 deletions Assets/Plugins/MetaSprite/Editor/Internal/ASEImporterEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if UNITY_2020_2_OR_NEWER
#define SCRIPTABLE_IMPORTERS
using UnityEditor.AssetImporters;
#elif UNITY_2019_4_OR_NEWER
#define SCRIPTABLE_IMPORTERS
using UnityEditor.Experimental.AssetImporters;
#endif
using UnityEditor;
using UnityEngine;

#if SCRIPTABLE_IMPORTERS
namespace MetaSprite.Internal {

[CustomEditor(typeof(ASEImporter))]
public class ASEImporterEditor : ScriptedImporterEditor {
public override void OnInspectorGUI() {
// EditorGUILayout.LabelField("Hello?");
base.OnInspectorGUI();

var importer = (ASEImporter) target;
GUI.enabled = importer.settings;
EditorGUILayout.BeginHorizontal();

GUILayout.FlexibleSpace();
if (GUILayout.Button("Import", GUILayout.Width(50))) {
ASEImportProcess.Startup();
ASEImportProcess.Import(importer.assetPath, importer.settings);
}

EditorGUILayout.EndHorizontal();
}
}
}
#endif

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

Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
using System;
#if UNITY_2020_2_OR_NEWER
#define SCRIPTABLE_IMPORTERS
using UnityEditor.AssetImporters;
#elif UNITY_2019_4_OR_NEWER
#define SCRIPTABLE_IMPORTERS
using UnityEditor.Experimental.AssetImporters;
#endif
using System;
using System.Linq;
using System.Collections.Generic;
using MetaSprite.Internal;
using UnityEditor;
using UnityEngine;

namespace MetaSprite.Internal {

Expand All @@ -24,14 +31,32 @@ private static void OnPostprocessAllAssets(string[] importedAssets, string[] del

private static void CompleteAutoImports() {
EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.update, _importDelegate);
ASEImporter.Startup();
ASEImportProcess.Startup();

foreach (var path in _autoImports) {
bool refreshed = false;


#if SCRIPTABLE_IMPORTERS
// Scriptable importer pipeline
var importer = (ASEImporter) AssetImporter.GetAtPath(path);
if (importer && importer.settings) {
ASEImportProcess.Import(path, importer.settings);
refreshed = true;
}
#else
// Legacy pipeline
var asset = AssetDatabase.LoadAssetAtPath<DefaultAsset>(path);
var reference = ImportUtil.LoadImportSettings(asset);
if (reference && reference.settings.automaticReimport)
{
ASEImporter.Import(asset, reference.settings);
ASEImportProcess.Import(path, reference.settings);
refreshed = true;
}
#endif

if (refreshed)
Debug.Log("Auto reimport ase success: " + path);
}
_autoImports.Clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void Process(ImportContext context, Layer layer) {
var sprites = AtlasGenerator.GenerateAtlas(context, layers,
Path.GetDirectoryName(context.atlasPath) + "/" +
context.fileNameNoExt + "_" + subImageName + ".png");
ASEImporter.GenerateClipImageLayer(context, targetChildObject, sprites);
ASEImportProcess.GenerateClipImageLayer(context, targetChildObject, sprites);
}

}
Expand Down

0 comments on commit e67bbfd

Please sign in to comment.