public static void ImportLayersUI(Object psdFile, ImportUserData importSettings, List <int[]> layerIndices) { int total = layerIndices.Count; Action <int, ImportLayerData> layerCallback = (current, layer) => EditorCoroutineRunner.UpdateUI($"[{current}/{total}] Layer: {layer.name}", (float)current / total); IEnumerator importCoroutine = ImportCoroutine(psdFile, importSettings.TargetDirectory, importSettings, layerIndices, layerCallback); EditorCoroutineRunner.StartCoroutineWithUI(importCoroutine, "Importing PSD Layers", true); }
public static void ImportLayersUI(Object psdFile, ImportUserData importSettings, List <int[]> layerIndices) { int total = layerIndices.Count; EditorCoroutineRunner.StartCoroutineWithUI( ImportCoroutine(psdFile, importSettings, layerIndices, true, layerCallback: (current, layer) => { string text = string.Format("[{0}/{1}] Layer: {2}", current, total, layer.name); float percent = (float)current / total; EditorCoroutineRunner.UpdateUI(text, percent); } ), "Importing PSD Layers", true ); }
private static IEnumerator ImportCoroutine(Object psdFile, string targetDirectory, ImportUserData importSettings, List <int[]> layerIndices, Action <int, ImportLayerData> layerCallback = null, Action <List <Sprite> > completeCallback = null) { string filepath = GetPsdFilepath(psdFile); if (string.IsNullOrEmpty(filepath)) { completeCallback?.Invoke(null); yield break; } if (string.IsNullOrEmpty(targetDirectory)) { targetDirectory = filepath.Substring(0, filepath.LastIndexOf("/")); } // Get the texture importer for the PSD TextureImporter psdUnitySettings = (TextureImporter)AssetImporter.GetAtPath(filepath); TextureImporterSettings psdUnityImport = new TextureImporterSettings(); psdUnitySettings.ReadTextureSettings(psdUnityImport); int importCurrent = 0; var sprites = new List <Sprite>(); using (PsdDocument psd = PsdDocument.Create(filepath)) { var layersSettings = new List <ImportLayerData>(); foreach (int[] layerIdx in layerIndices) { ImportLayerData layerSettings = importSettings.GetLayerData(layerIdx); if (layerSettings == null) { continue; } layerCallback?.Invoke(importCurrent, layerSettings); layersSettings.Add(layerSettings); } const float totalSteps = 4.0f; float currentStep = 0.0f; EditorCoroutineRunner.UpdateUI($"Getting psd layers", ++currentStep / totalSteps); yield return(null); var psdLayers = GetPsdLayers(layersSettings, psd); EditorCoroutineRunner.UpdateUI($"CreateTextures2D", ++currentStep / totalSteps); yield return(null); var textures = CreateTextures2D(psdLayers, psd, importSettings, psdUnityImport); EditorCoroutineRunner.UpdateUI($"GetTextureWithPathData", ++currentStep / totalSteps); yield return(null); var texturesData = GetTextureWithPathData(textures, psdLayers.Select(x => x.psdLayer).ToArray(), importSettings.fileNaming, importSettings.groupMode, targetDirectory); EditorCoroutineRunner.UpdateUI($"CreateSpriteAssets", ++currentStep / totalSteps); yield return(null); sprites.AddRange(CreateSpriteAssets(texturesData, layersSettings, psdUnityImport, importSettings.PackingTag)); } completeCallback?.Invoke(sprites); }