Skip to content

Instantly share code, notes, and snippets.

@chrisyarbrough
Created April 24, 2023 13:42
Show Gist options
  • Save chrisyarbrough/d1f722e1914726f7955cf8b25c1b459c to your computer and use it in GitHub Desktop.
Save chrisyarbrough/d1f722e1914726f7955cf8b25c1b459c to your computer and use it in GitHub Desktop.
A python script that opens a Unity project from within multiple supported locations (project, frontend, assets).
#!/usr/bin/env python3
import os
import sys
import re
import subprocess
from pathlib import Path
def find_version(project_settings_path):
version_pattern = re.compile(r'm_EditorVersion: (.*)')
with open(project_settings_path, 'r') as file:
for line in file:
match = version_pattern.match(line)
if match:
return match.group(1)
return None
def main():
current_directory = Path.cwd()
project_settings_file = Path('ProjectSettings') / 'ProjectVersion.txt'
project_settings_paths = [
current_directory / project_settings_file,
current_directory / 'frontend' / project_settings_file,
current_directory / '..' / project_settings_file
]
project_settings_path = None
for path in project_settings_paths:
if path.is_file():
project_settings_path = path.resolve()
break
if not project_settings_path:
print(f"Couldn't find ProjectSettings file in {current_directory} or {current_directory / 'frontend'}")
sys.exit(1)
unity_version = find_version(project_settings_path)
if unity_version is None:
print("Couldn't find Unity version in ProjectSettings file")
sys.exit(2)
unity_editor_path = f"/Applications/Unity/Hub/Editor/{unity_version}/Unity.app/Contents/MacOS/Unity"
if not os.path.isfile(unity_editor_path):
print(f"Couldn't find Unity Editor at {unity_editor_path}")
sys.exit(3)
project_path = str(project_settings_path.parent.parent)
unity_args = [
unity_editor_path,
'-projectPath', project_path,
'-cacheServerEnableDownload', 'false',
'-cacheServerEnableUpload', 'false',
]
unity_args.extend(sys.argv[1:])
print(f"Starting Unity {unity_version} with arguments: {' '.join(unity_args[1:])}")
subprocess.Popen(unity_args)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment