浏览代码

improved AddRandomizer menu

/main
Steven Leal 4 年前
当前提交
0898d4e4
共有 26 个文件被更改,包括 332 次插入31 次删除
  1. 6
      com.unity.perception/Editor/Randomization/VisualElements/RandomizerElement.cs
  2. 42
      com.unity.perception/Editor/Randomization/VisualElements/RandomizerList.cs
  3. 1
      com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/BackgroundObjectPlacementRandomizer.cs
  4. 1
      com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/ColorRandomizer.cs
  5. 1
      com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/ForegroundObjectPlacementRandomizer.cs
  6. 1
      com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/HueOffsetRandomizer.cs
  7. 1
      com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/RotationRandomizer.cs
  8. 1
      com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/TextureRandomizer.cs
  9. 5
      com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/ExampleTransformRandomizer.cs
  10. 13
      com.unity.perception/Editor/Randomization/Uss/AddRandomizerMenuStyles.uss
  11. 3
      com.unity.perception/Editor/Randomization/Uss/AddRandomizerMenuStyles.uss.meta
  12. 3
      com.unity.perception/Editor/Randomization/Uxml/Randomizer.meta
  13. 229
      com.unity.perception/Editor/Randomization/VisualElements/AddRandomizerMenu.cs
  14. 3
      com.unity.perception/Editor/Randomization/VisualElements/AddRandomizerMenu.cs.meta
  15. 14
      com.unity.perception/Runtime/Randomization/Randomizers/AddRandomizerMenuAttribute.cs
  16. 3
      com.unity.perception/Runtime/Randomization/Randomizers/AddRandomizerMenuAttribute.cs.meta
  17. 8
      com.unity.perception/Editor/Randomization/Uxml/Randomizer/AddRandomizerMenu.uxml
  18. 3
      com.unity.perception/Editor/Randomization/Uxml/Randomizer/AddRandomizerMenu.uxml.meta
  19. 6
      com.unity.perception/Editor/Randomization/Uxml/Randomizer/MenuDirectoryElement.uxml
  20. 3
      com.unity.perception/Editor/Randomization/Uxml/Randomizer/MenuDirectoryElement.uxml.meta
  21. 8
      com.unity.perception/Editor/Randomization/Uxml/Randomizer/RandomizerList.uxml
  22. 8
      com.unity.perception/Editor/Randomization/Uxml/RandomizerList.uxml
  23. 0
      /com.unity.perception/Editor/Randomization/Uxml/Randomizer/RandomizerElement.uxml
  24. 0
      /com.unity.perception/Editor/Randomization/Uxml/Randomizer/RandomizerElement.uxml.meta
  25. 0
      /com.unity.perception/Editor/Randomization/Uxml/Randomizer/RandomizerList.uxml.meta

6
com.unity.perception/Editor/Randomization/VisualElements/RandomizerElement.cs


set
{
m_Collapsed.boolValue = value;
m_Property.serializedObject.ApplyModifiedProperties();
m_Property.serializedObject.ApplyModifiedPropertiesWithoutUndo();
if (value)
AddToClassList(k_CollapsedParameterClass);
else

m_Property = property;
this.randomizerList = randomizerList;
m_Collapsed = property.FindPropertyRelative("collapsed");
collapsed = m_Collapsed.boolValue;
$"{StaticData.uxmlDir}/RandomizerElement.uxml").CloneTree(this);
$"{StaticData.uxmlDir}/Randomizer/RandomizerElement.uxml").CloneTree(this);
var classNameLabel = this.Q<TextElement>("class-name");
var splitType = property.managedReferenceFullTypename.Split(' ', '.');

42
com.unity.perception/Editor/Randomization/VisualElements/RandomizerList.cs


using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.Experimental.Perception.Randomization.Editor;
using UnityEngine.Experimental.Perception.Randomization.Parameters;
using UnityEngine.Experimental.Perception.Randomization.Scenarios;
using UnityEngine.UIElements;

SerializedProperty m_Property;
VisualElement m_Container;
ToolbarMenu m_AddRandomizerMenu;
public HashSet<Type> randomizerTypeSet = new HashSet<Type>();
VisualElement inspectorContainer
{
get
{
var viewport = parent;
while (!viewport.ClassListContains("unity-inspector-main-container"))
viewport = viewport.parent;
return viewport;
}
}
$"{StaticData.uxmlDir}/RandomizerList.uxml").CloneTree(this);
$"{StaticData.uxmlDir}/Randomizer/RandomizerList.uxml").CloneTree(this);
m_AddRandomizerMenu = this.Q<ToolbarMenu>("add-randomizer");
var addRandomizerButton = this.Q<Button>("add-randomizer-button");
addRandomizerButton.clicked += () =>
{
inspectorContainer.Add(new AddRandomizerMenu(inspectorContainer, addRandomizerButton, this));
};
var expandAllButton = this.Q<Button>("expand-all");
expandAllButton.clicked += () => CollapseRandomizers(false);

m_Container.Clear();
for (var i = 0; i < m_Property.arraySize; i++)
m_Container.Add(new RandomizerElement(m_Property.GetArrayElementAtIndex(i), this));
SetMenuOptions();
}
void SetMenuOptions()
{
m_AddRandomizerMenu.menu.MenuItems().Clear();
var typeSet = new HashSet<Type>();
randomizerTypeSet.Clear();
typeSet.Add(randomizer.GetType());
foreach (var randomizerType in StaticData.randomizerTypes)
{
if (typeSet.Contains(randomizerType))
continue;
m_AddRandomizerMenu.menu.AppendAction(
Parameter.GetDisplayName(randomizerType),
a => { AddRandomizer(randomizerType); });
}
randomizerTypeSet.Add(randomizer.GetType());
void AddRandomizer(Type randomizerType)
public void AddRandomizer(Type randomizerType)
{
var newRandomizer = scenario.CreateRandomizer(randomizerType);
newRandomizer.RandomizeParameterSeeds();

1
com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/BackgroundObjectPlacementRandomizer.cs


/// Creates multiple layers of evenly distributed but randomly placed objects
/// </summary>
[Serializable]
[AddRandomizerMenu("Perception/Background Object Placement Randomizer")]
public class BackgroundObjectPlacementRandomizer : Randomizer
{
List<GameObject> m_SpawnedObjects = new List<GameObject>();

1
com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/ColorRandomizer.cs


/// Randomizes the material color of objects tagged with a ColorRandomizerTag
/// </summary>
[Serializable]
[AddRandomizerMenu("Perception/Color Randomizer")]
public class ColorRandomizer : Randomizer
{
static readonly int k_BaseColor = Shader.PropertyToID("_BaseColor");

1
com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/ForegroundObjectPlacementRandomizer.cs


/// Creates a 2D layer of of evenly spaced GameObjects from a given list of prefabs
/// </summary>
[Serializable]
[AddRandomizerMenu("Perception/Foreground Object Placement Randomizer")]
public class ForegroundObjectPlacementRandomizer : Randomizer
{
List<GameObject> m_SpawnedObjects = new List<GameObject>();

1
com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/HueOffsetRandomizer.cs


/// Randomly offsets the hue of objects tagged with a ColorRandomizerTag
/// </summary>
[Serializable]
[AddRandomizerMenu("Perception/Hue Offset Randomizer")]
public class HueOffsetRandomizer : Randomizer
{
static readonly int k_HueOffsetShaderProperty = Shader.PropertyToID("_HueOffset");

1
com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/RotationRandomizer.cs


/// Randomizes the rotation of objects tagged with a RotationRandomizerTag
/// </summary>
[Serializable]
[AddRandomizerMenu("Perception/Rotation Randomizer")]
public class RotationRandomizer : Randomizer
{
/// <summary>

1
com.unity.perception/Runtime/Randomization/Randomizers/SampleRandomizers/Randomizers/TextureRandomizer.cs


/// Randomizes the material texture of objects tagged with a TextureRandomizerTag
/// </summary>
[Serializable]
[AddRandomizerMenu("Perception/Texture Randomizer")]
public class TextureRandomizer : Randomizer
{
static readonly int k_BaseTexture = Shader.PropertyToID("_BaseMap");

5
com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/ExampleTransformRandomizer.cs


using Unity.Mathematics;
using System;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Experimental.Perception.Randomization.Parameters;
using UnityEngine.Experimental.Perception.Randomization.Randomizers;

[Serializable]
[AddRandomizerMenu("Perception Tests/Example Transform Randomizer")]
public class ExampleTransformRandomizer : Randomizer
{
public Vector3Parameter position = new Vector3Parameter();

13
com.unity.perception/Editor/Randomization/Uss/AddRandomizerMenuStyles.uss


.menu-element {
padding: 2px 10px 2px 20px;
}
.chevron-right {
height: 12px;
width: 12px;
background-image: resource("Packages/com.unity.perception/Editor/Randomization/Icons/FoldoutClosed.png");
}
.menu-element:hover {
background-color: #3E5F96;
}

3
com.unity.perception/Editor/Randomization/Uss/AddRandomizerMenuStyles.uss.meta


fileFormatVersion: 2
guid: 4315140b032047f8ad6bce89e3653dce
timeCreated: 1600837395

3
com.unity.perception/Editor/Randomization/Uxml/Randomizer.meta


fileFormatVersion: 2
guid: 006b3ab9c14b41f581f235810765469c
timeCreated: 1600991712

229
com.unity.perception/Editor/Randomization/VisualElements/AddRandomizerMenu.cs


using System;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine.Experimental.Perception.Randomization.Editor;
using UnityEngine.Experimental.Perception.Randomization.Randomizers;
using UnityEngine.UIElements;
namespace UnityEngine.Experimental.Perception.Randomization.VisualElements
{
class AddRandomizerMenu : VisualElement
{
string m_CurrentPath = string.Empty;
string currentPath
{
get => m_CurrentPath;
set
{
m_CurrentPath = value;
DrawDirectoryItems();
}
}
string currentPathName
{
get
{
if (m_CurrentPath == string.Empty)
return "Randomizers";
var pathItems = m_CurrentPath.Split('/');
return pathItems[pathItems.Length - 1];
}
}
string m_SearchString = string.Empty;
string searchString
{
get => m_SearchString;
set
{
m_SearchString = value;
if (m_SearchString == string.Empty)
DrawDirectoryItems();
else
DrawSearchItems();
}
}
RandomizerList m_RandomizerList;
VisualElement m_MenuElements;
TextElement m_DirectoryName;
Dictionary<string, List<MenuItem>> m_MenuItemsMap = new Dictionary<string, List<MenuItem>>();
Dictionary<string, HashSet<string>> m_MenuDirectories = new Dictionary<string, HashSet<string>>();
List<MenuItem> m_MenuItems = new List<MenuItem>();
class MenuItem
{
public Type randomizerType;
public string itemName;
public MenuItem(Type randomizerType, string itemName)
{
this.randomizerType = randomizerType;
this.itemName = itemName;
}
}
sealed class MenuItemElement : TextElement
{
public MenuItemElement(MenuItem menuItem, AddRandomizerMenu menu)
{
text = menuItem.itemName;
AddToClassList("menu-element");
RegisterCallback<MouseUpEvent>(evt => menu.AddRandomizer(menuItem.randomizerType));
}
}
sealed class MenuDirectoryElement : VisualElement
{
public MenuDirectoryElement(string directory, AddRandomizerMenu menu)
{
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/Randomizer/MenuDirectoryElement.uxml").CloneTree(this);
var pathItems = directory.Split('/');
this.Q<TextElement>("directory").text = pathItems[pathItems.Length - 1];
RegisterCallback<MouseUpEvent>(evt => menu.currentPath = directory);
}
}
public AddRandomizerMenu(VisualElement parentElement, VisualElement button, RandomizerList randomizerList)
{
m_RandomizerList = randomizerList;
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/Randomizer/AddRandomizerMenu.uxml");
template.CloneTree(this);
style.position = new StyleEnum<Position>(Position.Absolute);
var buttonPosition = button.worldBound.position;
var top = math.min(buttonPosition.y, parentElement.worldBound.height - 300);
style.top = top;
style.left = buttonPosition.x;
focusable = true;
RegisterCallback<FocusOutEvent>(evt =>
{
if (evt.relatedTarget == null || ((VisualElement)evt.relatedTarget).FindCommonAncestor(this) != this)
ExitMenu();
});
m_DirectoryName = this.Q<TextElement>("directory-name");
m_DirectoryName.RegisterCallback<MouseUpEvent>(evt => { AscendDirectory(); });
var searchBar = this.Q<TextField>("search-bar");
searchBar.schedule.Execute(() => searchBar.ElementAt(0).Focus());
searchBar.RegisterValueChangedCallback(evt => searchString = evt.newValue);
m_MenuElements = this.Q<VisualElement>("menu-options");
CreateMenuItems();
DrawDirectoryItems();
}
void ExitMenu()
{
parent.Remove(this);
}
void AddRandomizer(Type randomizerType)
{
m_RandomizerList.AddRandomizer(randomizerType);
ExitMenu();
}
void AscendDirectory()
{
var pathItems = m_CurrentPath.Split('/');
var path = pathItems[0];
for (var i = 1; i < pathItems.Length - 1; i++)
path = $"{path}/{pathItems[i]}";
currentPath = path;
}
void DrawDirectoryItems()
{
m_DirectoryName.text = currentPathName;
m_MenuElements.Clear();
if (m_MenuDirectories.ContainsKey(currentPath))
{
var directories = m_MenuDirectories[currentPath];
foreach (var directory in directories)
m_MenuElements.Add(new MenuDirectoryElement(directory, this));
}
if (m_MenuItemsMap.ContainsKey(currentPath))
{
var menuItems = m_MenuItemsMap[currentPath];
foreach (var menuItem in menuItems)
m_MenuElements.Add(new MenuItemElement(menuItem, this));
}
}
void DrawSearchItems()
{
m_DirectoryName.text = "Randomizers";
m_MenuElements.Clear();
var upperSearchString = searchString.ToUpper();
foreach (var menuItem in m_MenuItems)
{
if (menuItem.itemName.ToUpper().Contains(upperSearchString))
m_MenuElements.Add(new MenuItemElement(menuItem, this));
}
}
void CreateMenuItems()
{
var rootList = new List<MenuItem>();
m_MenuItemsMap.Add(string.Empty, rootList);
foreach (var randomizerType in StaticData.randomizerTypes)
{
if (m_RandomizerList.randomizerTypeSet.Contains(randomizerType))
continue;
var menuAttribute = (AddRandomizerMenuAttribute)Attribute.GetCustomAttribute(randomizerType, typeof(AddRandomizerMenuAttribute));
if (menuAttribute != null)
{
var pathItems = menuAttribute.menuPath.Split('/');
if (pathItems.Length > 1)
{
var path = string.Empty;
var itemName = pathItems[pathItems.Length - 1];
for (var i = 0; i < pathItems.Length - 1; i++)
{
var childPath = $"{path}/{pathItems[i]}";
if (i < pathItems.Length - 1)
{
if (!m_MenuDirectories.ContainsKey(path))
m_MenuDirectories.Add(path, new HashSet<string>());
m_MenuDirectories[path].Add(childPath);
}
path = childPath;
}
if (!m_MenuItemsMap.ContainsKey(path))
m_MenuItemsMap.Add(path, new List<MenuItem>());
var item = new MenuItem(randomizerType, itemName);
m_MenuItems.Add(item);
m_MenuItemsMap[path].Add(item);
}
else
{
if (pathItems.Length == 0)
throw new AssertionException("Empty randomizer menu path");
var item = new MenuItem(randomizerType, pathItems[0]);
m_MenuItems.Add(item);
rootList.Add(item);
}
}
else
{
rootList.Add(new MenuItem(randomizerType, randomizerType.Name));
}
}
m_MenuItems.Sort((item1, item2) => item1.itemName.CompareTo(item2.itemName));
}
}
}

3
com.unity.perception/Editor/Randomization/VisualElements/AddRandomizerMenu.cs.meta


fileFormatVersion: 2
guid: 26546e1877734ec5bd9d40a36eaa5322
timeCreated: 1600836688

14
com.unity.perception/Runtime/Randomization/Randomizers/AddRandomizerMenuAttribute.cs


using System;
namespace UnityEngine.Experimental.Perception.Randomization.Randomizers
{
public class AddRandomizerMenuAttribute : Attribute
{
public string menuPath;
public AddRandomizerMenuAttribute(string menuPath)
{
this.menuPath = menuPath;
}
}
}

3
com.unity.perception/Runtime/Randomization/Randomizers/AddRandomizerMenuAttribute.cs.meta


fileFormatVersion: 2
guid: 3648602c3ca54d58a5a30664148febf7
timeCreated: 1600976320

8
com.unity.perception/Editor/Randomization/Uxml/Randomizer/AddRandomizerMenu.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement name="container" style="width: 225px; height: 300px; background-color: #383838; border-width: 1px; border-color: #232323;">
<Style src="../../Uss/AddRandomizerMenuStyles.uss"/>
<TextField name="search-bar" style="padding: 3px;"/>
<TextElement name="directory-name" text="Randomizers" style="-unity-text-align: middle-center; -unity-font-style: bold; padding: 3px; background-color: #414141; border-bottom-width: 1px; border-top-width: 1px;border-color: #232323;"/>
<VisualElement name="menu-options"/>
</VisualElement>
</UXML>

3
com.unity.perception/Editor/Randomization/Uxml/Randomizer/AddRandomizerMenu.uxml.meta


fileFormatVersion: 2
guid: 158d437640f24d70822be92bba46591b
timeCreated: 1600837112

6
com.unity.perception/Editor/Randomization/Uxml/Randomizer/MenuDirectoryElement.uxml


<UXML xmlns="UnityEngine.UIElements">
<VisualElement style="flex-direction: row; justify-content: space-between; align-items: center;" class="menu-element">
<TextElement name="directory" style="margin-right: 10px;"/>
<VisualElement class="chevron-right"/>
</VisualElement>
</UXML>

3
com.unity.perception/Editor/Randomization/Uxml/Randomizer/MenuDirectoryElement.uxml.meta


fileFormatVersion: 2
guid: f792ce75baa443b1bc1c6e4e19f79b5b
timeCreated: 1600991861

8
com.unity.perception/Editor/Randomization/Uxml/Randomizer/RandomizerList.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement name="randomizers-container" class="dark-viewport" style="margin-top: 6px; min-height: 100px;"/>
<VisualElement style="flex-direction: row; align-items: center; justify-content: center; margin-top: 4px;">
<Button name="add-randomizer-button" text="Add Randomizer"/>
<Button name="expand-all" text="Expand All"/>
<Button name="collapse-all" text="Collapse All"/>
</VisualElement>
</UXML>

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


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement name="randomizers-container" class="dark-viewport" style="margin-top: 6px; min-height: 100px;"/>
<VisualElement style="flex-direction: row; align-items: center; justify-content: center; margin-top: 4px;">
<editor:ToolbarMenu name="add-randomizer" text="Add Randomizer"/>
<Button name="expand-all" text="Expand All"/>
<Button name="collapse-all" text="Collapse All"/>
</VisualElement>
</UXML>

/com.unity.perception/Editor/Randomization/Uxml/RandomizerElement.uxml → /com.unity.perception/Editor/Randomization/Uxml/Randomizer/RandomizerElement.uxml

/com.unity.perception/Editor/Randomization/Uxml/RandomizerElement.uxml.meta → /com.unity.perception/Editor/Randomization/Uxml/Randomizer/RandomizerElement.uxml.meta

/com.unity.perception/Editor/Randomization/Uxml/RandomizerList.uxml.meta → /com.unity.perception/Editor/Randomization/Uxml/Randomizer/RandomizerList.uxml.meta

正在加载...
取消
保存