浏览代码

added AssetSource class

/generic-asset-sources
sleal-unity 3 年前
当前提交
095aebb1
共有 38 个文件被更改,包括 673 次插入24 次删除
  1. 2
      com.unity.perception/CHANGELOG.md
  2. 5
      com.unity.perception/Editor/Randomization/Utilities/StaticData.cs
  3. 2
      com.unity.perception/Editor/Randomization/Uxml/Sampler/SamplerInterfaceElement.uxml
  4. 14
      com.unity.perception/Editor/Randomization/VisualElements/Parameter/ParameterElement.cs
  5. 15
      com.unity.perception/Editor/Randomization/VisualElements/Sampler/SamplerInterfaceElement.cs
  6. 39
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerializer.cs
  7. 26
      com.unity.perception/Editor/Randomization/PropertyDrawers/AssetSourceDrawer.cs
  8. 11
      com.unity.perception/Editor/Randomization/PropertyDrawers/AssetSourceDrawer.cs.meta
  9. 22
      com.unity.perception/Editor/Randomization/Utilities/AssetLoadingUtilities.cs
  10. 11
      com.unity.perception/Editor/Randomization/Utilities/AssetLoadingUtilities.cs.meta
  11. 8
      com.unity.perception/Editor/Randomization/Uxml/AssetSource.meta
  12. 8
      com.unity.perception/Editor/Randomization/VisualElements/AssetSource.meta
  13. 8
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources.meta
  14. 10
      com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListElement.uxml
  15. 10
      com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListElement.uxml.meta
  16. 7
      com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListItemElement.uxml
  17. 10
      com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListItemElement.uxml.meta
  18. 17
      com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetSourceElement.uxml
  19. 10
      com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetSourceElement.uxml.meta
  20. 94
      com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListElement.cs
  21. 11
      com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListElement.cs.meta
  22. 39
      com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListItemElement.cs
  23. 11
      com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListItemElement.cs.meta
  24. 129
      com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetSourceElement.cs
  25. 11
      com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetSourceElement.cs.meta
  26. 12
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/Archetype.cs
  27. 3
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/Archetype.cs.meta
  28. 11
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/ArchetypeBase.cs
  29. 3
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/ArchetypeBase.cs.meta
  30. 90
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs
  31. 3
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs.meta
  32. 15
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSourceLocation.cs
  33. 3
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSourceLocation.cs.meta
  34. 24
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/LocalAssetSourceLocation.cs
  35. 3
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/LocalAssetSourceLocation.cs.meta

2
com.unity.perception/CHANGELOG.md


Added random seed field to the Run in Unity Simulation Window
Added AssetSource class for loading assets from generic sources inside randomizers
### Changed
Increased color variety in instance segmentation images

5
com.unity.perception/Editor/Randomization/Utilities/StaticData.cs


using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine.Perception.Randomization;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;

internal static Type[] randomizerTypes;
internal static Type[] samplerTypes;
internal static Type[] assetSourceLocationTypes;
internal static Type[] archetypeTypes;
assetSourceLocationTypes = GetConstructableDerivedTypes<AssetSourceLocation>();
archetypeTypes = GetConstructableDerivedTypes<ArchetypeBase>();
}
static Type[] GetConstructableDerivedTypes<T>()

2
com.unity.perception/Editor/Randomization/Uxml/Sampler/SamplerInterfaceElement.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement name="sampler-template" style="margin-bottom: 4px;">
<VisualElement style="margin-bottom: 4px;">
<Style src="../../Uss/Styles.uss"/>
<VisualElement style="flex-direction: row; align-items: center;">
<Label name="sampler-name" text="Sampler Name" class="unity-base-field__label"/>

14
com.unity.perception/Editor/Randomization/VisualElements/Parameter/ParameterElement.cs


"Add Options From Folder", Application.dataPath, string.Empty);
if (folderPath == string.Empty)
return;
var categories = LoadAssetsFromFolder(folderPath, categoricalParameter.sampleType);
var categories = AssetLoadingUtilities.LoadAssetsFromFolder(folderPath, categoricalParameter.sampleType);
var optionsIndex = optionsProperty.arraySize;
optionsProperty.arraySize += categories.Count;

});
m_PropertiesContainer.Add(template);
}
static List<Object> LoadAssetsFromFolder(string folderPath, Type assetType)
{
if (!folderPath.StartsWith(Application.dataPath))
throw new ApplicationException("Selected folder is not an asset folder in this project");
var assetsPath = "Assets" + folderPath.Remove(0, Application.dataPath.Length);
var assetIds = AssetDatabase.FindAssets($"t:{assetType.Name}", new[] { assetsPath });
var assets = new List<Object>();
foreach (var guid in assetIds)
assets.Add(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), assetType));
return assets;
}
}
}

15
com.unity.perception/Editor/Randomization/VisualElements/Sampler/SamplerInterfaceElement.cs


SerializedProperty m_RangeProperty;
ToolbarMenu m_SamplerTypeDropdown;
ISampler sampler => (ISampler)StaticData.GetManagedReferenceValue(m_Property);
public SamplerInterfaceElement(SerializedProperty property)
{
m_Property = property;

CreateSampler(typeof(UniformSampler));
var samplerName = this.Q<Label>("sampler-name");
samplerName.text = UppercaseFirstLetter(m_Property.name);
samplerName.text = m_Property.displayName;
;
foreach (var samplerType in StaticData.samplerTypes)
{
var displayName = SamplerUtility.GetSamplerDisplayName(samplerType);

a => { ReplaceSampler(samplerType); },
a => ReplaceSampler(samplerType),
ISampler sampler => (ISampler)StaticData.GetManagedReferenceValue(m_Property);
void ReplaceSampler(Type samplerType)
{

m_RangeProperty = null;
m_PropertiesContainer.Clear();
UIElementsEditorUtilities.CreatePropertyFields(m_Property, m_PropertiesContainer);
}
static string UppercaseFirstLetter(string s)
{
return string.IsNullOrEmpty(s) ? string.Empty : char.ToUpper(s[0]) + s.Substring(1);
}
}
}

39
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerializer.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

{
return new JObject
{
["archetypes"] = SerializeArchetypes(scenario.randomizers),
}
static JArray SerializeArchetypes(IReadOnlyList<Randomizer> randomizers)
{
var archetypesObj = new JArray();
var archetypeLabels = new HashSet<string>();
foreach (var randomizer in randomizers)
{
var assetSourceFields = randomizer.GetType().GetFields();
foreach (var field in assetSourceFields)
{
if (!IsSubclassOfRawGeneric(typeof(AssetSource<>), field.FieldType))
continue;
var assetSource = field.GetValue(randomizer);
if (assetSource == null)
continue;
var archetypeField = assetSource.GetType().GetField(
"m_ArchetypeBase", BindingFlags.NonPublic | BindingFlags.Instance);
var archetype = (ArchetypeBase)archetypeField.GetValue(assetSource);
if (archetype == null || archetypeLabels.Contains(archetype.label))
continue;
archetypeLabels.Add(archetype.label);
}
}
var labelsList = archetypeLabels.ToList();
labelsList.Sort();
foreach (var label in labelsList)
{
var archetypeObj = new JObject
{
new JProperty("name", label),
new JProperty("description", string.Empty)
};
archetypesObj.Add(archetypeObj);
}
return archetypesObj;
}
static JObject SerializeConstants(ScenarioConstants constants)

26
com.unity.perception/Editor/Randomization/PropertyDrawers/AssetSourceDrawer.cs


using Editor.Randomization.VisualElements.AssetSource;
using UnityEngine;
using UnityEngine.Perception.Randomization;
using UnityEngine.UIElements;
namespace UnityEditor.Perception.Randomization.PropertyDrawers
{
[CustomPropertyDrawer(typeof(AssetSource<>))]
public class AssetSourceDrawer : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
return new AssetSourceElement(property, fieldInfo);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.PropertyField(position, property, label, true);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property);
}
}
}

11
com.unity.perception/Editor/Randomization/PropertyDrawers/AssetSourceDrawer.cs.meta


fileFormatVersion: 2
guid: 5be7d531a18f32b4f93a73b173259337
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

22
com.unity.perception/Editor/Randomization/Utilities/AssetLoadingUtilities.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
namespace UnityEditor.Perception.Randomization
{
static class AssetLoadingUtilities
{
public static List<Object> LoadAssetsFromFolder(string folderPath, Type assetType)
{
if (!folderPath.StartsWith(Application.dataPath))
throw new ApplicationException("Selected folder is not an asset folder in this project");
var assetsPath = "Assets" + folderPath.Remove(0, Application.dataPath.Length);
var assetIds = AssetDatabase.FindAssets($"t:{assetType.Name}", new[] { assetsPath });
var assets = new List<Object>();
foreach (var guid in assetIds)
assets.Add(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), assetType));
return assets;
}
}
}

11
com.unity.perception/Editor/Randomization/Utilities/AssetLoadingUtilities.cs.meta


fileFormatVersion: 2
guid: 30b11971d353ad648b466b3f7763859e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
com.unity.perception/Editor/Randomization/Uxml/AssetSource.meta


fileFormatVersion: 2
guid: 7e7eabbe9ff71e64291989566ea6ec83
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
com.unity.perception/Editor/Randomization/VisualElements/AssetSource.meta


fileFormatVersion: 2
guid: 29370b0293eb9a046b454ffdd7b1c372
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources.meta


fileFormatVersion: 2
guid: c3b723ce8e6a50141834747972131766
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

10
com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListElement.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement class="parameter__categorical-options-list">
<ListView name="assets"/>
</VisualElement>
<VisualElement style="flex-direction: row; justify-content: flex-end;">
<Button name="add-asset" text="Add Asset" class="parameter__categorical-options-list-button"/>
<Button name="add-folder" text="Add Folder" class="parameter__categorical-options-list-button"/>
<Button name="clear-assets" text="Clear Assets" class="parameter__categorical-options-list-button"/>
</VisualElement>
</UXML>

10
com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListElement.uxml.meta


fileFormatVersion: 2
guid: e4915dfbbc1de39478264a8f5d168ca0
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}

7
com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListItemElement.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement class="parameter__categorical-option">
<Button name="remove" class="randomization__remove-item-button"/>
<Label name="index-label" text="[0]" style="min-width: 50px;"/>
<editor:ObjectField name="item" class="parameter__categorical-option-property-field"/>
</VisualElement>
</UXML>

10
com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListItemElement.uxml.meta


fileFormatVersion: 2
guid: d710875a5224e1b4186c4334f791ca19
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}

17
com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetSourceElement.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement style="margin-bottom: 4px;">
<Style src="../../Uss/Styles.uss"/>
<Label name="name" text="Field Name" class="unity-base-field__label"/>
<VisualElement style="margin-left: 18px;">
<VisualElement style="flex-direction: row; align-items: center;">
<Label text="Archetype" class="unity-base-field__label"/>
<editor:ToolbarMenu name="archetype-dropdown" text="Type" class="sampler__type-menu"/>
</VisualElement>
<VisualElement style="flex-direction: row; align-items: center;">
<Label text="Location" class="unity-base-field__label"/>
<editor:ToolbarMenu name="location-dropdown" text="Type" class="sampler__type-menu"/>
</VisualElement>
<VisualElement name="fields-container" style="flex-grow: 1;"/>
</VisualElement>
</VisualElement>
</UXML>

10
com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetSourceElement.uxml.meta


fileFormatVersion: 2
guid: e6fba9b826939224fac37b7ead85df2a
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}

94
com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListElement.cs


using System;
using System.Collections;
using UnityEditor;
using UnityEditor.Perception.Randomization;
using UnityEngine;
using UnityEngine.UIElements;
namespace Editor.Randomization.VisualElements.AssetSource
{
public class AssetListElement : VisualElement
{
SerializedProperty m_Property;
IList list => (IList)StaticData.GetManagedReferenceValue(m_Property);
public AssetListElement(SerializedProperty property, Type itemType)
{
m_Property = property;
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/AssetSource/AssetListElement.uxml");
template.CloneTree(this);
var listView = this.Q<ListView>("assets");
listView.itemsSource = list;
listView.itemHeight = 22;
listView.selectionType = SelectionType.None;
listView.style.flexGrow = 1.0f;
listView.style.height = new StyleLength(listView.itemHeight * 4);
listView.makeItem = () =>
{
return new AssetListItemElement(m_Property, itemType);
};
listView.bindItem = (element, i) =>
{
var optionElement = (AssetListItemElement)element;
optionElement.BindProperties(i);
var removeButton = optionElement.Q<Button>("remove");
removeButton.clicked += () =>
{
// First delete sets option to null, second delete removes option
var numOptions = m_Property.arraySize;
m_Property.DeleteArrayElementAtIndex(i);
if (numOptions == m_Property.arraySize)
m_Property.DeleteArrayElementAtIndex(i);
m_Property.serializedObject.ApplyModifiedProperties();
listView.itemsSource = list;
listView.Refresh();
};
};
var addOptionButton = this.Q<Button>("add-asset");
addOptionButton.clicked += () =>
{
m_Property.arraySize++;
m_Property.serializedObject.ApplyModifiedProperties();
listView.itemsSource = list;
listView.Refresh();
listView.ScrollToItem(m_Property.arraySize);
};
var addFolderButton = this.Q<Button>("add-folder");
addFolderButton.clicked += () =>
{
var folderPath = EditorUtility.OpenFolderPanel(
"Add Assets From Folder", Application.dataPath, string.Empty);
if (folderPath == string.Empty)
return;
var assets = AssetLoadingUtilities.LoadAssetsFromFolder(folderPath, itemType);
var optionsIndex = m_Property.arraySize;
m_Property.arraySize += assets.Count;
for (var i = 0; i < assets.Count; i++)
{
var optionProperty = m_Property.GetArrayElementAtIndex(optionsIndex + i);
optionProperty.objectReferenceValue = assets[i];
m_Property.GetArrayElementAtIndex(i).floatValue = 0;
}
m_Property.serializedObject.ApplyModifiedProperties();
listView.itemsSource = list;
listView.Refresh();
};
var clearOptionsButton = this.Q<Button>("clear-assets");
clearOptionsButton.clicked += () =>
{
m_Property.arraySize = 0;
m_Property.serializedObject.ApplyModifiedProperties();
listView.itemsSource = list;
listView.Refresh();
};
}
}
}

11
com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListElement.cs.meta


fileFormatVersion: 2
guid: 7c9fc5cac7e306247a669a3f33479736
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

39
com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListItemElement.cs


using System;
using UnityEditor;
using UnityEditor.Perception.Randomization;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace Editor.Randomization.VisualElements.AssetSource
{
class AssetListItemElement : VisualElement
{
int m_Index;
Type m_ItemType;
SerializedProperty m_Property;
public AssetListItemElement(SerializedProperty property, Type itemType)
{
m_Property = property;
m_ItemType = itemType;
}
public void BindProperties(int i)
{
Clear();
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/AssetSource/{nameof(AssetListItemElement)}.uxml");
template.CloneTree(this);
m_Index = i;
var indexLabel = this.Q<Label>("index-label");
indexLabel.text = $"[{m_Index}]";
var optionProperty = m_Property.GetArrayElementAtIndex(i);
var option = this.Q<ObjectField>("item");
option.BindProperty(optionProperty);
option.objectType = m_ItemType;
option.allowSceneObjects = false;
}
}
}

11
com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListItemElement.cs.meta


fileFormatVersion: 2
guid: 5fd946360906cd5449f8d54f3c65e41c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

129
com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetSourceElement.cs


