{"id":404,"date":"2016-08-24T12:26:42","date_gmt":"2016-08-24T10:26:42","guid":{"rendered":"http:\/\/davikingcode.com\/blog\/?p=404"},"modified":"2016-08-24T12:46:43","modified_gmt":"2016-08-24T10:46:43","slug":"unity-fast-build-platform-switcher-script","status":"publish","type":"post","link":"https:\/\/davikingcode.com\/blog\/unity-fast-build-platform-switcher-script\/","title":{"rendered":"Unity &#8211; fast build platform switcher script!"},"content":{"rendered":"<p>Today a quick tip for a huge trick!<\/p>\n<p>Working with <a href=\"http:\/\/unity3d.com\/\" target=\"_blank\">Unity<\/a> is pretty awesome until you have to make a build on an other platform than your current one! Depending your project size (number of assets etc.), just changing your current platform for Android to iOS or vice versa could take one hour&#8230; yup seriously! During that time you won&#8217;t be able to work on your project and your computer&#8217;s CPU will be pretty busy&#8230; so time for a long break? Until now!<br \/>\n<!--more--><\/p>\n<p>This tip is based on something well known in Unity community: the library folder is recreated when you change your current target, and so it takes lot of time. This could be optimized by keeping a copy of this folder before changing platform and restore them later when returning back!<\/p>\n<p>We need to use an <a href=\"https:\/\/docs.unity3d.com\/Manual\/ExtendingTheEditor.html\" target=\"_blank\">editor script<\/a> for doing that, here we go:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing UnityEditor;\r\nusing UnityEngine;\r\nusing System;\r\nusing System.Collections;\r\nusing System.IO;\r\n\r\npublic class FastBuildSwitcher:ScriptableWizard {\r\n\r\n\tpublic BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;\r\n\r\n\t[MenuItem(&quot;Tools\/Fast Build Switcher&quot;)]\r\n\tstatic void CreateWizard () {\r\n\r\n\t\tScriptableWizard.DisplayWizard&lt;FastBuildSwitcher&gt;(&quot;Switch Platform&quot;, &quot;Switch&quot;);\r\n\t}\r\n\r\n\tvoid OnWizardCreate() {\r\n\r\n\t\t\/\/Debug.Log(&quot;current platform: &quot; + EditorUserBuildSettings.activeBuildTarget);\r\n\t\t\/\/Debug.Log(&quot;next platform: &quot; + buildTarget);\r\n\r\n\t\tif (EditorUserBuildSettings.activeBuildTarget == buildTarget) {\r\n\r\n\t\t\tDebug.LogWarning(&quot;You set the same next platform than the current one!&quot;);\r\n\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t\/\/save current Library folder state\r\n\t\tif (Directory.Exists(&quot;Library-&quot; + EditorUserBuildSettings.activeBuildTarget))\r\n\t\t\tDirectoryClear(&quot;Library-&quot; + EditorUserBuildSettings.activeBuildTarget);\r\n\r\n\t\tDirectoryCopy(&quot;Library&quot;, &quot;Library-&quot; + EditorUserBuildSettings.activeBuildTarget, true);\r\n\r\n\t\t\/\/restore new target Library folder state\r\n\t\tif (Directory.Exists(&quot;Library-&quot; + buildTarget)) {\r\n\r\n\t\t\tDirectoryClear(&quot;Library&quot;);\r\n\t\t\tDirectory.Delete(&quot;Library&quot;, true);\r\n\r\n\t\t\tDirectory.Move(&quot;Library-&quot; + buildTarget, &quot;Library&quot;);\r\n\t\t}\r\n\r\n\t\tEditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);\r\n\t}\r\n\r\n\tvoid DirectoryClear(string FolderName) {\r\n\t\tDirectoryInfo dir = new DirectoryInfo(FolderName);\r\n\r\n\t\tforeach(FileInfo fi in dir.GetFiles())\r\n\t\t\tfi.Delete();\r\n\r\n\t\tforeach (DirectoryInfo di in dir.GetDirectories()) {\r\n\t\t\t\r\n\t\t\tDirectoryClear(di.FullName);\r\n\t\t\tdi.Delete(true);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) {\r\n\t\t\r\n\t\tDirectoryInfo dir = new DirectoryInfo(sourceDirName);\r\n\t\tDirectoryInfo[] dirs = dir.GetDirectories();\r\n\r\n\t\t\/\/ If the source directory does not exist, throw an exception.\r\n\t\tif (!dir.Exists)\r\n\t\t\tthrow new DirectoryNotFoundException(&quot;Source directory does not exist or could not be found: &quot; + sourceDirName);\r\n\r\n\t\t\/\/ If the destination directory does not exist, create it.\r\n\t\tif (!Directory.Exists(destDirName))\r\n\t\t\tDirectory.CreateDirectory(destDirName);\r\n\r\n\r\n\t\t\/\/ Get the file contents of the directory to copy.\r\n\t\tFileInfo[] files = dir.GetFiles();\r\n\r\n\t\tforeach (FileInfo file in files) {\r\n\t\t\t\/\/ Create the path to the new copy of the file.\r\n\t\t\tstring temppath = Path.Combine(destDirName, file.Name);\r\n\r\n\t\t\t\/\/ Copy the file.\r\n\t\t\tfile.CopyTo(temppath, false);\r\n\t\t}\r\n\r\n\t\t\/\/ If copySubDirs is true, copy the subdirectories.\r\n\t\tif (copySubDirs)\r\n\t\t\tforeach (DirectoryInfo subdir in dirs) {\r\n\t\t\t\t\r\n\t\t\t\t\/\/ Create the subdirectory.\r\n\t\t\t\tstring temppath = Path.Combine(destDirName, subdir.Name);\r\n\r\n\t\t\t\t\/\/ Copy the subdirectories.\r\n\t\t\t\tDirectoryCopy(subdir.FullName, temppath, copySubDirs);\r\n\t\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>The downside is you <strong>should not<\/strong> use anymore Unity build panel for changing platform but our custom window in Unity menu: <em>Tools\/Fast Build Switcher<\/em>.<br \/>\nObviously note each time you&#8217;ve added new assets, it will take some time to import them into the Library folder. Before this script, on a new project with many sprites (more than 1000 512&#215;512!) it takes me 30-40 minutes to change platform, now it&#8217;s less than 3!<br \/>\nI also tested <a href=\"https:\/\/docs.unity3d.com\/Manual\/CacheServer.html\" target=\"_blank\">Unity Cache Server<\/a>, it is free now, running it in localhost but it wasn&#8217;t as fast as this custom script!<\/p>\n<p>Let me know how it goes for you \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today a quick tip for a huge trick! Working with Unity is pretty awesome until you have to make a build on an other platform than your current one! Depending your project size (number of assets etc.), just changing your current platform for Android to iOS or vice versa could take one hour&#8230; yup seriously! &hellip; <a href=\"https:\/\/davikingcode.com\/blog\/unity-fast-build-platform-switcher-script\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Unity &#8211; fast build platform switcher script!<\/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":[32,31,5],"_links":{"self":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/404"}],"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=404"}],"version-history":[{"count":7,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/404\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/404\/revisions\/412"}],"wp:attachment":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/media?parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/categories?post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/tags?post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}