您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

109 行
3.9 KiB

using System;
using UnityEngine.Perception.Randomization.Scenarios;
using UnityEngine.UIElements;
namespace UnityEditor.Perception.Randomization
{
class RandomizerList : VisualElement
{
VisualElement m_Container;
SerializedProperty m_Property;
public RandomizerList(SerializedProperty property)
{
m_Property = property;
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/Randomizer/RandomizerList.uxml").CloneTree(this);
m_Container = this.Q<VisualElement>("randomizers-container");
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);
var collapseAllButton = this.Q<Button>("collapse-all");
collapseAllButton.clicked += () => CollapseRandomizers(true);
RefreshList();
Undo.undoRedoPerformed += () =>
{
m_Property.serializedObject.Update();
RefreshList();
};
}
public ScenarioBase scenario => (ScenarioBase)m_Property.serializedObject.targetObject;
VisualElement inspectorContainer
{
get
{
var viewport = parent;
while (!viewport.ClassListContains("unity-inspector-main-container"))
viewport = viewport.parent;
return viewport;
}
}
void RefreshList()
{
m_Container.Clear();
if (m_Property.arraySize > 0 &&
string.IsNullOrEmpty(m_Property.GetArrayElementAtIndex(0).managedReferenceFullTypename))
{
var textElement = new TextElement()
{
text = "One or more randomizers have missing scripts. See console for more info."
};
textElement.AddToClassList("scenario__info-box");
textElement.AddToClassList("scenario__error-box");
m_Container.Add(textElement);
return;
}
for (var i = 0; i < m_Property.arraySize; i++)
m_Container.Add(new RandomizerElement(m_Property.GetArrayElementAtIndex(i), this));
}
public void AddRandomizer(Type randomizerType)
{
Undo.RegisterCompleteObjectUndo(m_Property.serializedObject.targetObject, "Add Randomizer");
scenario.CreateRandomizer(randomizerType);
m_Property.serializedObject.Update();
RefreshList();
}
public void RemoveRandomizer(RandomizerElement element)
{
Undo.RegisterCompleteObjectUndo(m_Property.serializedObject.targetObject, "Remove Randomizer");
scenario.RemoveRandomizerAt(element.parent.IndexOf(element));
m_Property.serializedObject.Update();
RefreshList();
}
public void ReorderRandomizer(int currentIndex, int nextIndex)
{
if (currentIndex == nextIndex)
return;
if (nextIndex > currentIndex)
nextIndex--;
Undo.RegisterCompleteObjectUndo(m_Property.serializedObject.targetObject, "Reorder Randomizer");
var randomizer = scenario.GetRandomizer(currentIndex);
scenario.RemoveRandomizerAt(currentIndex);
scenario.InsertRandomizer(nextIndex, randomizer);
m_Property.serializedObject.Update();
RefreshList();
}
void CollapseRandomizers(bool collapsed)
{
foreach (var child in m_Container.Children())
((RandomizerElement)child).collapsed = collapsed;
}
}
}