浏览代码

Merge branch 'h-i' into generic-asset-sources

/generic-asset-sources
sleal-unity 3 年前
当前提交
2dfadc6a
共有 5 个文件被更改,包括 206 次插入35 次删除
  1. 149
      com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
  2. 13
      com.unity.perception/Editor/Randomization/Utilities/UIElementsEditorUtilities.cs
  3. 8
      com.unity.perception/Editor/Randomization/Uxml/RunInUnitySimulationWindow.uxml
  4. 42
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs
  5. 29
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs

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


RunParameters m_RunParameters;
const string k_SupportedGPUString = "NVIDIA";
EnumField m_BuildTypeMenu;
VisualElement m_BuildSelectControls;
TextField m_BuildPathField;
TextField m_SelectedBuildPathTextField;
TextField m_BuildIdField;
[MenuItem("Window/Run in Unity Simulation")]
static void ShowWindow()
{

}
}
enum BuildIdKind
{
BuildPlayer,
ExistingBuildID,
ExistingBuildZip
}
void CreateRunInUnitySimulationUI()
{
var root = rootVisualElement;

copyPrevRandomSeedButton.clicked += () =>
EditorGUIUtility.systemCopyBuffer = PlayerPrefs.GetString("SimWindow/prevRandomSeed");
m_BuildTypeMenu = root.Q<EnumField>("build-type-menu");
m_BuildTypeMenu.Init(BuildIdKind.BuildPlayer);
m_BuildTypeMenu.RegisterValueChangedCallback(evt => { UpdateBuildIdElements((BuildIdKind) evt.newValue); });
m_BuildSelectControls = root.Q<VisualElement>("build-select-controls");
m_SelectedBuildPathTextField = root.Q<TextField>("selected-build-path");
m_SelectedBuildPathTextField.isReadOnly = true;
m_BuildIdField = root.Q<TextField>("build-id");
UpdateBuildIdElements((BuildIdKind) m_BuildTypeMenu.value);
var selectBuildButton = root.Q<Button>("select-build-file-button");
selectBuildButton.clicked += () =>
{
var path = EditorUtility.OpenFilePanel("Select build ZIP file", "", "zip");
if (path.Length != 0)
{
m_SelectedBuildPathTextField.value = path;
}
};
private void UpdateBuildIdElements(BuildIdKind buildIdKind)
{
switch (buildIdKind)
{
case BuildIdKind.ExistingBuildID:
m_BuildSelectControls.SetEnabled(false);
m_BuildIdField.SetEnabled(true);
break;
case BuildIdKind.ExistingBuildZip:
m_BuildSelectControls.SetEnabled(true);
m_BuildIdField.SetEnabled(false);
break;
case BuildIdKind.BuildPlayer:
m_BuildSelectControls.SetEnabled(false);
m_BuildIdField.SetEnabled(false);
break;
}
}
void SetFieldsFromPlayerPreferences()
{
m_RunNameField.value = IncrementRunName(PlayerPrefs.GetString("SimWindow/runName"));

null);
try
{
m_RunButton.SetEnabled(false);
// Upload build
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
CreateLinuxBuildAndZip();
await StartUnitySimulationRun(runGuid);
string buildId = null;
var buildIdKind = (BuildIdKind)m_BuildTypeMenu.value;
switch (buildIdKind)
{
case BuildIdKind.BuildPlayer:
CreateLinuxBuildAndZip();
buildId = await UploadBuild(cancellationTokenSource, token);
break;
case BuildIdKind.ExistingBuildZip:
m_BuildZipPath = m_BuildPathField.value;
buildId = await UploadBuild(cancellationTokenSource, token);
break;
case BuildIdKind.ExistingBuildID:
buildId = m_BuildIdField.value;
break;
}
if (buildId == null)
return;
await StartUnitySimulationRun(runGuid, buildId);
EditorUtility.ClearProgressBar();
finally
{
m_RunButton.SetEnabled(true);
EditorUtility.ClearProgressBar();
}
}
private async Task<string> UploadBuild(CancellationTokenSource cancellationTokenSource, CancellationToken token)
{
var buildId = await API.UploadBuildAsync(
m_RunParameters.runName,
m_BuildZipPath,
null, null,
cancellationTokenSource,
progress =>
{
EditorUtility.DisplayProgressBar(
"Unity Simulation Run", "Uploading build...", progress * 0.90f);
});
if (token.IsCancellationRequested)
{
Debug.Log("The build upload process has been cancelled. Aborting Unity Simulation launch.");
EditorUtility.ClearProgressBar();
return null;
}
return buildId;
}
void ValidateSettings()

Path.GetExtension(m_RunParameters.scenarioConfigAssetPath) != ".json")
throw new NotSupportedException(
"Scenario configuration must be a JSON text asset");
if (((BuildIdKind)m_BuildTypeMenu.value) == BuildIdKind.ExistingBuildZip &&
!File.Exists(m_SelectedBuildPathTextField.value))
{
throw new NotSupportedException(
"Selected build path does not exist");
}
if (((BuildIdKind)m_BuildTypeMenu.value) == BuildIdKind.ExistingBuildID &&
String.IsNullOrEmpty(m_BuildIdField.value))
{
throw new NotSupportedException("Empty Build ID");
}
}
void CreateLinuxBuildAndZip()

