Skip to content

Instantly share code, notes, and snippets.

@waldobronchart
Last active October 12, 2023 09:13
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save waldobronchart/b3cb789c028c199e2855 to your computer and use it in GitHub Desktop.
Save waldobronchart/b3cb789c028c199e2855 to your computer and use it in GitHub Desktop.
A simple fast platform switcher for Unity
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
public class PlatformSwitcher
{
[MenuItem("Platform/PC, Mac and Linux Standalone")]
static void SwitchPlatformToDesktop()
{
SwitchTo(BuildTarget.StandaloneOSXIntel);
}
[MenuItem("Platform/PC, Mac and Linux Standalone", true)]
static bool SwitchPlatformToDesktopValidate()
{
return EditorUserBuildSettings.activeBuildTarget != BuildTarget.StandaloneOSXIntel;
}
[MenuItem("Platform/iOS")]
static void SwitchPlatformToIOS()
{
SwitchTo(BuildTarget.iPhone);
}
[MenuItem("Platform/iOS", true)]
static bool SwitchPlatformToIOSValidate()
{
return EditorUserBuildSettings.activeBuildTarget != BuildTarget.iPhone;
}
[MenuItem("Platform/Android")]
static void SwitchPlatformToAndroid()
{
SwitchTo(BuildTarget.Android);
}
[MenuItem("Platform/Android", true)]
static bool SwitchPlatformToAndroidValidate()
{
return EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android;
}
public static void SwitchTo(BuildTarget targetPlatform)
{
var currentPlatform = EditorUserBuildSettings.activeBuildTarget;
if (currentPlatform == targetPlatform)
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 playMode");
return;
}
Debug.Log("Switching platform from " + currentPlatform + " to " + targetPlatform);
string libDir = "Library";
string libDirCurrent = libDir + "_" + currentPlatform;
string libDirTarget = libDir + "_" + targetPlatform;
// If target dir doesn't exist yet, make a copy of the current one
if (!Directory.Exists(libDirTarget))
{
Debug.Log("Making a copy of " + libDir + " because " + libDirTarget + " doesn't exist yet");
CopyFilesRecursively(new DirectoryInfo(libDir), new DirectoryInfo(libDirTarget));
}
// Safety check, libDirCurrent shouldn't exist (current data is stored in libDir)
if (Directory.Exists(libDirCurrent))
Directory.Delete(libDirCurrent, true);
// Rename dirs
Directory.Move(libDir, libDirCurrent);
Directory.Move(libDirTarget, libDir);
EditorUserBuildSettings.SwitchActiveBuildTarget(targetPlatform);
Debug.Log("Platform switched to " + targetPlatform);
}
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
}
@Demircivi
Copy link

I have to note this, when you change your platform first time with this script it will take long time as usual. It lets unity build/compile its assets. And when you change back to a built platform in its menu the script copies your old compiled files into Library folder instead of compiling whole platform again. So don't think it's not working.

@silverua
Copy link

silverua commented Jun 5, 2018

Really wanted to use this code, but I am getting this error when it tries to move/rename the folder:

UnauthorizedAccessException: Access to the path is denied.
System.IO.Directory.Move (System.String sourceDirName, System.String destDirName) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/Directory.cs:403)
PlatformSwitcher.SwitchTo (BuildTarget targetPlatform) (at Assets/Scripts/Editor/PlatformSwitcher.cs:82)
PlatformSwitcher.SwitchPlatformToAndroid () (at Assets/Scripts/Editor/PlatformSwitcher.cs:35)

If anyone knows how to fix this, it would be superb. Tried running Unity as administrator, changed the owner of the folder to my current win10 account, granted full permissions, still getting this.

@dustinkerstein
Copy link

This is working great for me in Unity 2017.4.21f1 on OSX for both iOS and Android, but when I switch to PC/OSX standalone I get this error and it doesn't actually switch: Switching to : is disabled

Any idea on how to fix that?

@DataGreed
Copy link

How do I use it?

@andreiagmu
Copy link

andreiagmu commented Oct 13, 2019

Thank you for sharing this awesome code! 😄

I made an improved version of this platform switcher, working in Unity 2018 (and maybe 2019 too?)
https://gist.github.com/andreiagmu/add97f64c3cae3ce7f379a5b376a9c25

An even faster version using symlinks (only for Windows systems)
https://gist.github.com/andreiagmu/116d2dffd7de40d0792ed5536789f218

@pjc0247
Copy link

pjc0247 commented Oct 21, 2019

@andreiagmu How can I run Unity with admin? Unity 2019 always forward me to Unity Hub if I ran directly.

@andreiagmu
Copy link

andreiagmu commented Oct 27, 2019

@pjc0247 I think if you run Unity Hub as admin and open your project, the Editor will also run as admin, if I'm not mistaken.

But if you're using Unity 2019.3, you don't need to use these Platform Switcher scripts anymore, beacuse Unity has already added an improved Asset Pipeline. (Asset Pipeline Version 2)
With this pipeline, Unity natively caches the platforms that you build your project on.

Unity still has to create the Library for each platform (this process already seems a bit faster than before), in subsequent switches the platform switch seems even faster than my symlink script! 😄

@pjc0247
Copy link

pjc0247 commented Oct 27, 2019

@andreiagmu Thanks! I have to download 2019.3 then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment