using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Unity.Simulation.Client; using UnityEditor.Build.Reporting; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.Perception.Randomization.Scenarios; using UnityEngine.SceneManagement; using UnityEngine.UIElements; using ZipUtility; namespace UnityEditor.Perception.Randomization { class RunInUnitySimulationWindow : EditorWindow { string m_BuildDirectory; string m_BuildZipPath; SysParamDefinition[] m_SysParamDefinitions; IntegerField m_InstanceCountField; TextField m_RunNameField; IntegerField m_TotalIterationsField; ToolbarMenu m_SysParamMenu; int m_SysParamIndex; ObjectField m_ScenarioConfigField; Button m_RunButton; Label m_PrevRunNameLabel; Label m_ProjectIdLabel; Label m_PrevExecutionIdLabel; RunParameters m_RunParameters; [MenuItem("Window/Run in Unity Simulation")] static void ShowWindow() { var window = GetWindow(); window.titleContent = new GUIContent("Run In Unity Simulation"); window.minSize = new Vector2(250, 50); window.Show(); } void OnEnable() { m_BuildDirectory = Application.dataPath + "/../Build"; Project.Activate(); Project.clientReadyStateChanged += CreateEstablishingConnectionUI; CreateEstablishingConnectionUI(Project.projectIdState); } void OnFocus() { Application.runInBackground = true; } void OnLostFocus() { Application.runInBackground = false; } void CreateEstablishingConnectionUI(Project.State state) { rootVisualElement.Clear(); if (Project.projectIdState == Project.State.Pending) { var waitingText = new TextElement(); waitingText.text = "Waiting for connection to Unity Cloud..."; rootVisualElement.Add(waitingText); } else if (Project.projectIdState == Project.State.Invalid) { var waitingText = new TextElement(); waitingText.text = "The current project must be associated with a valid Unity Cloud project " + "to run in Unity Simulation"; rootVisualElement.Add(waitingText); } else { CreateRunInUnitySimulationUI(); } } void CreateRunInUnitySimulationUI() { var root = rootVisualElement; AssetDatabase.LoadAssetAtPath( $"{StaticData.uxmlDir}/RunInUnitySimulationWindow.uxml").CloneTree(root); m_RunNameField = root.Q("run-name"); m_TotalIterationsField = root.Q("total-iterations"); m_InstanceCountField = root.Q("instance-count"); m_SysParamDefinitions = API.GetSysParams(); m_SysParamMenu = root.Q("sys-param"); for (var i = 0; i < m_SysParamDefinitions.Length; i++) { var index = i; var param = m_SysParamDefinitions[i]; m_SysParamMenu.menu.AppendAction( param.description, action => { m_SysParamIndex = index; m_SysParamMenu.text = param.description; }); } m_ScenarioConfigField = root.Q("scenario-config"); m_ScenarioConfigField.objectType = typeof(TextAsset); var configPath = PlayerPrefs.GetString("SimWindow/scenarioConfig"); if (configPath != string.Empty) m_ScenarioConfigField.value = AssetDatabase.LoadAssetAtPath(configPath); m_RunButton = root.Q