using UnityEngine; using UnityEditor.Experimental.AssetImporters; using System.IO; using System.Collections; using System; using System.Diagnostics; using System.ComponentModel; using System.Runtime.InteropServices; using SimpleJSON; using UnityEditor; using Debug = UnityEngine.Debug; using Assets.Config; using Assets.Editor.BedTimeCreatorMenu; namespace Assets.Editor.AssetPipeline.Scripts { //the first argument is the version, which will trigger a reimport of all spine documents if the version number doesn't match what they were imported with //the second argument is the file extension [ScriptedImporter(20, "spine")] public class SpineImporter : ScriptedImporter { private GameConfigData _gameConfigData; public override void OnImportAsset(AssetImportContext ctx) { _gameConfigData = GameConfigDataManager.GetGameData(); //todo determining whether to run the processing should be in a class method //var assetPipelineSettingsTxt = // new StreamReader("Assets/AssetPipeline/AssetPipelineSettings.json").ReadToEnd(); //var assetPipelineSettingsTxt = // new StreamReader(_gameConfigData.AssetPipelineSettings).ReadToEnd(); //var assetPipelineSettings = JSONDecoder.Decode(assetPipelineSettingsTxt); //if (!(bool)assetPipelineSettings["AssetPipeline"]["ProcessArtSrc"] || !(bool)assetPipelineSettings["AssetPipeline"]["ProcessSpine"]) //{ // Debug.Log("Spine import is turned off. Skipping " + ctx.assetPath); // return; //} if (!_gameConfigData.AssetPipelineSettings.ProcessArtSrc || !_gameConfigData.AssetPipelineSettings.ProcessSpine) { Debug.Log("Spine import is turned off. Skipping " + ctx.assetPath); return; } Debug.Log("SpineDoc: " + ctx.assetPath); // todo gettting the actor name and paths should be in a utility class //var actorsSrcPath = "Assets/ArtSrc/Actors"; var actorsSrcPath = _gameConfigData.ActorsSrcPath; //var actorsExportPath = "Assets/ArtExports/Actors"; var actorsExportPath = _gameConfigData.ActorsExportPath; var actorName = Path.GetFileNameWithoutExtension(ctx.assetPath).Split('_')[0]; var outputDir = Path.GetDirectoryName(ctx.assetPath); Debug.Log("Dir Name: " + Path.GetDirectoryName(ctx.assetPath)); //todo validation tests like this should be in their own class if (Path.GetDirectoryName(Path.GetDirectoryName(ctx.assetPath)) == actorsSrcPath) { Debug.Log("Spine doc is in ArtSrc/Actors, will output to ArtExports"); outputDir = actorsExportPath + "/" + actorName; } Debug.Log(string.Format("OutputDir: {0}", outputDir)); //todo both strings should be in configuration //todo getting the strings should be in a class method //var settingsFile = "Assets/ArtSrc/Actors/actors.export.json"; var settingsFile = _gameConfigData.SettingsSrcFile; //you must use \\ for the spine.com path or windows won't find it with / //var spinePath = "Tools\\Spine\\win\\Spine.com"; var spinePath = _gameConfigData.SpineWinPath; if (SystemInfo.operatingSystem.Contains("OS X")) { //spinePath = "Tools/Spine/osx/Spine.app/Contents/MacOS/Spine"; spinePath = _gameConfigData.SpineOSPath; } var spineArgs = string.Format("-i \"{0}\" -o \"{1}\" -e \"{2}\"", ctx.assetPath, outputDir, settingsFile); try { var spineProcess = new Process { StartInfo = { FileName = spinePath, Arguments = spineArgs, CreateNoWindow = true, UseShellExecute = true, ErrorDialog = true } }; Debug.Log("Exporting Spine command: " + spineProcess.StartInfo.FileName + " " + spineProcess.StartInfo.Arguments); spineProcess.Start(); spineProcess.WaitForExit(); } catch (Exception e) { //exceptions never seem to be thrown when the spineProcess doesn't work, i have no idea why, maybe this class needs to run as a dll? Console.WriteLine(e.Message); } //invokes Actors import method AssetDatabase.Refresh(); } } }