sleal-unity
3 年前
当前提交
095aebb1
共有 38 个文件被更改,包括 673 次插入 和 24 次删除
-
2com.unity.perception/CHANGELOG.md
-
5com.unity.perception/Editor/Randomization/Utilities/StaticData.cs
-
2com.unity.perception/Editor/Randomization/Uxml/Sampler/SamplerInterfaceElement.uxml
-
14com.unity.perception/Editor/Randomization/VisualElements/Parameter/ParameterElement.cs
-
15com.unity.perception/Editor/Randomization/VisualElements/Sampler/SamplerInterfaceElement.cs
-
39com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerializer.cs
-
26com.unity.perception/Editor/Randomization/PropertyDrawers/AssetSourceDrawer.cs
-
11com.unity.perception/Editor/Randomization/PropertyDrawers/AssetSourceDrawer.cs.meta
-
22com.unity.perception/Editor/Randomization/Utilities/AssetLoadingUtilities.cs
-
11com.unity.perception/Editor/Randomization/Utilities/AssetLoadingUtilities.cs.meta
-
8com.unity.perception/Editor/Randomization/Uxml/AssetSource.meta
-
8com.unity.perception/Editor/Randomization/VisualElements/AssetSource.meta
-
8com.unity.perception/Runtime/Randomization/Randomizers/AssetSources.meta
-
10com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListElement.uxml
-
10com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListElement.uxml.meta
-
7com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListItemElement.uxml
-
10com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetListItemElement.uxml.meta
-
17com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetSourceElement.uxml
-
10com.unity.perception/Editor/Randomization/Uxml/AssetSource/AssetSourceElement.uxml.meta
-
94com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListElement.cs
-
11com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListElement.cs.meta
-
39com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListItemElement.cs
-
11com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetListItemElement.cs.meta
-
129com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetSourceElement.cs
-
11com.unity.perception/Editor/Randomization/VisualElements/AssetSource/AssetSourceElement.cs.meta
-
12com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/Archetype.cs
-
3com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/Archetype.cs.meta
-
11com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/ArchetypeBase.cs
-
3com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/ArchetypeBase.cs.meta
-
90com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs
-
3com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs.meta
-
15com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSourceLocation.cs
-
3com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSourceLocation.cs.meta
-
24com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/LocalAssetSourceLocation.cs
-
3com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/LocalAssetSourceLocation.cs.meta
|
|||
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); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5be7d531a18f32b4f93a73b173259337 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 30b11971d353ad648b466b3f7763859e |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 7e7eabbe9ff71e64291989566ea6ec83 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 29370b0293eb9a046b454ffdd7b1c372 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: c3b723ce8e6a50141834747972131766 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
<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> |
|
|||
fileFormatVersion: 2 |
|||
guid: e4915dfbbc1de39478264a8f5d168ca0 |
|||
ScriptedImporter: |
|||
internalIDToNameTable: [] |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|||
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} |
|
|||
<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> |
|
|||
fileFormatVersion: 2 |
|||
guid: d710875a5224e1b4186c4334f791ca19 |
|||
ScriptedImporter: |
|||
internalIDToNameTable: [] |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|||
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} |
|
|||
<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> |
|
|||
fileFormatVersion: 2 |
|||
guid: e6fba9b826939224fac37b7ead85df2a |
|||
ScriptedImporter: |
|||
internalIDToNameTable: [] |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|||
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} |
|
|||
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(); |
|||
}; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 7c9fc5cac7e306247a669a3f33479736 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5fd946360906cd5449f8d54f3c65e41c |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 43a7574956bc88b45a6597e2c94c9cb6 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ffdc828eccff45728455a572d1f2b0e3 |
|||
timeCreated: 1618978426 |
|
|||
using UnityEngine; |
|||
|
|||
namespace UnityEngine.Perception.Randomization |
|||
{ |
|||
public abstract class ArchetypeBase |
|||
{ |
|||
public abstract string label { get; } |
|||
|
|||
public abstract void PreprocessAsset(Object asset); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 88c34672b8a649439c1fd55bc201acde |
|||
timeCreated: 1619731337 |
|
|||
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; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 27aa08b82cf94015adc22309c8f3bc48 |
|||
timeCreated: 1618977151 |
|
|||
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; |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9f2e99decdb94c7c898d7702e10cbce5 |
|||
timeCreated: 1619732135 |
|
|||
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]; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 20f161f5e56247c280fc1b56b567f49a |
|||
timeCreated: 1618983500 |
撰写
预览
正在加载...
取消
保存
Reference in new issue