Forked from waldobronchart/UnityPlatformSwitcher.cs
Last active
February 5, 2025 00:56
-
-
Save TheBrunoRM/d8a8e04641ebdb846631512d689b621e to your computer and use it in GitHub Desktop.
A simple fast platform switcher for Unity 6 (made for a client-server solution project)
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
using System; | |
using System.IO; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEditor.Build.Profile; | |
using UnityEngine; | |
namespace Editor | |
{ | |
public abstract class PlatformSwitcher | |
{ | |
private static readonly BuildProfile ClientProfile = | |
AssetDatabase.LoadAssetAtPath<BuildProfile>("Assets/Settings/Build Profiles/Client.asset"); | |
private static readonly BuildProfile ServerProfile = | |
AssetDatabase.LoadAssetAtPath<BuildProfile>("Assets/Settings/Build Profiles/Server.asset"); | |
private static string _currentTaskTitle = "Copying files..."; | |
private static int _totalFiles; | |
private static int _currentFile; | |
[MenuItem("Platform/Client")] | |
private static void SwitchPlatformToWindows() | |
{ | |
SwitchTo(ClientProfile); | |
} | |
[MenuItem("Platform/Client", true)] | |
private static bool SwitchPlatformToDesktopValidate() | |
{ | |
return BuildProfile.GetActiveBuildProfile() != ClientProfile; | |
} | |
[MenuItem("Platform/Server")] | |
private static void SwitchPlatformToLinux() | |
{ | |
SwitchTo(ServerProfile); | |
} | |
[MenuItem("Platform/Server", true)] | |
private static bool SwitchPlatformToLinuxValidate() | |
{ | |
return BuildProfile.GetActiveBuildProfile() != ServerProfile; | |
} | |
//[MenuItem("Platform/Test")] | |
private static void TestDirectories() | |
{ | |
var dirs = Directory.GetFileSystemEntries("Library"); | |
foreach (var dir in dirs) Debug.Log(GetWithoutFolder(dir)); | |
Debug.Log(dirs.Length); | |
} | |
private static string GetWithoutFolder(string path) | |
{ | |
return string.Join(Path.DirectorySeparatorChar, path.Split(Path.DirectorySeparatorChar).Skip(1)); | |
} | |
private static void SwitchTo(BuildProfile targetPlatform) | |
{ | |
var currentPlatform = BuildProfile.GetActiveBuildProfile(); | |
if (currentPlatform == targetPlatform) | |
return; | |
// Don't switch when compiling | |
if (EditorApplication.isCompiling) | |
{ | |
Debug.LogWarning("Platform switch cancelled: Unity is compiling."); | |
return; | |
} | |
// Don't switch while playing | |
if (EditorApplication.isPlayingOrWillChangePlaymode) | |
{ | |
Debug.LogWarning("Platform switch cancelled: Unity is playing."); | |
return; | |
} | |
Debug.Log($"Switching platform from {currentPlatform.name} to {targetPlatform.name}"); | |
var libDir = "Library"; | |
var libDirCacheCurrent = libDir + "_" + currentPlatform.name; | |
var libDirCacheTarget = libDir + "_" + targetPlatform.name; | |
// this is essentially what we do | |
// Library => CurrentCache | |
// TargetCache => Library | |
if (!Directory.Exists(libDirCacheCurrent)) | |
{ | |
_totalFiles = 0; | |
_currentTaskTitle = "Caching current Library folder..."; | |
// Library => CurrentCache | |
Debug.Log($"Making a copy of {libDir} because {libDirCacheCurrent} doesn't exist yet"); | |
CopyFilesRecursively(new DirectoryInfo(libDir), new DirectoryInfo(libDirCacheCurrent)); | |
EditorUtility.ClearProgressBar(); | |
} | |
if (!Directory.Exists(libDirCacheTarget)) | |
{ | |
Debug.Log("Target Library does not exist, will create now (slow)!"); | |
} | |
else | |
{ | |
// TargetCache => Library | |
_totalFiles = 0; | |
_currentTaskTitle = "Copying files from cache..."; | |
Debug.Log("Copying files from the Target to the current Library!"); | |
CopyFilesRecursively(new DirectoryInfo(libDirCacheTarget), new DirectoryInfo(libDir)); | |
EditorUtility.ClearProgressBar(); | |
} | |
BuildProfile.SetActiveBuildProfile(targetPlatform); | |
Debug.Log("Platform switched to " + targetPlatform.name); | |
} | |
private static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) | |
{ | |
foreach (var dir in source.GetDirectories()) | |
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name)); | |
var files = source.GetFiles(); | |
_totalFiles += files.Length; | |
foreach (var file in files) | |
try | |
{ | |
file.CopyTo(Path.Combine(target.FullName, file.Name), true); | |
_currentFile++; | |
EditorUtility.DisplayProgressBar(_currentTaskTitle, $"Processing file {_currentFile}/{_totalFiles}", | |
(float)_currentFile / _totalFiles); | |
} | |
catch (Exception e) | |
{ | |
Debug.LogWarning($"Could not copy {file}: {e.Message}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment