浏览代码

improved run in usim window

/main
Steven Leal 4 年前
当前提交
cf9332bb
共有 6 个文件被更改,包括 148 次插入58 次删除
  1. 1
      com.unity.perception/Editor/Randomization/Editors/PerceptionEditorAnalytics.cs
  2. 97
      com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
  3. 49
      com.unity.perception/Editor/Randomization/Editors/ScenarioBaseEditor.cs
  4. 38
      com.unity.perception/Editor/Randomization/Uxml/RunInUnitySimulationWindow.uxml
  5. 18
      com.unity.perception/Editor/Randomization/Uss/RunInUnitySimulationWindowStyles.uss
  6. 3
      com.unity.perception/Editor/Randomization/Uss/RunInUnitySimulationWindowStyles.uss.meta

1
com.unity.perception/Editor/Randomization/Editors/PerceptionEditorAnalytics.cs


using System;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Analytics;
namespace UnityEditor.Perception.Randomization

97
com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs


class RunInUnitySimulationWindow : EditorWindow
{
string m_BuildDirectory;
SysParamDefinition[] m_SysParamDefinitions;
ObjectField m_MainSceneField;
ObjectField m_ScenarioField;
SysParamDefinition m_SysParam;
int m_SysParamIndex;
Label m_PrevProjectId;
Label m_PrevExecutionId;
static string currentOpenScenePath => SceneManager.GetSceneAt(0).path;
static ScenarioBase currentScenario => FindObjectOfType<ScenarioBase>();
[MenuItem("Window/Run in Unity Simulation")]
static void ShowWindow()

}
/// <summary>
/// Enables a visual element to remember values between editor sessions
/// Enables a visual element to remember values between editor sessions
/// </summary>
/// <param name="element">The visual element to enable view data for</param>
static void SetViewDataKey(VisualElement element)

$"{StaticData.uxmlDir}/RunInUnitySimulationWindow.uxml").CloneTree(root);
m_RunNameField = root.Q<TextField>("run-name");
SetViewDataKey(m_RunNameField);
m_RunNameField.value = PlayerPrefs.GetString("SimWindow/runName");
SetViewDataKey(m_TotalIterationsField);
m_TotalIterationsField.value = PlayerPrefs.GetInt("SimWindow/totalIterations");
SetViewDataKey(m_InstanceCountField);
m_InstanceCountField.value = PlayerPrefs.GetInt("SimWindow/instanceCount");
m_MainSceneField = root.Q<ObjectField>("main-scene");
m_MainSceneField.objectType = typeof(SceneAsset);
if (SceneManager.sceneCount > 0)
m_SysParamDefinitions = API.GetSysParams();
var sysParamMenu = root.Q<ToolbarMenu>("sys-param");
for (var i = 0; i < m_SysParamDefinitions.Length; i++)
var path = SceneManager.GetSceneAt(0).path;
var asset = AssetDatabase.LoadAssetAtPath<SceneAsset>(path);
m_MainSceneField.value = asset;
}
m_ScenarioField = root.Q<ObjectField>("scenario");
m_ScenarioField.objectType = typeof(ScenarioBase);
m_ScenarioField.value = FindObjectOfType<ScenarioBase>();
var sysParamDefinitions = API.GetSysParams();
var sysParamMenu = root.Q<ToolbarMenu>("sys-param");
foreach (var definition in sysParamDefinitions)
var index = i;
var param = m_SysParamDefinitions[i];
definition.description,
param.description,
m_SysParam = definition;
sysParamMenu.text = definition.description;
m_SysParamIndex = index;
sysParamMenu.text = param.description;
}
sysParamMenu.text = sysParamDefinitions[0].description;
m_SysParam = sysParamDefinitions[0];
m_SysParamIndex = PlayerPrefs.GetInt("SimWindow/sysParamIndex");
sysParamMenu.text = m_SysParamDefinitions[m_SysParamIndex].description;
m_PrevProjectId = root.Q<Label>("project-id");
m_PrevProjectId.text = $"Project ID: {CloudProjectSettings.projectId}";
m_PrevExecutionId = root.Q<Label>("execution-id");
m_PrevExecutionId.text = $"Execution ID: {PlayerPrefs.GetString("SimWindow/prevExecutionId")}";
var copyExecutionIdButton = root.Q<Button>("copy-execution-id");
copyExecutionIdButton.clicked += () =>
EditorGUIUtility.systemCopyBuffer = PlayerPrefs.GetString("SimWindow/prevExecutionId");
var copyProjectIdButton = root.Q<Button>("copy-project-id");
copyProjectIdButton.clicked += () =>
EditorGUIUtility.systemCopyBuffer = CloudProjectSettings.projectId;
}
async void RunInUnitySimulation()

