Skip to content

Instantly share code, notes, and snippets.

@waldobronchart
Last active October 12, 2023 09:13
Show Gist options
  • 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));
}
}
@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