private bool LoadImage() { settings = new PsdExportSettings(image); bool valid = (settings.Psd != null); if (valid) { // Parse the layer info fileInfo = new PsdFileInfo(settings.Psd); } return(valid); }
public static Sprite CreateSprite(PsdExportSettings settings, Layer layer) { Texture2D tex = CreateTexture(layer); if (tex == null) { return(null); } Sprite sprite = SaveAsset(settings, tex, "_" + layer.Name); Object.DestroyImmediate(tex); return(sprite); }
public static void Export(PsdExportSettings settings, PsdFileInfo fileInfo) { for (int i = 0; i < settings.Psd.Layers.Count; i++) { var groupInfo = fileInfo.GetGroupByLayerIndex(i); if (groupInfo != null && !groupInfo.visible) { continue; } if (!fileInfo.LayerVisibility[i]) { continue; } var layer = settings.Psd.Layers[i]; CreateSprite(settings, layer); } settings.SaveMetaData(); }
private static Sprite SaveAsset(PsdExportSettings settings, Texture2D tex, string suffix) { string assetPath = AssetDatabase.GetAssetPath(settings.Image); string path = Path.Combine(Path.GetDirectoryName(assetPath), Path.GetFileNameWithoutExtension(assetPath) + suffix + ".png"); if (settings.ScaleBy > 0) { int width = Mathf.RoundToInt(tex.width / 2); int height = Mathf.RoundToInt(tex.height / 2); int mipLevel = 1; if (settings.ScaleBy == 2) { width = Mathf.RoundToInt(tex.width / 4); height = Mathf.RoundToInt(tex.height / 4); mipLevel = 2; } Texture2D resized = new Texture2D(width, height); resized.SetPixels32(tex.GetPixels32(mipLevel)); resized.Apply(); tex = resized; } byte[] buf = tex.EncodeToPNG(); File.WriteAllBytes(path, buf); AssetDatabase.Refresh(); // Load the texture so we can change the type var textureObj = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)); TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(path); textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.spritePivot = settings.PivotVector; textureImporter.spritePixelsToUnits = settings.PixelsToUnitSize; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); EditorUtility.SetDirty(textureObj); return((Sprite)AssetDatabase.LoadAssetAtPath(path, typeof(Sprite))); }