parse_project_settings.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import argparse
  2. import sys
  3. import json
  4. from pathlib import Path
  5. # Add the utils directory to the Python path
  6. utils_path = Path(__file__).parent.parent / 'utils'
  7. sys.path.append(str(utils_path))
  8. from json_utils import write_json
  9. from yaml_utils import load_unity_yaml, convert_to_plain_python_types
  10. def main():
  11. parser = argparse.ArgumentParser(description="Parses key ProjectSettings files into JSON.")
  12. parser.add_argument("--input", type=str, required=True, help="The root directory of the target Unity project.")
  13. parser.add_argument("--output", type=str, required=True, help="The directory where the output will be saved.")
  14. parser.add_argument("--indent", type=int, default=None)
  15. parser.add_argument("--shrink-json", action='store_true')
  16. # This script doesn't search asset folders, so ignored_folders is not needed,
  17. # but we add it to prevent unrecognized argument errors.
  18. parser.add_argument("--ignored-folders", type=str, default='[]')
  19. args = parser.parse_args()
  20. input_dir = Path(args.input).resolve()
  21. output_dir = Path(args.output).resolve()
  22. project_settings_dir = input_dir / "ProjectSettings"
  23. output_settings_dir = output_dir / "ProjectSettings"
  24. if not project_settings_dir.is_dir():
  25. print("No ProjectSettings directory found.")
  26. return
  27. files_to_parse = [
  28. "TagManager.asset", "DynamicsManager.asset", "Physics2DSettings.asset",
  29. "EditorBuildSettings.asset", "GraphicsSettings.asset", "QualitySettings.asset",
  30. "InputManager.asset", "ProjectSettings.asset"
  31. ]
  32. print(f"\n--- Starting Project Settings Parsing ---")
  33. found_any = False
  34. for filename in files_to_parse:
  35. file_path = project_settings_dir / filename
  36. if not file_path.is_file():
  37. continue
  38. found_any = True
  39. print(f"Processing: {filename}")
  40. output_path = (output_settings_dir / filename).with_suffix('.json')
  41. output_path.parent.mkdir(parents=True, exist_ok=True)
  42. try:
  43. documents = load_unity_yaml(file_path)
  44. if documents:
  45. # Most settings files have one document, export it directly
  46. data = convert_to_plain_python_types(documents[0])
  47. write_json(data, output_path, indent=args.indent, shrink=args.shrink_json)
  48. except Exception as e:
  49. print(f"Error processing {filename}: {e}", file=sys.stderr)
  50. if not found_any:
  51. print("No project settings files found to parse.")
  52. else:
  53. print("Project settings parsing complete.")
  54. if __name__ == "__main__":
  55. main()