Lab

Lancer pngquant via un NativeProcess : C#, Unity & AS3/AIR

Après notre générateur de SpriteSheets à l'exécution pour Unity, c'est une bonne idée d'optimiser les fichiers pngs générés. pngquant est un utilitaire en ligne de commande et une bibliothèque de compression avec perte pour les images PNG.…

Après notre générateur de SpriteSheets à l’exécution pour Unity, c’est une bonne idée d’optimiser les fichiers pngs générés.

pngquant est un utilitaire en ligne de commande et une bibliothèque de compression avec perte pour les images PNG. La conversion réduit significativement la taille des fichiers (souvent jusqu’à 70%) et préserve la transparence alpha complète. Autrement dit, c’est un outil indispensable si vous travaillez avec beaucoup de pngs !

J’ai utilisé pngquant de nombreuses fois directement en ligne de commande, mais selon votre projet, vous pourriez avoir besoin de l’exécuter directement à l’intérieur de votre application ! Je n’ai pas trouvé d’exemple pour faire ça, et c’était bien plus difficile que je ne le pensais à cause de mon manque de connaissances en scripts batch et shell ! C’est parti :

Nous utilisons un fichier batch personnalisé (pour Windows) et un script shell (pour Mac OS X) pour lancer pngquant. Il prendra le chemin des pngs à compresser et les écrasera.

OS X :

#!/bin/sh
#!/usr/bin/env bash
#$ -N $2

DIR="$( cd "$( dirname "$0" )" && pwd )"

(cd "$DIR" && ./pngquant -f --ext .png "$1"/*.png)

Windows :

cd %~dp0
pngquant -f --ext .png "%~1"/*.png

Maintenant, un exemple C# pour appeler ces scripts, notez que ça marche très bien avec Unity aussi :

System.Diagnostics.Process process = new  System.Diagnostics.Process();

string exec = "";
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
	exec = "pngquant-windows.cmd";
else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)
	exec = "pngquant-osx";
else
	throw new Exception("Platform not supported");

process.StartInfo.FileName = Application.dataPath + "/../../" + exec;
process.StartInfo.Arguments = Application.dataPath + "/../../png-to-compress";

// if your path have blank spaces use:
//process.StartInfo.Arguments = "\"" + Application.dataPath + "/../../png compress\"";

process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;

process.Start();

Et enfin, un exemple en AS3 pour AIR :

var process:NativeProcess = new NativeProcess();

var startupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = File.applicationDirectory;

var exec:String = "";
if (Capabilities.os.indexOf("Windows") >= 0)
	exec = "pngquant-windows.cmd";
else if (Capabilities.os.indexOf("Mac") >= 0)
	exec = "pngquant-osx";
else
	throw new Error("doesn't work on " + Capabilities.os + " operating system");

file.nativePath = file.nativePath + "/../" + exec;
startupInfo.executable = file;

var processArgs:Vector.<String> = new Vector.<String>();
processArgs[0] = File.applicationDirectory.nativePath + "/../png-to-compress";
startupInfo.arguments = processArgs;

process.start(startupInfo);

Pensez à jeter un œil au dépôt git de PngQuantNativeProcess pour rester à jour !

AL
Aymeric Lamboley
Fondateur & Directeur technique