return appParamIds;
}
async Task StartUnitySimulationRun(Guid runGuid)
async Task StartUnitySimulationRun(Guid runGuid, string buildId)
m_RunButton.SetEnabled(false);
// Upload build
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
var buildId = await API.UploadBuildAsync(
m_RunParameters.runName,
m_BuildZipPath,
null, null,
cancellationTokenSource,
progress =>
{
EditorUtility.DisplayProgressBar(
"Unity Simulation Run", "Uploading build...", progress * 0.90f);
});
if (token.IsCancellationRequested)
{
Debug.Log("The build upload process has been cancelled. Aborting Unity Simulation launch.");
EditorUtility.ClearProgressBar();
return;
}
// Generate and upload app-params
EditorUtility.DisplayProgressBar("Unity Simulation Run", "Uploading app-params...", 0.90f);
var appParams = UploadAppParam();

run.Execute();
// Cleanup
m_RunButton.SetEnabled(true);
EditorUtility.ClearProgressBar();
PerceptionEditorAnalytics.ReportRunInUnitySimulationSucceeded(runGuid, run.executionId);
// Set new Player Preferences

13
com.unity.perception/Editor/Randomization/Utilities/UIElementsEditorUtilities.cs


{
var propertyField = new PropertyField(iterator.Copy());
propertyField.Bind(iterator.serializedObject);
var originalField = parentPropertyType.GetField(iterator.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
FieldInfo originalField;
do
{
originalField = parentPropertyType.GetField(iterator.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
parentPropertyType = parentPropertyType.BaseType;
}
while (originalField == null && parentPropertyType != null) ;
if (originalField == null)
return null;
var tooltipAttribute = originalField.GetCustomAttributes(true)
.ToList().Find(att => att.GetType() == typeof(TooltipAttribute));
if (tooltipAttribute != null)

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


tooltip="Selects a scenario JSON configuration to load during the run.
You can leave this option blank to use the scenario settings currently configured in the editor."/>
<editor:EnumField name="build-type-menu" label="Build type" style="flex-grow:1; margin:0; border-width: 1px;"/>
<VisualElement name="build-select-controls" style="margin-bottom: 0; flex-direction: row; justify-content: space-between; align-items: center;">
<TextField name="selected-build-path" label="Build Zip Path" style="flex-grow: 1; flex-shrink: 1;" />
<Button name="select-build-file-button" text="Browse..." style="margin: 0; align-items: flex-end; margin-top: 0; width: 70px;" />
</VisualElement>
<TextField name="build-id" label="Build ID" style="flex-grow: 1; flex-shrink: 1;" />
<VisualElement style="align-items: center; margin-top: 8px;">
<Button name="run-button" text="Build and Run" style="margin: 10px; padding: 2 20; font-size: 13px;"/>
</VisualElement>

42
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs


m_NumObjectsActive[i] = 0;
foreach (var cachedObjectData in m_InstantiatedObjects[i])
{
// Position outside the frame
cachedObjectData.instance.transform.localPosition = new Vector3(10000, 0, 0);
foreach (var tag in cachedObjectData.randomizerTags)
tag.Unregister();
ResetObjectState(cachedObjectData);
}
/// <summary>
/// Returns the given cache object back to an inactive state
/// </summary>
/// <param name="gameObject">The object to make inactive</param>
/// <exception cref="ArgumentException">Thrown when gameObject is not an active cached object.</exception>
public void ResetObject(GameObject gameObject)
{
for (var i = 0; i < m_InstantiatedObjects.Length; ++i)
{
var instantiatedObjectList = m_InstantiatedObjects[i];
int indexFound = -1;
for (var j = 0; j < instantiatedObjectList.Count && indexFound < 0; j++)
{
if (instantiatedObjectList[j].instance == gameObject)
indexFound = j;
}
if (indexFound >= 0)
{
ResetObjectState(instantiatedObjectList[indexFound]);
instantiatedObjectList.RemoveAt(indexFound);
m_NumObjectsActive[i]--;
return;
}
}
throw new ArgumentException("Passed GameObject is not an active object in the cache.");
}
private static void ResetObjectState(CachedObjectData cachedObjectData)
{
// Position outside the frame
cachedObjectData.instance.transform.localPosition = new Vector3(10000, 0, 0);
foreach (var tag in cachedObjectData.randomizerTags)
tag.Unregister();
}
static bool IsPrefab(GameObject obj)

29
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


/// </summary>
public TextAsset configuration;
private bool m_ShouldRestartIteration;
private const int k_MaxIterationStartCount = 100;
/// <summary>
/// Enumerates over all enabled randomizers
/// </summary>

{
ResetRandomStateOnIteration();
OnIterationStart();
foreach (var randomizer in activeRandomizers)
randomizer.IterationStart();
int iterationStartCount = 0;
do
{
m_ShouldRestartIteration = false;
iterationStartCount++;
foreach (var randomizer in activeRandomizers)
{
randomizer.IterationStart();
if (m_ShouldRestartIteration)
break;
}
} while (m_ShouldRestartIteration && iterationStartCount < k_MaxIterationStartCount);
if (m_ShouldRestartIteration)
{
Debug.LogError($"The iteration was restarted {k_MaxIterationStartCount} times. Continuing the scenario to prevent an infinite loop.");
m_ShouldRestartIteration = false;
}
}
// Perform new frame tasks

/// The scenario has finished and is idle
/// </summary>
Idle
}
public void RestartIteration()
{
m_ShouldRestartIteration = true;
}
}
}
正在加载...
取消
保存