using System;
using System.ComponentModel;
using System.Reflection;
using UnityEditor;
using UnityEditor.Perception.Randomization;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Perception.Randomization;
using UnityEngine.UIElements;
namespace Editor.Randomization.VisualElements.AssetSource
{
class AssetSourceElement : VisualElement
{
SerializedProperty m_ArchetypeProperty;
SerializedProperty m_LocationProperty;
ToolbarMenu m_ArchetypeToolbarMenu;
ToolbarMenu m_LocationToolbarMenu;
VisualElement m_FieldsContainer;
Type m_AssetType;
ArchetypeBase archetype =>
(ArchetypeBase)StaticData.GetManagedReferenceValue(m_ArchetypeProperty);
AssetSourceLocation assetSourceLocation =>
(AssetSourceLocation)StaticData.GetManagedReferenceValue(m_LocationProperty);
public AssetSourceElement(SerializedProperty property, FieldInfo fieldInfo)
{
m_AssetType = fieldInfo.FieldType.GetGenericArguments()[0];
m_ArchetypeProperty = property.FindPropertyRelative("m_ArchetypeBase");
m_LocationProperty = property.FindPropertyRelative(nameof(AssetSource<GameObject>.assetSourceLocation));
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/AssetSource/AssetSourceElement.uxml");
template.CloneTree(this);
if (assetSourceLocation == null)
CreateAssetSourceLocation(typeof(LocalAssetSourceLocation));
var nameLabel = this.Q<Label>("name");
nameLabel.text = property.displayName;
m_FieldsContainer = this.Q<VisualElement>("fields-container");
m_ArchetypeToolbarMenu = this.Q<ToolbarMenu>("archetype-dropdown");
var storedArchetype = archetype;
m_ArchetypeToolbarMenu.text = storedArchetype != null
? GetArchetypeDisplayName(archetype.GetType()) : "None";
// ReSharper disable once PossibleNullReferenceException
var baseType = fieldInfo.FieldType.GetProperty("archetype").PropertyType;
m_ArchetypeToolbarMenu.menu.AppendAction(
"None",
a => ReplaceArchetype(null),
a => DropdownMenuAction.Status.Normal);
foreach (var type in StaticData.archetypeTypes)
{
if (!type.IsSubclassOf(baseType))
continue;
m_ArchetypeToolbarMenu.menu.AppendAction(
GetArchetypeDisplayName(type),
a => ReplaceArchetype(type),
a => DropdownMenuAction.Status.Normal);
}
m_LocationToolbarMenu = this.Q<ToolbarMenu>("location-dropdown");
m_LocationToolbarMenu.text = GetDisplayName(assetSourceLocation.GetType());
foreach (var type in StaticData.assetSourceLocationTypes)
{
m_LocationToolbarMenu.menu.AppendAction(
GetDisplayName(type),
a => ReplaceLocation(type),
a => DropdownMenuAction.Status.Normal);
}
CreatePropertyFields();
}
void ReplaceLocation(Type type)
{
CreateAssetSourceLocation(type);
m_LocationToolbarMenu.text = GetDisplayName(type);
CreatePropertyFields();
}
void ReplaceArchetype(Type type)
{
if (type == null)
{
m_ArchetypeToolbarMenu.text = "None";
m_ArchetypeProperty.managedReferenceValue = null;
}
else
{
m_ArchetypeToolbarMenu.text = GetDisplayName(type);
var newArchetype = (ArchetypeBase)Activator.CreateInstance(type);
m_ArchetypeProperty.managedReferenceValue = newArchetype;
}
m_ArchetypeProperty.serializedObject.ApplyModifiedProperties();
}
void CreateAssetSourceLocation(Type type)
{
var newLocation = (AssetSourceLocation)Activator.CreateInstance(type);
m_LocationProperty.managedReferenceValue = newLocation;
m_LocationProperty.serializedObject.ApplyModifiedProperties();
}
void CreatePropertyFields()
{
m_FieldsContainer.Clear();
if (m_LocationProperty.type == $"managedReference<{nameof(LocalAssetSourceLocation)}>")
m_FieldsContainer.Add(
new AssetListElement(m_LocationProperty.FindPropertyRelative("assets"), m_AssetType));
else
UIElementsEditorUtilities.CreatePropertyFields(m_LocationProperty, m_FieldsContainer);
}
static string GetDisplayName(Type type)
{
var attribute = (DisplayNameAttribute)Attribute.GetCustomAttribute(type, typeof(DisplayNameAttribute));
return attribute != null ? attribute.DisplayName : type.Name;
}
static string GetArchetypeDisplayName(Type type)
{
return type.Name.Replace("Archetype", string.Empty);
}
}
}

11
com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetSourceElement.cs.meta


fileFormatVersion: 2
guid: 43a7574956bc88b45a6597e2c94c9cb6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/Archetype.cs


