|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using UnityEditor; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +namespace Unity.MLAgents |
| 9 | +{ |
| 10 | + public class SampleExporter |
| 11 | + { |
| 12 | + const string k_MLAgentsSampleFile = "mlagents-sample.json"; |
| 13 | + const string k_PackageSampleFile = ".sample.json"; |
| 14 | + const string k_MLAgentsDir = "ML-Agents"; |
| 15 | + const string k_MLAgentsExamplesDir = "Examples"; |
| 16 | + const string k_MLAgentsPackageName = "com.unity.ml-agents"; |
| 17 | + const string k_MLAgentsSamplesDirName = "Samples"; |
| 18 | + const string k_MLAgentsScriptsDirName = "Scripts"; |
| 19 | + |
| 20 | + struct MLAgentsSampleJson |
| 21 | + { |
| 22 | +#pragma warning disable 649 |
| 23 | + public string displayName; |
| 24 | + public string description; |
| 25 | + // ReSharper disable once CollectionNeverUpdated.Local |
| 26 | + public List<string> scenes; |
| 27 | +#pragma warning restore 649 |
| 28 | + } |
| 29 | + |
| 30 | + struct PackageSampleJson |
| 31 | + { |
| 32 | + public string displayName; |
| 33 | + public string description; |
| 34 | + } |
| 35 | + |
| 36 | + public static void ExportCuratedSamples() |
| 37 | + { |
| 38 | + var oldBurst = EditorPrefs.GetBool("BurstCompilation"); |
| 39 | + EditorPrefs.SetBool("BurstCompilation", false); |
| 40 | + try |
| 41 | + { |
| 42 | + // Path to Project/Assets |
| 43 | + var assetsDir = Application.dataPath; |
| 44 | + var repoRoot = Directory.GetParent(Directory.GetParent(assetsDir).FullName).FullName; |
| 45 | + |
| 46 | + // Top level of where to store the samples |
| 47 | + var samplesDir = Path.Combine( |
| 48 | + repoRoot, |
| 49 | + k_MLAgentsPackageName, |
| 50 | + k_MLAgentsSamplesDirName); |
| 51 | + |
| 52 | + if (!Directory.Exists(samplesDir)) |
| 53 | + { |
| 54 | + Directory.CreateDirectory(samplesDir); |
| 55 | + } |
| 56 | + |
| 57 | + // Path to the examples dir in the project |
| 58 | + var examplesDir = Path.Combine(Application.dataPath, k_MLAgentsDir, k_MLAgentsExamplesDir); |
| 59 | + foreach (var exampleDirectory in Directory.GetDirectories(examplesDir)) |
| 60 | + { |
| 61 | + var mlAgentsSamplePath = Path.Combine(exampleDirectory, k_MLAgentsSampleFile); |
| 62 | + if (File.Exists(mlAgentsSamplePath)) |
| 63 | + { |
| 64 | + var sampleJson = JsonConvert.DeserializeObject<MLAgentsSampleJson>(File.ReadAllText(mlAgentsSamplePath)); |
| 65 | + Debug.Log(JsonConvert.SerializeObject(sampleJson)); |
| 66 | + foreach (var scene in sampleJson.scenes) |
| 67 | + { |
| 68 | + var scenePath = Path.Combine(exampleDirectory, scene); |
| 69 | + if (File.Exists(scenePath)) |
| 70 | + { |
| 71 | + // Create a Sample Directory |
| 72 | + var currentSampleDir = Directory.CreateDirectory(Path.Combine(samplesDir, |
| 73 | + Path.GetFileNameWithoutExtension(scenePath))); |
| 74 | + |
| 75 | + |
| 76 | + var scriptsPath = Path.Combine(exampleDirectory, k_MLAgentsScriptsDirName); |
| 77 | + Debug.Log($"Scene Path: {scenePath}"); |
| 78 | + var assets = new List<string> { scenePath.Substring(scenePath.IndexOf("Assets")) }; |
| 79 | + if (!Directory.Exists(Path.Combine(scriptsPath))) |
| 80 | + { |
| 81 | + scriptsPath = exampleDirectory; |
| 82 | + } |
| 83 | + |
| 84 | + scriptsPath = scriptsPath.Substring(scriptsPath.IndexOf("Assets")); |
| 85 | + foreach (var guid in AssetDatabase.FindAssets("t:Script", new[] { scriptsPath })) |
| 86 | + { |
| 87 | + var path = AssetDatabase.GUIDToAssetPath(guid); |
| 88 | + assets.Add(path); |
| 89 | + Debug.Log($"Adding Asset: {path}"); |
| 90 | + } |
| 91 | + |
| 92 | + var packageFilePath = Path.GetFileNameWithoutExtension(scenePath) + ".unitypackage"; |
| 93 | + AssetDatabase.ExportPackage(assets.ToArray(), |
| 94 | + Path.Combine(Application.dataPath, packageFilePath), |
| 95 | + ExportPackageOptions.IncludeDependencies | ExportPackageOptions.Recurse); |
| 96 | + |
| 97 | + // Move the .unitypackage into the samples folder. |
| 98 | + var packageFileFullPath = Path.Combine(Application.dataPath, packageFilePath); |
| 99 | + |
| 100 | + var packageInSamplePath = Path.Combine(currentSampleDir.FullName, packageFilePath); |
| 101 | + Debug.Log($"Moving {packageFileFullPath} to {packageInSamplePath}"); |
| 102 | + File.Move(packageFileFullPath, packageInSamplePath); |
| 103 | + |
| 104 | + // write the .sample.json file to the sample directory |
| 105 | + File.WriteAllText(Path.Combine(currentSampleDir.FullName, k_PackageSampleFile), |
| 106 | + JsonConvert.SerializeObject(new PackageSampleJson |
| 107 | + { |
| 108 | + description = sampleJson.description, |
| 109 | + displayName = sampleJson.displayName |
| 110 | + })); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + catch (Exception e) |
| 117 | + { |
| 118 | + Debug.Log(e); |
| 119 | + EditorApplication.Exit(1); |
| 120 | + } |
| 121 | + EditorPrefs.SetBool("BurstCompilation", oldBurst); |
| 122 | + EditorApplication.Exit(0); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments