1
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
@@ -19,6 +21,8 @@ namespace BITKit
|
||||
private Button buildButton;
|
||||
private Label _titleLabel;
|
||||
private VisualElement _container;
|
||||
|
||||
private MeshRenderer[] _renderers=Array.Empty<MeshRenderer>();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
@@ -38,22 +42,52 @@ namespace BITKit
|
||||
|
||||
private void OnSelectionChanged()
|
||||
{
|
||||
var active = UnityEditor.Selection.activeGameObject;
|
||||
var actives = UnityEditor.Selection.gameObjects;
|
||||
|
||||
_container.Clear();
|
||||
|
||||
_titleLabel.text = active ? active.name : "未选择物体";
|
||||
if (!active) return;
|
||||
foreach (var x in active.GetComponentsInChildren<MeshRenderer>(true))
|
||||
|
||||
_renderers = actives.SelectMany(x => x.GetComponentsInChildren<MeshRenderer>()).ToArray();
|
||||
|
||||
var materials = _renderers.SelectMany(x => x.sharedMaterials).Distinct().ToArray();
|
||||
|
||||
_titleLabel.text = actives is not {Length:0} ?$"选择了{actives.Length }个物体,{materials.Length}个材质" : "未选择物体";
|
||||
|
||||
foreach (var material in materials)
|
||||
{
|
||||
foreach (var material in x.sharedMaterials)
|
||||
var filed = _container.Create<ObjectField>();
|
||||
filed.label = material.name;
|
||||
filed.objectType = typeof(Material);
|
||||
filed.allowSceneObjects = false;
|
||||
filed.value = material;
|
||||
filed.RegisterValueChangedCallback(OnChanged);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnChanged(ChangeEvent<Object> evt)
|
||||
{
|
||||
if(evt.newValue is not Material value) return;
|
||||
var list = new List<Object>();
|
||||
foreach (var renderer in _renderers)
|
||||
{
|
||||
var sharedMaterials = renderer.sharedMaterials;
|
||||
var isDirty = false;
|
||||
for (var i = 0; i < sharedMaterials.Length; i++)
|
||||
{
|
||||
var filed = rootVisualElement.Create<ObjectField>();
|
||||
filed.label = material.name;
|
||||
filed.objectType = typeof(Material);
|
||||
filed.value = material;
|
||||
var current = sharedMaterials[i];
|
||||
if(current != evt.previousValue) continue;
|
||||
sharedMaterials[i] = value;
|
||||
isDirty = true;
|
||||
}
|
||||
|
||||
|
||||
if (!isDirty) continue;
|
||||
renderer.sharedMaterials = sharedMaterials;
|
||||
list.Add(renderer);
|
||||
EditorUtility.SetDirty(renderer);
|
||||
}
|
||||
|
||||
if (list.Count > 0)
|
||||
{
|
||||
OnSelectionChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user