namespace UnityEngine.Perception.Randomization
{
public abstract class Archetype<T> : ArchetypeBase where T : Object
{
public abstract void Preprocess(T item);
public override void PreprocessAsset(Object asset)
{
Preprocess((T)asset);
}
}
}

3
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/Archetype.cs.meta


fileFormatVersion: 2
guid: ffdc828eccff45728455a572d1f2b0e3
timeCreated: 1618978426

11
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/ArchetypeBase.cs


using UnityEngine;
namespace UnityEngine.Perception.Randomization
{
public abstract class ArchetypeBase
{
public abstract string label { get; }
public abstract void PreprocessAsset(Object asset);
}
}

3
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/ArchetypeBase.cs.meta


fileFormatVersion: 2
guid: 88c34672b8a649439c1fd55bc201acde
timeCreated: 1619731337

90
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs


using System;
using UnityEngine.Perception.Randomization.Samplers;
namespace UnityEngine.Perception.Randomization
{
[Serializable]
public sealed class AssetSource<T> where T : Object
{
bool m_Initialized;
UniformSampler m_Sampler = new UniformSampler();
[SerializeReference] ArchetypeBase m_ArchetypeBase;
[SerializeReference] public AssetSourceLocation assetSourceLocation = new LocalAssetSourceLocation();
public Archetype<T> archetype
{
get => (Archetype<T>)m_ArchetypeBase;
set => m_ArchetypeBase = value;
}
public int Count => assetSourceLocation.Count;
public void Initialize()
{
assetSourceLocation.Initialize(archetype);
m_Initialized = true;
}
public T GetAsset(int index)
{
CheckIfInitialized();
return assetSourceLocation.GetAsset<T>(index);
}
public T[] GetAssets()
{
var array = new T[Count];
for (var i = 0; i < Count; i++)
array[i] = GetAsset(i);
return array;
}
public T GetInstance(int index)
{
CheckIfInitialized();
return CreateInstance(GetAsset(index));
}
public T[] GetInstances()
{
var array = new T[Count];
for (var i = 0; i < Count; i++)
array[i] = GetInstance(i);
return array;
}
public T SampleAsset()
{
CheckIfInitialized();
return assetSourceLocation.GetAsset<T>((int)(m_Sampler.Sample() * Count));
}
public T SampleInstance()
{
CheckIfInitialized();
return CreateInstance(SampleAsset());
}
public void ReleaseAssets()
{
CheckIfInitialized();
assetSourceLocation.ReleaseAssets();
}
void CheckIfInitialized()
{
if (!m_Initialized)
throw new InvalidOperationException(
"Initialize() must be called on this AssetSource before executing this operation");
}
T CreateInstance(T asset)
{
var instance = Object.Instantiate(asset);
if (archetype != null)
archetype.Preprocess(instance);
return instance;
}
}
}

3
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs.meta


fileFormatVersion: 2
guid: 27aa08b82cf94015adc22309c8f3bc48
timeCreated: 1618977151

15
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSourceLocation.cs


using UnityEngine;
namespace UnityEngine.Perception.Randomization
{
public abstract class AssetSourceLocation
{
public abstract int Count { get; }
public abstract void Initialize<T>(Archetype<T> archetype) where T : Object;
public abstract void ReleaseAssets();
public abstract T GetAsset<T>(int index) where T : Object;
}
}

3
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSourceLocation.cs.meta


fileFormatVersion: 2
guid: 9f2e99decdb94c7c898d7702e10cbce5
timeCreated: 1619732135

24
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/LocalAssetSourceLocation.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace UnityEngine.Perception.Randomization
{
[Serializable]
[DisplayName("Local")]
public class LocalAssetSourceLocation : AssetSourceLocation
{
[SerializeField] public List<Object> assets;
public override int Count => assets.Count;
public override void Initialize<T>(Archetype<T> archetype) { }
public override void ReleaseAssets() { }
public override T GetAsset<T>(int index)
{
return (T)assets[index];
}
}
}

3
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/LocalAssetSourceLocation.cs.meta


fileFormatVersion: 2
guid: 20f161f5e56247c280fc1b56b567f49a
timeCreated: 1618983500
正在加载...
取消
保存