iFactory.Cutting.Unity/Assets/BITKit/Unity/Scripts/Scenes/Editor/MaterialPaletteWindow.cs

62 lines
1.4 KiB
C#
Raw Normal View History

2024-03-11 00:28:51 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit
{
public class MaterialPaletteWindow : EditorWindow
{
[MenuItem("Tools/Scenes/Material Palette")]
public static void Open()
{
GetWindow<MaterialPaletteWindow>().Show();
}
private Button buildButton;
private Label _titleLabel;
private VisualElement _container;
private void OnEnable()
{
UnityEditor.Selection.selectionChanged += OnSelectionChanged;
rootVisualElement.Clear();
_titleLabel = rootVisualElement.Create<Label>();
_container = rootVisualElement.Create<VisualElement>();
_titleLabel.text = "选择一个物体";
}
private void OnDisable()
{
UnityEditor.Selection.selectionChanged -= OnSelectionChanged;
}
private void OnSelectionChanged()
{
var active = UnityEditor.Selection.activeGameObject;
_container.Clear();
_titleLabel.text = active ? active.name : "未选择物体";
if (!active) return;
foreach (var x in active.GetComponentsInChildren<MeshRenderer>(true))
{
foreach (var material in x.sharedMaterials)
{
var filed = rootVisualElement.Create<ObjectField>();
filed.label = material.name;
filed.objectType = typeof(Material);
filed.value = material;
}
}
}
}
}