Last active
June 19, 2024 07:26
-
-
Save andreiagmu/116d2dffd7de40d0792ed5536789f218 to your computer and use it in GitHub Desktop.
Improved FastPlatformSwitcher for Unity 2018 (and maybe other Unity versions), for Asset Database v1 - Symlink version (for Windows systems)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Improved FastPlatformSwitcher for Unity 2018 (and maybe other Unity versions) | |
// For Asset Database v1 | |
// Symlink version (for Windows systems) | |
// by Andy Miira (Andrei Müller), October 2019 | |
// | |
// Based on: | |
// Unity – fast build platform switcher script! (by Aymeric - Da Viking Code) | |
// https://davikingcode.com/blog/unity-fast-build-platform-switcher-script/ | |
// | |
// A simple fast platform switcher for Unity (by Waldo Bronchart) | |
// https://gist.github.com/waldobronchart/b3cb789c028c199e2855 | |
// | |
// | |
// [INSTRUCTIONS] | |
// 0) Add this script inside an "Editor" folder in your project. | |
// You must create this folder if it doesn't already exist. | |
// | |
// 1) Close Unity, then rename your project's Library folder, adding "-<name of the current Target Platform>" | |
// | |
// For example, if your current Target Platform is Windows 64-bit, rename "Library" to "Library-StandaloneWindows64". | |
// Or if your current Target Platform is Android, rename "Library" to "Library-Android". | |
// | |
// You can check the supported Target Platform names in this webpage: | |
// https://docs.unity3d.com/ScriptReference/BuildTarget.html | |
// | |
// 2) Open a Windows Command Prompt (cmd.exe) or PowerShell window as administrator (!), and navigate to the project's folder. | |
// Then, create a symlink called "Library", pointing to the directory where your actual Library is currently located. | |
// | |
// For example, if your current Target Platform is Windows 64-bit: | |
// In Command Prompt, type or copy the following line: | |
// mklink /D "Library" "Library-StandaloneWindows64" | |
// | |
// If you use PowerShell instead, type or copy the following line: | |
// New-Item -ItemType SymbolicLink -Name "Library" -Target "Library-StandaloneWindows64" | |
// | |
// Press Enter, and the required symlink will be created. | |
// | |
// 3) Open Unity as administrator (!), | |
// then click on "Tools -> Fast Build Switcher -> Use Symlink - Windows" at the menu bar. | |
// | |
// After the switcher window is loaded, select your desired Target Platform on the dropdown, | |
// then click on the "Switch Platform" button. | |
// Please do not switch to a Target Platform that you don't have installed, to avoid symlink errors! | |
// | |
// | |
// [IMPORTANT] | |
// Remember, you MUST run Command Prompt, PowerShell and especially Unity with ADMINISTRATOR PRIVILEGES, | |
// or else the symlink operations (including this Platform Switcher) won't work! | |
// | |
// To switch the project's platform, ONLY use THIS platform switcher, | |
// instead of Unity's standard platform switcher (in Build Settings) or any other switcher. | |
// | |
// When you are switching to a Target Platform for the first time, Unity must create that platform's Library. | |
// You must wait for Unity's standard Library creation to finish, | |
// that could take a long time depending on your project's size. | |
// But at the next switches, if the Target Platform's Library already exists, | |
// the switch process will be MUCH faster. | |
// | |
// Some entries you may want to add in your .gitignore file: | |
// # Ignore all folders whose name starts with Library | |
// /[Ll]ibrary*/ | |
// # Ignore any symlink called Library | |
// /[Ll]ibrary* | |
// | |
// | |
// Check out more Unity scripts and utilities at: | |
// https://gist.github.com/andreiagmu | |
using UnityEditor; | |
using UnityEngine; | |
using System.IO; | |
using System.Runtime.InteropServices; | |
namespace MirrorMirai | |
{ | |
public class FastPlatformSwitcherSymlink_Windows : ScriptableWizard | |
{ | |
public BuildTarget targetPlatform; | |
#region PInvoke signatures | |
/// <summary> | |
/// Creates a symbolic link in the filesystem. Requires Vista or higher. Requires administrator access. | |
/// </summary> | |
/// <param name="lpSymlinkFileName"></param> | |
/// <param name="lpTargetFileName"></param> | |
/// <param name="dwFlags"></param> | |
/// <returns></returns> | |
[DllImport("kernel32.dll", CharSet=CharSet.Unicode)] | |
static extern bool CreateSymbolicLink( | |
string lpSymlinkFileName, | |
string lpTargetFileName, | |
SymbolicLink dwFlags); | |
enum SymbolicLink | |
{ | |
File = 0, | |
Directory = 1 | |
} | |
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)] | |
public static extern bool RemoveDirectory(string path); | |
#endregion PInvoke signatures | |
[MenuItem("Tools/Fast Platform Switcher/Use Symlink - Windows")] | |
static void CreateWizard() | |
{ | |
ScriptableWizard.DisplayWizard<FastPlatformSwitcherSymlink_Windows>("Fast Platform Switcher (Use Symlink - Windows)", "Switch Platform"); | |
} | |
void OnWizardCreate() | |
{ | |
if (targetPlatform == 0) { | |
Debug.LogWarning("You didn't select a valid Target Platform!"); | |
return; | |
} | |
var currentPlatform = EditorUserBuildSettings.activeBuildTarget; | |
//Debug.Log("current platform: " + currentPlatform); | |
//Debug.Log("next platform: " + buildTarget); | |
if (currentPlatform == targetPlatform) { | |
Debug.LogWarning("You selected the current platform as the Target Platform!"); | |
return; | |
} | |
// Don't switch when compiling | |
if (EditorApplication.isCompiling) { | |
Debug.LogWarning("Could not switch platform because Unity is compiling!"); | |
return; | |
} | |
// Don't switch while playing | |
if (EditorApplication.isPlayingOrWillChangePlaymode) { | |
Debug.LogWarning("Could not switch platform because Unity is in Play Mode!"); | |
return; | |
} | |
Debug.Log("Switching platform from " + currentPlatform + " to " + targetPlatform); | |
if (!Directory.Exists("Library-" + targetPlatform)) { | |
DirectoryCopy("Library-" + currentPlatform, "Library-" + targetPlatform, true); | |
} | |
RemoveDirectory("Library"); | |
CreateSymbolicLink( | |
"Library", | |
"Library-" + targetPlatform, | |
SymbolicLink.Directory); | |
var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(targetPlatform); | |
EditorUserBuildSettings.SwitchActiveBuildTarget(buildTargetGroup, targetPlatform); | |
Debug.Log("Platform switched to " + targetPlatform); | |
} | |
void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) | |
{ | |
DirectoryInfo dir = new DirectoryInfo(sourceDirName); | |
DirectoryInfo[] dirs = dir.GetDirectories(); | |
// If the source directory does not exist, throw an exception. | |
if (!dir.Exists) | |
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName); | |
// If the destination directory does not exist, create it. | |
if (!Directory.Exists(destDirName)) | |
Directory.CreateDirectory(destDirName); | |
// Get the file contents of the directory to copy. | |
FileInfo[] files = dir.GetFiles(); | |
foreach (FileInfo file in files) { | |
if (IsFileBlacklisted(file.Name)) | |
continue; | |
// Create the path to the new copy of the file. | |
string temppath = Path.Combine(destDirName, file.Name); | |
// Copy the file. | |
file.CopyTo(temppath, false); | |
} | |
// If copySubDirs is true, copy the subdirectories. | |
if (copySubDirs) | |
foreach (DirectoryInfo subdir in dirs) { | |
// Create the subdirectory. | |
string temppath = Path.Combine(destDirName, subdir.Name); | |
// Copy the subdirectories. | |
DirectoryCopy(subdir.FullName, temppath, copySubDirs); | |
} | |
} | |
/// <summary> | |
/// Checks for files that shouldn't be moved or copied (mainly files that are being used by some Unity process) | |
/// </summary> | |
/// <returns></returns> | |
private static bool IsFileBlacklisted(string filename) | |
{ | |
if (filename == "ShaderCache.db" || filename.StartsWith("shadercompiler-UnityShaderCompiler.exe")) | |
return true; | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment