Last active
February 19, 2020 18:52
-
-
Save daltonbr/07a860a980fd4d77f3a1550f6072d7a0 to your computer and use it in GitHub Desktop.
Python script to fast switch platforms in Unity for Windows 10, relies in symlinks
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
#!/usr/bin/env python3-64 | |
# Windows 10 | |
# You can associate this file in Windows in order to avoid calling the Python interpreter | |
# right click -> Open With -> Choose Another App, and then choose Python. | |
import sys | |
import os | |
import shutil | |
from pathlib import Path | |
def RemoveLibraryDirErrorHandler(func, path, exc_info): | |
print("β[Error] Cannot find or remove Library folder") | |
print(exc_info) | |
exit() | |
def TryKillUnityHub(): | |
print("πͺ Trying to kill Unity Hub processes") | |
killHubCmd = 'wmic process where name="Unity Hub.exe" call terminate' | |
os.system(killHubCmd) | |
def find_unityprocesses(): | |
print("β³ Trying to find Unity Processes") | |
# Need improvements, any Process with the name 'Unity' will trigger this check | |
# wmic is a cli for Windows | |
with os.popen('wmic process where "name like \'%Unity%\'" get name, processid /format:table') as textfile: | |
contents = textfile.read() | |
# when the (log) textfile above is 'empty' his length is 4 | |
if (len(contents) > 4): | |
# processes found | |
print (contents) | |
return True | |
else: | |
return False | |
return 0 | |
sys.stdout.write("π±βπ--== Windows Unity Platform Switcher! ==--\nπ Powered by Python %s\n\n" % (sys.version)) | |
platformToSwitch = '' | |
if (len(sys.argv) == 2): | |
if (sys.argv[1] == 'ios'): | |
platformToSwitch = 'ios' | |
elif (sys.argv[1] == 'android'): | |
platformToSwitch = 'android' | |
else: | |
print("β[Error] Invalid argument.") | |
print(" USAGE") | |
print("$ WindowsSwitchPlatform.py <android>|<ios>") | |
exit() | |
else : | |
print("β[Error] Wrong number of arguments.") | |
print(" USAGE") | |
print("$ WindowsSwitchPlatform.py <android>|<ios>") | |
exit() | |
found = find_unityprocesses() | |
if found: | |
TryKillUnityHub() | |
found = find_unityprocesses() | |
if found: | |
print("π€ Please close Unity Processess manually and run it again.") | |
exit() | |
else: | |
print("β All Unity processes are closed") | |
# `path.parents[1]` is the same as `path.parent.parent` | |
# we start this script 2 levels bellow projectDir, we can improve this part | |
projectDir = Path(__file__).resolve().parents[2] | |
if os.path.isfile(os.path.join(projectDir, 'ProjectSettings/ProjectVersion.txt')): | |
print("β Find project root directory ", projectDir) | |
else: | |
print("β[Error] Cannot find project root directory", projectDir) | |
exit() | |
libraryDir = os.path.join(projectDir, 'Library') | |
platformDir = os.path.join(projectDir, platformToSwitch + 'Library') | |
if os.path.islink(libraryDir): | |
print("β Old Library symlink found, deactivating it", libraryDir) | |
os.unlink(libraryDir) | |
else: | |
print("β Trying to remove Library folder") | |
shutil.rmtree(libraryDir, onerror = RemoveLibraryDirErrorHandler) | |
if (os.path.isdir(platformDir)): | |
print("β Find target platform library: ", platformDir) | |
else: | |
print("β Creating target platform library: ", platformDir) | |
os.mkdir(platformDir) | |
print("β Creating new Library symlink", platformDir) | |
os.symlink(platformDir, libraryDir, target_is_directory = True) | |
print("π Platform switched!") | |
# Force kill Unity Hub | |
# wmic process where name="Unity Hub.exe" call terminate | |
# find all processes with the name 'Unity' and export to a csv file | |
# wmic process where "name like '%Unity%'" get name, processid /format:csv > unity.txt | |
#wmic process where name='Unity.exe' call terminate | |
#wmic process where name='Unity Hub.exe' call terminate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment