浏览代码

prevented usim run parameters from changing during run

/main
Steven Leal 4 年前
当前提交
900a30c7
共有 2 个文件被更改,包括 111 次插入60 次删除
  1. 170
      com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
  2. 1
      com.unity.perception/Editor/Randomization/Uxml/RunInUnitySimulationWindow.uxml

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


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;

IntegerField m_InstanceCountField;
TextField m_RunNameField;
IntegerField m_TotalIterationsField;
ToolbarMenu m_SysParamMenu;
ObjectField m_ScenarioConfig;
ObjectField m_ScenarioConfigField;
Label m_PrevProjectId;
Label m_PrevExecutionId;
static string currentOpenScenePath => SceneManager.GetSceneAt(0).path;
static ScenarioBase currentScenario => FindObjectOfType<ScenarioBase>();
TextAsset scenarioConfig => (TextAsset)m_ScenarioConfig.value;
string scenarioConfigAssetPath => AssetDatabase.GetAssetPath(scenarioConfig);
Label m_PrevRunNameLabel;
Label m_ProjectIdLabel;
Label m_PrevExecutionIdLabel;
RunParameters m_RunParameters;
[MenuItem("Window/Run in Unity Simulation")]
static void ShowWindow()

$"{StaticData.uxmlDir}/RunInUnitySimulationWindow.uxml").CloneTree(root);
m_RunNameField = root.Q<TextField>("run-name");
m_RunNameField.value = PlayerPrefs.GetString("SimWindow/runName");
m_TotalIterationsField.value = PlayerPrefs.GetInt("SimWindow/totalIterations");
m_InstanceCountField.value = PlayerPrefs.GetInt("SimWindow/instanceCount");
var sysParamMenu = root.Q<ToolbarMenu>("sys-param");
m_SysParamMenu = root.Q<ToolbarMenu>("sys-param");
sysParamMenu.menu.AppendAction(
m_SysParamMenu.menu.AppendAction(
sysParamMenu.text = param.description;
m_SysParamMenu.text = param.description;
m_SysParamIndex = PlayerPrefs.GetInt("SimWindow/sysParamIndex");
sysParamMenu.text = m_SysParamDefinitions[m_SysParamIndex].description;
m_ScenarioConfig = root.Q<ObjectField>("scenario-config");
m_ScenarioConfig.objectType = typeof(TextAsset);
m_ScenarioConfigField = root.Q<ObjectField>("scenario-config");
m_ScenarioConfigField.objectType = typeof(TextAsset);
m_ScenarioConfig.value = AssetDatabase.LoadAssetAtPath<TextAsset>(configPath);
m_ScenarioConfigField.value = AssetDatabase.LoadAssetAtPath<TextAsset>(configPath);
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")}";
m_PrevRunNameLabel = root.Q<Label>("prev-run-name");
m_ProjectIdLabel = root.Q<Label>("project-id");
m_PrevExecutionIdLabel = root.Q<Label>("execution-id");
var copyExecutionIdButton = root.Q<Button>("copy-execution-id");
copyExecutionIdButton.clicked += () =>

copyProjectIdButton.clicked += () =>
EditorGUIUtility.systemCopyBuffer = CloudProjectSettings.projectId;
SetFieldsFromPlayerPreferences();
}
void SetFieldsFromPlayerPreferences()
{
m_RunNameField.value = IncrementRunName(PlayerPrefs.GetString("SimWindow/runName"));
m_TotalIterationsField.value = PlayerPrefs.GetInt("SimWindow/totalIterations");
m_InstanceCountField.value = PlayerPrefs.GetInt("SimWindow/instanceCount");
m_SysParamIndex = PlayerPrefs.GetInt("SimWindow/sysParamIndex");
m_SysParamMenu.text = m_SysParamDefinitions[m_SysParamIndex].description;
m_PrevRunNameLabel.text = $"Run Name: {PlayerPrefs.GetString("SimWindow/runName")}";
m_ProjectIdLabel.text = $"Project ID: {CloudProjectSettings.projectId}";
m_PrevExecutionIdLabel.text = $"Execution ID: {PlayerPrefs.GetString("SimWindow/prevExecutionId")}";
}
static string IncrementRunName(string runName)
{
if (string.IsNullOrEmpty(runName))
return "Run0";
var stack = new Stack<char>();
var i = runName.Length - 1;
for (; i >= 0; i--)
{
if (!char.IsNumber(runName[i]))
break;
stack.Push(runName[i]);
}
if (stack.Count == 0)
return runName + "1";
var numericString = string.Concat(stack.ToArray());
var runVersion = int.Parse(numericString) + 1;
return runName.Substring(0, i + 1) + runVersion;
m_RunParameters = new RunParameters
{
runName = m_RunNameField.value,
totalIterations = m_TotalIterationsField.value,
instanceCount = m_InstanceCountField.value,
sysParamIndex = m_SysParamIndex,
scenarioConfig = (TextAsset)m_ScenarioConfigField.value,
currentOpenScenePath = SceneManager.GetSceneAt(0).path,
currentScenario = FindObjectOfType<ScenarioBase>()
};
m_TotalIterationsField.value,
m_InstanceCountField.value,
m_RunParameters.totalIterations,
m_RunParameters.instanceCount,
SetNewPlayerPreferences();
CreateLinuxBuildAndZip();
await StartUnitySimulationRun(runGuid);
}

void ValidateSettings()
{
if (string.IsNullOrEmpty(m_RunNameField.value))
if (string.IsNullOrEmpty(m_RunParameters.runName))
if (string.IsNullOrEmpty(currentOpenScenePath))
if (m_RunParameters.instanceCount <= 0)
throw new NotSupportedException("Invalid instance count specified");
if (m_RunParameters.totalIterations <= 0)
throw new NotSupportedException("Invalid total iteration count specified");
if (string.IsNullOrEmpty(m_RunParameters.currentOpenScenePath))
if (currentScenario == null)
if (m_RunParameters.currentScenario == null)
if (!StaticData.IsSubclassOfRawGeneric(typeof(UnitySimulationScenario<>), currentScenario.GetType()))
if (!StaticData.IsSubclassOfRawGeneric(
typeof(UnitySimulationScenario<>), m_RunParameters.currentScenario.GetType()))
if (scenarioConfig != null && Path.GetExtension(scenarioConfigAssetPath) != ".json")
if (m_RunParameters.scenarioConfig != null &&
Path.GetExtension(m_RunParameters.scenarioConfigAssetPath) != ".json")
}
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);
PlayerPrefs.SetString("SimWindow/scenarioConfig",
scenarioConfig != null ? scenarioConfigAssetPath : string.Empty);
var projectBuildDirectory = $"{m_BuildDirectory}/{m_RunNameField.value}";
var projectBuildDirectory = $"{m_BuildDirectory}/{m_RunParameters.runName}";
scenes = new[] { currentOpenScenePath },
locationPathName = Path.Combine(projectBuildDirectory, $"{m_RunNameField.value}.x86_64"),
scenes = new[] { m_RunParameters.currentOpenScenePath },
locationPathName = Path.Combine(projectBuildDirectory, $"{m_RunParameters.runName}.x86_64"),
target = BuildTarget.StandaloneLinux64
};
var report = BuildPipeline.BuildPlayer(buildPlayerOptions);

EditorUtility.DisplayProgressBar("Unity Simulation Run", "Zipping Linux build...", 0f);
Zip.DirectoryContents(projectBuildDirectory, m_RunNameField.value);
Zip.DirectoryContents(projectBuildDirectory, m_RunParameters.runName);
m_BuildZipPath = projectBuildDirectory + ".zip";
}

var configuration = JObject.Parse(scenarioConfig != null
? File.ReadAllText(scenarioConfigAssetPath)
: currentScenario.SerializeToJson());
var configuration = JObject.Parse(m_RunParameters.scenarioConfig != null
? File.ReadAllText(m_RunParameters.scenarioConfigAssetPath)
: m_RunParameters.currentScenario.SerializeToJson());
constants["totalIterations"] = m_TotalIterationsField.value;
constants["instanceCount"] = m_InstanceCountField.value;
constants["totalIterations"] = m_RunParameters.totalIterations;
constants["instanceCount"] = m_RunParameters.instanceCount;
var appParamName = $"{m_RunNameField.value}";
var appParamName = $"{m_RunParameters.runName}";
var appParamsString = JsonConvert.SerializeObject(configuration, Formatting.Indented);
var appParamId = API.UploadAppParam(appParamName, appParamsString);
appParamIds.Add(new AppParam

num_instances = m_InstanceCountField.value
num_instances = m_RunParameters.instanceCount
});
return appParamIds;
}

var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
var buildId = await API.UploadBuildAsync(
m_RunNameField.value,
m_RunParameters.runName,
m_BuildZipPath,
null, null,
cancellationTokenSource,

var runDefinitionId = API.UploadRunDefinition(new RunDefinition
{
app_params = appParams.ToArray(),
name = m_RunNameField.value,
sys_param_id = m_SysParamDefinitions[m_SysParamIndex].id,
name = m_RunParameters.runName,
sys_param_id = m_SysParamDefinitions[m_RunParameters.sysParamIndex].id,
build_id = buildId
});

m_RunButton.SetEnabled(true);
EditorUtility.ClearProgressBar();
PerceptionEditorAnalytics.ReportRunInUnitySimulationSucceeded(runGuid, run.executionId);
// Set new Player Preferences
PlayerPrefs.SetString("SimWindow/runName", m_RunParameters.runName);
m_PrevExecutionId.text = $"Execution ID: {run.executionId}";
PlayerPrefs.SetInt("SimWindow/totalIterations", m_RunParameters.totalIterations);
PlayerPrefs.SetInt("SimWindow/instanceCount", m_RunParameters.instanceCount);
PlayerPrefs.SetInt("SimWindow/sysParamIndex", m_RunParameters.sysParamIndex);
PlayerPrefs.SetString("SimWindow/scenarioConfig",
m_RunParameters.scenarioConfig != null ? m_RunParameters.scenarioConfigAssetPath : string.Empty);
SetFieldsFromPlayerPreferences();
}
struct RunParameters
{
public string runName;
public int totalIterations;
public int instanceCount;
public int sysParamIndex;
public TextAsset scenarioConfig;
public string currentOpenScenePath;
public ScenarioBase currentScenario;
public string scenarioConfigAssetPath => AssetDatabase.GetAssetPath(scenarioConfig);
}
}
}

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


<VisualElement class="sim-window__container-outer">
<Label text="Previous Run Information" class="sim-window__header-1"/>
<Label name="prev-run-name" text="Run Name: " class="sim-window__label-prev-result"/>
<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"/>

正在加载...
取消
保存