try
{
ValidateSettings();
SetNewPlayerPreferences();
CreateLinuxBuildAndZip();
await StartUnitySimulationRun(runGuid);
}

{
if (string.IsNullOrEmpty(m_RunNameField.value))
throw new MissingFieldException("Empty run name");
if (m_MainSceneField.value == null)
throw new MissingFieldException("Main scene unselected");
if (m_ScenarioField.value == null)
throw new MissingFieldException("Scenario unselected");
var scenario = (ScenarioBase)m_ScenarioField.value;
if (!StaticData.IsSubclassOfRawGeneric(typeof(UnitySimulationScenario<>), scenario.GetType()))
if (string.IsNullOrEmpty(currentOpenScenePath))
throw new MissingFieldException("Invalid scene path");
if (currentScenario == null)
throw new MissingFieldException(
"There is not a Unity Simulation compatible scenario present in the scene");
if (!StaticData.IsSubclassOfRawGeneric(typeof(UnitySimulationScenario<>), currentScenario.GetType()))
void SetNewPlayerPreferences()
{
PlayerPrefs.SetString("SimWindow/runName", m_RunNameField.value);
PlayerPrefs.SetInt("SimWindow/totalIterations", m_TotalIterationsField.value);
PlayerPrefs.SetInt("SimWindow/instanceCount", m_InstanceCountField.value);
PlayerPrefs.SetInt("SimWindow/sysParamIndex", m_SysParamIndex);
}
void CreateLinuxBuildAndZip()
{
// Create build directory

Debug.Log("Creating Linux build...");
var buildPlayerOptions = new BuildPlayerOptions
{
scenes = new[] { AssetDatabase.GetAssetPath(m_MainSceneField.value) },
scenes = new[] { currentOpenScenePath },
locationPathName = Path.Combine(projectBuildDirectory, $"{m_RunNameField.value}.x86_64"),
target = BuildTarget.StandaloneLinux64
};

List<AppParam> GenerateAppParamIds(CancellationToken token, float progressStart, float progressEnd)
{
var appParamIds = new List<AppParam>();
var scenario = (ScenarioBase)m_ScenarioField.value;
var configuration = JObject.Parse(scenario.SerializeToJson());
var configuration = JObject.Parse(currentScenario.SerializeToJson());
var constants = configuration["constants"];
constants["totalIterations"] = m_TotalIterationsField.value;

{
app_params = appParams.ToArray(),
name = m_RunNameField.value,
sys_param_id = m_SysParam.id,
sys_param_id = m_SysParamDefinitions[m_SysParamIndex].id,
build_id = buildId
});
Debug.Log($"Run definition upload complete: run definition id {runDefinitionId}");

EditorUtility.ClearProgressBar();
PerceptionEditorAnalytics.ReportRunInUnitySimulationSucceeded(runGuid, run.executionId);
PlayerPrefs.SetString("SimWindow/prevExecutionId", run.executionId);
m_PrevExecutionId.text = $"Execution ID: {run.executionId}";
}
}
}

49
com.unity.perception/Editor/Randomization/Editors/ScenarioBaseEditor.cs


using UnityEngine;
using System.IO;
using UnityEngine;
using Object = UnityEngine.Object;
namespace UnityEditor.Perception.Randomization
{

ScenarioBase m_Scenario;
SerializedObject m_SerializedObject;
const string k_ConfigFilePlayerPrefKey = "ScenarioBaseEditor/configFilePath";
public override VisualElement CreateInspectorGUI()
{
m_Scenario = (ScenarioBase)target;

CreatePropertyFields();
CheckIfConstantsExist();
var serializeConstantsButton = m_Root.Q<Button>("generate-json-config");
serializeConstantsButton.clicked += () =>
var generateConfigButton = m_Root.Q<Button>("generate-json-config");
generateConfigButton.clicked += () =>
var filePath = EditorUtility.SaveFilePanel(
"Generate Scenario JSON Configuration", Application.dataPath, "scenarioConfiguration", "json");
var filePath = GetSaveFilePath(
"Generate Scenario JSON Configuration", Application.dataPath,
"scenarioConfiguration", "json", k_ConfigFilePlayerPrefKey);
if (string.IsNullOrEmpty(filePath))
return;
PlayerPrefs.SetString(k_ConfigFilePlayerPrefKey, filePath);
var filePath = EditorUtility.OpenFilePanel(
"Import Scenario JSON Configuration", Application.dataPath, "json");
var filePath = GetOpenFilePath(
"Import Scenario JSON Configuration", Application.dataPath, "json", k_ConfigFilePlayerPrefKey);
if (string.IsNullOrEmpty(filePath))
return;
PlayerPrefs.SetString(k_ConfigFilePlayerPrefKey, filePath);
};
return m_Root;

m_InspectorPropertiesContainer.style.marginBottom = 0;
m_ConstantsListVisualContainer.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
}
}
static string GetSaveFilePath(
string title, string defaultDirectory, string defaultFileName, string fileExtension, string playerPrefKey)
{
var prevFilePath = PlayerPrefs.GetString(playerPrefKey);
var prevDirectory = defaultDirectory;
var prevFileName = defaultFileName;
if (File.Exists(prevFilePath))
{
prevDirectory = Path.GetDirectoryName(prevFilePath);
prevFileName = Path.GetFileNameWithoutExtension(prevFilePath);
}
return EditorUtility.SaveFilePanel(
title, prevDirectory, prevFileName, fileExtension);
}
static string GetOpenFilePath(string title, string defaultDirectory, string fileExtension, string playerPrefKey)
{
var prevFilePath = PlayerPrefs.GetString(playerPrefKey);
var prevDirectory = defaultDirectory;
if (File.Exists(prevFilePath))
prevDirectory = Path.GetDirectoryName(prevFilePath);
return EditorUtility.OpenFilePanel(title, prevDirectory, fileExtension);
}
}
}

38
com.unity.perception/Editor/Randomization/Uxml/RunInUnitySimulationWindow.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<TextField name="run-name" label="Run Name"/>
<editor:IntegerField name="total-iterations" label="Total Iterations"/>
<editor:IntegerField name="instance-count" label="Instance Count" max-value="10000"/>
<editor:ObjectField name="main-scene" label="Main Scene" allow-scene-objects="false"/>
<editor:ObjectField name="scenario" label="Scenario"/>
<VisualElement class="unity-base-field">
<Label text="Sys-Param" class="unity-base-field__label"/>
<editor:ToolbarMenu name="sys-param" class="unity-base-field__input" style="border-width: 1px;"/>
</VisualElement>
<VisualElement style="align-items: center;">
<Button name="run-button" text="Build and Run" style="margin: 10px; padding: 2 20; font-size: 13px;"/>
<VisualElement style="margin: 2 4 2 4;">
<Style src="../Uss/RunInUnitySimulationWindowStyles.uss"/>
<VisualElement class="sim-window__container-outer">
<Label text="Simulation Parameters" class="sim-window__header-1"/>
<TextField name="run-name" label="Run Name"/>
<editor:IntegerField name="total-iterations" label="Total Iterations"/>
<editor:IntegerField name="instance-count" label="Instance Count" max-value="10000"/>
<VisualElement class="unity-base-field">
<Label text="Sys-Param" class="unity-base-field__label"/>
<editor:ToolbarMenu name="sys-param" class="unity-base-field__input" style="border-width: 1px;"/>
</VisualElement>
<VisualElement style="align-items: center;">
<Button name="run-button" text="Build and Run" style="margin: 10px; padding: 2 20; font-size: 13px;"/>
</VisualElement>
</VisualElement>
<VisualElement class="sim-window__container-outer">
<Label text="Results of Previous Run" class="sim-window__header-1"/>
<Label name="project-id" text="Project ID: " class="sim-window__label-prev-result"/>
<Label name="execution-id" text="Execution ID: " class="sim-window__label-prev-result"/>
<VisualElement style="flex-direction: row;">
<Button name="copy-execution-id" text="Copy Execution ID" style="flex-grow: 1; flex-shrink: 0;"/>
<Button name="copy-project-id" text="Copy Project ID" style="flex-grow: 1; flex-shrink: 0;"/>
</VisualElement>
</VisualElement>
</VisualElement>
</UXML>

18
com.unity.perception/Editor/Randomization/Uss/RunInUnitySimulationWindowStyles.uss


.sim-window__header-1 {
font-size: 14px;
-unity-font-style: bold;
margin-bottom: 1px;
margin-left: 2px;
}
.sim-window__container-outer {
background-color: #2A2A2A;
margin-bottom: 8px;
margin-top: 2px;
padding: 2px;
}
.sim-window__label-prev-result {
padding: 2 2 0 1; /*top, right, bottom, left*/
margin: 1 3 1 3;
}

3
com.unity.perception/Editor/Randomization/Uss/RunInUnitySimulationWindowStyles.uss.meta


fileFormatVersion: 2
guid: 67ac54c923534486a149e4d1d85d9c83
timeCreated: 1613109854
正在加载...
取消
保存