浏览代码

renamed Archetype to AssetRole

/generic-asset-sources
sleal-unity 4 年前
当前提交
afb1a5f9
共有 20 个文件被更改,包括 186 次插入120 次删除
  1. 4
      com.unity.perception/Editor/Randomization/Utilities/StaticData.cs
  2. 6
      com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetSourceElement.uxml
  3. 50
      com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetSourceElement.cs
  4. 41
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs
  5. 4
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSourceLocation.cs
  6. 2
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/LocalAssetSourceLocation.cs
  7. 38
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerializer.cs
  8. 1
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/SampleScenarioConfiguration.json
  9. 16
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetRole.cs
  10. 11
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetRole.cs.meta
  11. 15
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetRoleBase.cs
  12. 11
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetRoleBase.cs.meta
  13. 8
      com.unity.perception/Tests/Runtime/Randomization/AssetSourceTests.meta
  14. 51
      com.unity.perception/Tests/Runtime/Randomization/AssetSourceTests/LocalAssetSourceTests.cs
  15. 11
      com.unity.perception/Tests/Runtime/Randomization/AssetSourceTests/LocalAssetSourceTests.cs.meta
  16. 16
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/Archetype.cs
  17. 3
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/Archetype.cs.meta
  18. 15
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/ArchetypeBase.cs
  19. 3
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/ArchetypeBase.cs.meta

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


internal static Type[] randomizerTypes;
internal static Type[] samplerTypes;
internal static Type[] assetSourceLocationTypes;
internal static Type[] archetypeTypes;
internal static Type[] assetRoleTypes;
static StaticData()
{

archetypeTypes = GetConstructableDerivedTypes<ArchetypeBase>();
assetRoleTypes = GetConstructableDerivedTypes<AssetRoleBase>();
}
static Type[] GetConstructableDerivedTypes<T>()

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


</VisualElement>
<VisualElement style="flex-direction: row; align-items: center;">
<Label
text="Archetype"
text="Asset Role"
tooltip="Archetypes preprocess assets loaded from the selected asset source location"/>
<editor:ToolbarMenu name="archetype-dropdown" text="Type" class="sampler__type-menu"/>
tooltip="Asset Roles preprocess assets loaded from the selected asset source location"/>
<editor:ToolbarMenu name="asset-role-dropdown" text="Type" class="sampler__type-menu"/>
</VisualElement>
</VisualElement>
</VisualElement>

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


{
class AssetSourceElement : VisualElement
{
SerializedProperty m_ArchetypeProperty;
SerializedProperty m_AssetRoleProperty;
ToolbarMenu m_ArchetypeToolbarMenu;
ToolbarMenu m_AssetRoleToolbarMenu;
ArchetypeBase archetype =>
(ArchetypeBase)StaticData.GetManagedReferenceValue(m_ArchetypeProperty);
AssetRoleBase assetRole =>
(AssetRoleBase)StaticData.GetManagedReferenceValue(m_AssetRoleProperty);
AssetSourceLocation assetSourceLocation =>
(AssetSourceLocation)StaticData.GetManagedReferenceValue(m_LocationProperty);

m_ArchetypeProperty = property.FindPropertyRelative("m_ArchetypeBase");
m_AssetRoleProperty = property.FindPropertyRelative("m_AssetRoleBase");
m_LocationProperty = property.FindPropertyRelative(nameof(AssetSource<GameObject>.assetSourceLocation));
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/AssetSource/AssetSourceElement.uxml");

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";
m_AssetRoleToolbarMenu = this.Q<ToolbarMenu>("asset-role-dropdown");
var storedAssetRole = assetRole;
m_AssetRoleToolbarMenu.text = storedAssetRole != null
? GetAssetRoleDisplayName(assetRole.GetType()) : "None";
var baseType = fieldInfo.FieldType.GetProperty("archetype").PropertyType;
m_ArchetypeToolbarMenu.menu.AppendAction(
var baseType = fieldInfo.FieldType.GetProperty("assetRole").PropertyType;
m_AssetRoleToolbarMenu.menu.AppendAction(
a => ReplaceArchetype(null),
a => ReplaceAssetRole(null),
foreach (var type in StaticData.archetypeTypes)
foreach (var type in StaticData.assetRoleTypes)
m_ArchetypeToolbarMenu.menu.AppendAction(
GetArchetypeDisplayName(type),
a => ReplaceArchetype(type),
m_AssetRoleToolbarMenu.menu.AppendAction(
GetAssetRoleDisplayName(type),
a => ReplaceAssetRole(type),
a => DropdownMenuAction.Status.Normal);
}

UpdateLocationUI(assetSourceLocation.GetType());
}
void ReplaceArchetype(Type type)
void ReplaceAssetRole(Type type)
m_ArchetypeToolbarMenu.text = "None";
m_ArchetypeProperty.managedReferenceValue = null;
m_AssetRoleToolbarMenu.text = "None";
m_AssetRoleProperty.managedReferenceValue = null;
m_ArchetypeToolbarMenu.text = GetDisplayName(type);
var newArchetype = (ArchetypeBase)Activator.CreateInstance(type);
m_ArchetypeProperty.managedReferenceValue = newArchetype;
m_AssetRoleToolbarMenu.text = GetDisplayName(type);
var newAssetRole = (AssetRoleBase)Activator.CreateInstance(type);
m_AssetRoleProperty.managedReferenceValue = newAssetRole;
m_ArchetypeProperty.serializedObject.ApplyModifiedProperties();
m_AssetRoleProperty.serializedObject.ApplyModifiedProperties();
}
void CreateAssetSourceLocation(Type type)

return attribute != null ? attribute.DisplayName : type.Name;
}
static string GetArchetypeDisplayName(Type type)
static string GetAssetRoleDisplayName(Type type)
return type.Name.Replace("Archetype", string.Empty);
return type.Name.Replace("AssetRole", string.Empty);
}
}
}

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


