{"id":607,"date":"2017-02-03T16:19:17","date_gmt":"2017-02-03T15:19:17","guid":{"rendered":"http:\/\/davikingcode.com\/blog\/?p=607"},"modified":"2017-02-03T16:21:02","modified_gmt":"2017-02-03T15:21:02","slug":"run-pngquant-via-a-nativeprocess-c-unity-as3air","status":"publish","type":"post","link":"https:\/\/davikingcode.com\/blog\/run-pngquant-via-a-nativeprocess-c-unity-as3air\/","title":{"rendered":"Run pngquant via a NativeProcess: C#, Unity &#038; AS3\/AIR"},"content":{"rendered":"<p>After our <a href=\"http:\/\/davikingcode.com\/blog\/unity-generate-spritesheets-at-runtime\/\" target=\"_blank\">Unity runtime SpriteSheets generator<\/a>, it is a good idea to optimize the generated pngs files.<\/p>\n<p><a href=\"http:\/\/pngquant.org\/\" target=\"_blank\">pngquant<\/a> is a command-line utility and a library for lossy compression of PNG images. The conversion reduces file sizes significantly (often as much as 70%) and preserves full alpha transparency. In other words this is a must have tool if you&#8217;re working with many pngs!<\/p>\n<p>I used many times pngquant directly from the command line, but depending your project, you might need to run it directly inside your application! I didn&#8217;t find example for doing this, and it was way harder than I thought due to my lack of knowledge with batch and shell scripts! So here we go:<\/p>\n<p>We use custom batch file (for Windows) and shell script (for Mac OS X) for launching pngquant. It will take the path to pngs to compress and overwrite them.<\/p>\n<p><strong>OS X<\/strong>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/sh\r\n#!\/usr\/bin\/env bash\r\n#$ -N $2\r\n\r\nDIR=&quot;$( cd &quot;$( dirname &quot;$0&quot; )&quot; &amp;&amp; pwd )&quot;\r\n\r\n(cd &quot;$DIR&quot; &amp;&amp; .\/pngquant -f --ext .png &quot;$1&quot;\/*.png)\r\n<\/pre>\n<p><strong>Windows<\/strong>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd %~dp0\r\npngquant -f --ext .png &quot;%~1&quot;\/*.png\r\n<\/pre>\n<p>Now a C# example for calling thoses scripts, note it works fine with Unity too:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSystem.Diagnostics.Process process = new  System.Diagnostics.Process();\r\n\r\nstring exec = &quot;&quot;;\r\nif (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)\r\n\texec = &quot;pngquant-windows.cmd&quot;;\r\nelse if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)\r\n\texec = &quot;pngquant-osx&quot;;\r\nelse\r\n\tthrow new Exception(&quot;Platform not supported&quot;);\r\n\r\nprocess.StartInfo.FileName = Application.dataPath + &quot;\/..\/..\/&quot; + exec;\r\nprocess.StartInfo.Arguments = Application.dataPath + &quot;\/..\/..\/png-to-compress&quot;;\r\n\r\n\/\/ if your path have blank spaces use:\r\n\/\/process.StartInfo.Arguments = &quot;\\&quot;&quot; + Application.dataPath + &quot;\/..\/..\/png compress\\&quot;&quot;;\r\n\r\nprocess.StartInfo.CreateNoWindow = true;\r\nprocess.StartInfo.UseShellExecute = false;\r\n\r\nprocess.Start();\r\n<\/pre>\n<p>And finally an example with AS3 for AIR:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar process:NativeProcess = new NativeProcess();\r\n\r\nvar startupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();\r\nvar file:File = File.applicationDirectory;\r\n\r\nvar exec:String = &quot;&quot;;\r\nif (Capabilities.os.indexOf(&quot;Windows&quot;) &gt;= 0)\r\n\texec = &quot;pngquant-windows.cmd&quot;;\r\nelse if (Capabilities.os.indexOf(&quot;Mac&quot;) &gt;= 0)\r\n\texec = &quot;pngquant-osx&quot;;\r\nelse\r\n\tthrow new Error(&quot;doesn't work on &quot; + Capabilities.os + &quot; operating system&quot;);\r\n\r\nfile.nativePath = file.nativePath + &quot;\/..\/&quot; + exec;\r\nstartupInfo.executable = file;\r\n\r\nvar processArgs:Vector.&lt;String&gt; = new Vector.&lt;String&gt;();\r\nprocessArgs[0] = File.applicationDirectory.nativePath + &quot;\/..\/png-to-compress&quot;;\r\nstartupInfo.arguments = processArgs;\r\n\r\nprocess.start(startupInfo);\r\n<\/pre>\n<p>Be sure to have a look to <a href=\"https:\/\/github.com\/DaVikingCode\/PngQuantNativeProcess\" target=\"_blank\">PngQuantNativeProcess&#8217;s git repository<\/a> to be up to date!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After our Unity runtime SpriteSheets generator, it is a good idea to optimize the generated pngs files. pngquant is a command-line utility and a library for lossy compression of PNG images. The conversion reduces file sizes significantly (often as much as 70%) and preserves full alpha transparency. In other words this is a must have &hellip; <a href=\"https:\/\/davikingcode.com\/blog\/run-pngquant-via-a-nativeprocess-c-unity-as3air\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Run pngquant via a NativeProcess: C#, Unity &#038; AS3\/AIR<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_discordance_state":"","_discordance_checked":true},"categories":[15],"tags":[8,22,40,41,43,39,5],"_links":{"self":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/607"}],"collection":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/comments?post=607"}],"version-history":[{"count":14,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/media?parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/categories?post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/tags?post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}