using System; using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; using Unity.Simulation.Client; using UnityEditor; using UnityEditor.Build.Reporting; using UnityEditor.UIElements; using UnityEngine.Experimental.Perception.Randomization.Editor; using UnityEngine.Experimental.Perception.Randomization.Scenarios; using UnityEngine.SceneManagement; using UnityEngine.UIElements; using ZipUtility; namespace UnityEngine.Perception.Randomization.Editor { class RunInUnitySimulationWindow : EditorWindow { string m_BuildDirectory; string m_BuildZipPath; SysParamDefinition m_SysParam; TextField m_RunNameField; IntegerField m_TotalIterationsField; IntegerField m_InstanceCountField; ObjectField m_MainSceneField; ObjectField m_ScenarioField; Button m_RunButton; [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(); } } /// /// Enables a visual element to remember values between editor sessions /// /// The visual element to enable view data for static void SetViewDataKey(VisualElement element) { element.viewDataKey = $"RunInUnitySimulation_{element.name}"; } void CreateRunInUnitySimulationUI() { var root = rootVisualElement; AssetDatabase.LoadAssetAtPath( $"{StaticData.uxmlDir}/RunInUnitySimulationWindow.uxml").CloneTree(root); m_RunNameField = root.Q("run-name"); SetViewDataKey(m_RunNameField); m_TotalIterationsField = root.Q("total-iterations"); SetViewDataKey(m_TotalIterationsField); m_InstanceCountField = root.Q("instance-count"); SetViewDataKey(m_InstanceCountField); m_MainSceneField = root.Q("main-scene"); m_MainSceneField.objectType = typeof(SceneAsset); if (SceneManager.sceneCount > 0) { var path = SceneManager.GetSceneAt(0).path; var asset = AssetDatabase.LoadAssetAtPath(path); m_MainSceneField.value = asset; } m_ScenarioField = root.Q("scenario"); m_ScenarioField.objectType = typeof(ScenarioBase); m_ScenarioField.value = FindObjectOfType(); var sysParamDefinitions = API.GetSysParams(); var sysParamMenu = root.Q("sys-param"); foreach (var definition in sysParamDefinitions) { sysParamMenu.menu.AppendAction( definition.description, action => { m_SysParam = definition; sysParamMenu.text = definition.description; }); } sysParamMenu.text = sysParamDefinitions[0].description; m_SysParam = sysParamDefinitions[0]; m_RunButton = root.Q