public static List <int> GetExportLayers(PsdExportSettings settings, PsdFileInfo fileInfo) { List <int> exportLayers = new List <int>(); foreach (var keypair in settings.layerSettings) { PsdExportSettings.LayerSetting layerSetting = keypair.Value; // Don't export if not set to export if (!layerSetting.doExport) { continue; } // Don't export if group is off var groupInfo = fileInfo.GetGroupByLayerIndex(layerSetting.layerIndex); if (groupInfo != null && !groupInfo.visible) { continue; } exportLayers.Add(layerSetting.layerIndex); } return(exportLayers); }
private static Sprite SaveAsset(PsdExportSettings settings, Texture2D tex, int layer) { string assetPath = GetLayerFilename(settings, layer); if (assetPath == null) { return(null); } // Setup scaling variables float pixelsToUnits = settings.PixelsToUnitSize; // Apply global scaling, if any if (settings.ScaleBy > 0) { tex = ScaleTextureByMipmap(tex, settings.ScaleBy); } PsdExportSettings.LayerSetting layerSetting = settings.layerSettings[layer]; // Then scale by layer scale if (layerSetting.scaleBy != ScaleDown.Default) { // By default, scale by half int scaleLevel = 1; pixelsToUnits = Mathf.RoundToInt(settings.PixelsToUnitSize / 2f); // Setting is actually scale by quarter if (layerSetting.scaleBy == ScaleDown.Quarter) { scaleLevel = 2; pixelsToUnits = Mathf.RoundToInt(settings.PixelsToUnitSize / 4f); } // Apply scaling tex = ScaleTextureByMipmap(tex, scaleLevel); } byte[] buf = tex.EncodeToPNG(); File.WriteAllBytes(assetPath, buf); AssetDatabase.Refresh(); // Load the texture so we can change the type var textureObj = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D)); // Get the texture importer for the asset TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(assetPath); // Read out the texture import settings so import pivot point can be changed TextureImporterSettings importSetting = new TextureImporterSettings(); textureImporter.ReadTextureSettings(importSetting); // Set the pivot import setting importSetting.spriteAlignment = (int)settings.Pivot; // But if layer setting has a different pivot, set as new pivot if (settings.Pivot != layerSetting.pivot) { importSetting.spriteAlignment = (int)layerSetting.pivot; } // Pivot settings are the same but custom, set the vector //else if (settings.Pivot == SpriteAlignment.Custom) // importSetting.spritePivot = settings.PivotVector; importSetting.mipmapEnabled = false; importSetting.spritePixelsPerUnit = pixelsToUnits; // Set the rest of the texture settings textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.spritePackingTag = settings.PackingTag; // Write in the texture import settings textureImporter.SetTextureSettings(importSetting); EditorUtility.SetDirty(textureObj); AssetDatabase.WriteImportSettingsIfDirty(assetPath); AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); return((Sprite)AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite))); }