-
-
Save waldobronchart/b3cb789c028c199e2855 to your computer and use it in GitHub Desktop.
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)); | |
} | |
} |
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?
How do I use it?
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
@andreiagmu How can I run Unity with admin? Unity 2019 always forward me to Unity Hub if I ran directly.
@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! 😄
@andreiagmu Thanks! I have to download 2019.3 then.
Really wanted to use this code, but I am getting this error when it tries to move/rename the folder:
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.