[Serializable]
public sealed class AssetSource<T> where T : Object
{
[SerializeReference] ArchetypeBase m_ArchetypeBase;
[SerializeReference] AssetRoleBase m_AssetRoleBase;
/// <summary>
/// The location to load assets from

bool m_Initialized;
UniformSampler m_Sampler = new UniformSampler();
/// The archetype used to preprocess assets from this source
/// The asset role used to preprocess assets from this source
public Archetype<T> archetype
public AssetRole<T> assetRole
get => (Archetype<T>)m_ArchetypeBase;
set => m_ArchetypeBase = value;
get => (AssetRole<T>)m_AssetRoleBase;
set => m_AssetRoleBase = value;
}
/// <summary>

{
if (!m_Initialized)
{
assetSourceLocation.Initialize(archetype);
assetSourceLocation.Initialize(assetRole);
m_Initialized = true;
}
}

public T LoadRawAsset(int index)
{
CheckIfInitialized();
if (count == 0)
return null;
return assetSourceLocation.LoadAsset<T>(index);
}

}
/// <summary>
/// Creates an instance of the asset loaded from the provided index and preprocesses it using the archetype
/// Creates an instance of the asset loaded from the provided index and preprocesses it using the asset role
/// assigned to this asset source
/// </summary>
/// <param name="index">The index of the asset to load</param>

}
/// <summary>
/// Returns a uniformly random sampled asset from this AssetSource
/// </summary>
/// <returns>The randomly sampled asset</returns>
public T SampleAsset()
{
CheckIfInitialized();
return assetSourceLocation.LoadAsset<T>((int)(m_Sampler.Sample() * count));
}
/// <summary>
/// Instantiates and preprocesses a uniformly random sampled asset from this AssetSource
/// </summary>
/// <returns>The generated random instance</returns>
public T SampleInstance()
{
CheckIfInitialized();
return CreateProcessedInstance(SampleAsset());
}
/// <summary>
/// Unloads all assets that have been loaded from this AssetSource
/// </summary>
public void ReleaseAssets()

return null;
var instance = Object.Instantiate(asset);
if (archetype != null)
archetype.Preprocess(instance);
if (assetRole != null)
assetRole.Preprocess(instance);
return instance;
}
}

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


/// <summary>
/// Execute setup steps before accessing assets at this location
/// </summary>
/// <param name="archetype">The archetype that will be used to preprocess assets from this location</param>
/// <param name="assetRole">The asset role that will be used to preprocess assets from this location</param>
public abstract void Initialize<T>(Archetype<T> archetype) where T : Object;
public abstract void Initialize<T>(AssetRole<T> assetRole) where T : Object;
/// <summary>
/// Unload all assets loaded from this location

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


public override int count => assets.Count;
/// <inheritdoc/>
public override void Initialize<T>(Archetype<T> archetype) { }
public override void Initialize<T>(AssetRole<T> assetRole) { }
/// <inheritdoc/>
public override void ReleaseAssets() { }

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


{
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)

1
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/SampleScenarioConfiguration.json


{
"archetypes": [],
"constants": {
"framesPerIteration": 1,
"totalIterations": 100,

16
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetRole.cs


namespace UnityEngine.Perception.Randomization
{
/// <summary>
/// Derive this class to create a typed asset role.
/// Typed asset roles are used to apply preprocessing steps to assets loaded from an <see cref="AssetSource{T}"/>.
/// </summary>
/// <typeparam name="T">The type of asset to preprocess</typeparam>
public abstract class AssetRole<T> : AssetRoleBase where T : Object
{
/// <summary>
/// Perform preprocessing operations on an asset loaded from an <see cref="AssetSource{T}"/>.
/// </summary>
/// <param name="asset">The asset to preprocess</param>
public abstract void Preprocess(T asset);
}
}

11
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetRole.cs.meta


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

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


using UnityEngine;
namespace UnityEngine.Perception.Randomization
{
/// <summary>
/// The base asset role class. Derive from <see cref="AssetRole{T}"/> instead to create a new asset role.
/// </summary>
public abstract class AssetRoleBase
{
/// <summary>
/// The string label uniquely associated with this asset role
/// </summary>
public abstract string label { get; }
}
}

11
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetRoleBase.cs.meta


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

8
com.unity.perception/Tests/Runtime/Randomization/AssetSourceTests.meta


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

51
com.unity.perception/Tests/Runtime/Randomization/AssetSourceTests/LocalAssetSourceTests.cs


using NUnit.Framework;
using UnityEngine;
using UnityEngine.Perception.Randomization;
namespace RandomizationTests.AssetSourceTests
{
[TestFixture]
public class LocalAssetSourceTests
{
GameObject m_TestObject;
TestBehaviour m_Behaviour;
class TestAssetRole : AssetRole<GameObject>
{
public override string label => "test";
public override void Preprocess(GameObject asset)
{
throw new System.NotImplementedException();
}
}
class TestBehaviour : MonoBehaviour
{
public AssetSource<GameObject> gameObjectSource = new AssetSource<GameObject>
{
assetRole = null,
assetSourceLocation = new LocalAssetSourceLocation()
};
public AssetSource<Material> materialSource = new AssetSource<Material>
{
assetRole = null,
assetSourceLocation = new LocalAssetSourceLocation()
};
}
[SetUp]
public void Setup()
{
m_TestObject = new GameObject();
m_Behaviour = m_TestObject.AddComponent<TestBehaviour>();
}
[TearDown]
public void TearDown()
{
Object.DestroyImmediate(m_TestObject);
}
}
}

11
com.unity.perception/Tests/Runtime/Randomization/AssetSourceTests/LocalAssetSourceTests.cs.meta


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

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


namespace UnityEngine.Perception.Randomization
{
/// <summary>
/// Derive this class to create a typed Archetype.
/// Typed Archetypes are used to apply preprocessing steps to assets loaded from an <see cref="AssetSource{T}"/>.
/// </summary>
/// <typeparam name="T">The type of asset to preprocess</typeparam>
public abstract class Archetype<T> : ArchetypeBase where T : Object
{
/// <summary>
/// Perform preprocessing operations on an asset loaded from an <see cref="AssetSource{T}"/>.
/// </summary>
/// <param name="asset">The asset to preprocess</param>
public abstract void Preprocess(T asset);
}
}

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


fileFormatVersion: 2
guid: ffdc828eccff45728455a572d1f2b0e3
timeCreated: 1618978426

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


using UnityEngine;
namespace UnityEngine.Perception.Randomization
{
/// <summary>
/// The base Archetype class. Derive from <see cref="AssetSource{T}"/> instead to create a new archetype.
/// </summary>
public abstract class ArchetypeBase
{
/// <summary>
/// The string label uniquely associated with this archetype
/// </summary>
public abstract string label { get; }
}
}

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


fileFormatVersion: 2
guid: 88c34672b8a649439c1fd55bc201acde
timeCreated: 1619731337
正在加载...
取消
保存