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.UIElements; using ZipUtility; namespace UnityEngine.Perception.Randomization.Editor { class RunInUSimWindow : EditorWindow { 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 USim")] static void ShowWindow() { var window = GetWindow(); window.titleContent = new GUIContent("Run In Unity Simulation"); window.minSize = new Vector2(250, 50); window.Show(); } void OnEnable() { 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 { CreateRunInUSimUI(); } } /// /// Enables a visual element to remember values between editor sessions /// static void SetViewDataKey(VisualElement element) { element.viewDataKey = $"RunInUSim_{element.name}"; } void CreateRunInUSimUI() { var root = rootVisualElement; AssetDatabase.LoadAssetAtPath( $"{StaticData.uxmlDir}/RunInUSimWindow.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); m_ScenarioField = root.Q("scenario"); m_ScenarioField.objectType = typeof(ScenarioBase); 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 = sysParamDefinitions[0].description; m_SysParam = sysParamDefinitions[0]; m_RunButton = root.Q