138 lines
4.7 KiB
C#
138 lines
4.7 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine.ProBuilder;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace UnityEditor.ProBuilder.Actions
|
|
{
|
|
sealed class SelectMaterial : MenuAction
|
|
{
|
|
GUIContent gc_restrictToSelection = new GUIContent("Current Selection", "Optionally restrict the matches to only those faces on currently selected objects.");
|
|
internal Pref<bool> m_RestrictToSelectedObjects = new Pref<bool>("SelectMaterial.restrictToSelectedObjects", false, SettingsScope.Project);
|
|
|
|
public override ToolbarGroup group
|
|
{
|
|
get { return ToolbarGroup.Selection; }
|
|
}
|
|
|
|
public override string iconPath => "Toolbar/Selection_SelectByMaterial";
|
|
public override Texture2D icon => IconUtility.GetIcon(iconPath);
|
|
|
|
public override TooltipContent tooltip
|
|
{
|
|
get { return s_Tooltip; }
|
|
}
|
|
|
|
static readonly TooltipContent s_Tooltip = new TooltipContent
|
|
(
|
|
"Select Material",
|
|
"Selects all faces matching the selected materials."
|
|
);
|
|
|
|
public override SelectMode validSelectModes
|
|
{
|
|
get { return SelectMode.Face | SelectMode.TextureFace; }
|
|
}
|
|
|
|
public override bool enabled
|
|
{
|
|
get { return base.enabled && MeshSelection.selectedFaceCount > 0; }
|
|
}
|
|
|
|
protected override MenuActionState optionsMenuState
|
|
{
|
|
get { return MenuActionState.VisibleAndEnabled; }
|
|
}
|
|
|
|
public override VisualElement CreateSettingsContent()
|
|
{
|
|
var root = new VisualElement();
|
|
|
|
var toggle = new Toggle(gc_restrictToSelection.text);
|
|
toggle.tooltip = gc_restrictToSelection.tooltip;
|
|
toggle.SetValueWithoutNotify(m_RestrictToSelectedObjects);
|
|
toggle.RegisterCallback<ChangeEvent<bool>>(evt =>
|
|
{
|
|
m_RestrictToSelectedObjects.SetValue(evt.newValue);
|
|
PreviewActionManager.UpdatePreview();
|
|
});
|
|
root.Add(toggle);
|
|
|
|
return root;
|
|
}
|
|
|
|
protected override void OnSettingsGUI()
|
|
{
|
|
GUILayout.Label("Select Material Options", EditorStyles.boldLabel);
|
|
|
|
EditorGUI.BeginChangeCheck();
|
|
|
|
m_RestrictToSelectedObjects.value = EditorGUILayout.Toggle(gc_restrictToSelection, m_RestrictToSelectedObjects);
|
|
|
|
if (EditorGUI.EndChangeCheck())
|
|
ProBuilderSettings.Save();
|
|
|
|
GUILayout.FlexibleSpace();
|
|
|
|
if (GUILayout.Button("Select Faces with Material"))
|
|
{
|
|
PerformAction();
|
|
SceneView.RepaintAll();
|
|
}
|
|
}
|
|
|
|
protected override ActionResult PerformActionImplementation()
|
|
{
|
|
IEnumerable<ProBuilderMesh> selection;
|
|
|
|
if (m_RestrictToSelectedObjects)
|
|
selection = MeshSelection.topInternal;
|
|
else
|
|
selection = EditorUtility.FindObjectsByType<ProBuilderMesh>();
|
|
|
|
UndoUtility.RecordSelection("Select Faces with Material");
|
|
|
|
//Need to go from submesh index to material
|
|
HashSet<Material> selectedMaterials = new HashSet<Material>();
|
|
foreach(var pb in MeshSelection.topInternal)
|
|
{
|
|
HashSet<int> submeshIndex = new HashSet<int>(pb.selectedFacesInternal.Select(y => y.submeshIndex));
|
|
foreach (var index in submeshIndex)
|
|
{
|
|
selectedMaterials.Add(pb.renderer.sharedMaterials[index]);
|
|
}
|
|
}
|
|
|
|
List<GameObject> newSelection = new List<GameObject>();
|
|
foreach (var pb in selection)
|
|
{
|
|
List<int> subMeshIndices = new List<int>();
|
|
for (int matIndex = 0; matIndex < pb.renderer.sharedMaterials.Length; ++matIndex)
|
|
{
|
|
if(selectedMaterials.Contains(pb.renderer.sharedMaterials[matIndex]))
|
|
{
|
|
subMeshIndices.Add(matIndex);
|
|
}
|
|
}
|
|
|
|
if(subMeshIndices.Count > 0)
|
|
{
|
|
IEnumerable<Face> matches = pb.facesInternal.Where(x => subMeshIndices.Contains(x.submeshIndex));
|
|
if (matches.Any())
|
|
{
|
|
newSelection.Add(pb.gameObject);
|
|
pb.SetSelectedFaces(matches);
|
|
}
|
|
}
|
|
}
|
|
|
|
Selection.objects = newSelection.ToArray();
|
|
|
|
ProBuilderEditor.Refresh();
|
|
|
|
return new ActionResult(ActionResult.Status.Success, "Select Faces with Material");
|
|
}
|
|
}
|
|
}
|