Skip to content

Commit

Permalink
feat: add material appy logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Nov 5, 2024
1 parent 4f872c8 commit ce4310a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Code/ApplyMaterialToAll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#nullable enable

using System.Linq;
using UnityEditor;
using UnityEngine;

namespace At.Ac.FhStp.ApplyMaterialAll
{
[InitializeOnLoad]
public static class ApplyMaterialToAll
{
private static GameObject? TryAsSelectedGameObject(int instanceID)
{
return EditorUtility.InstanceIDToObject(instanceID) as GameObject;
}

private static Material? TryGetDraggedMaterial()
{
return DragAndDrop.objectReferences?.SingleOrDefault() as Material;
}

private static void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
var currentEvent = Event.current;

// Must hold alt
if (!currentEvent.alt) return;

// Must only drag single material
if (TryGetDraggedMaterial() is not { } material) return;

// Must target game object
if (TryAsSelectedGameObject(instanceID) is not { } gameObject)
return;

// Handle drag updated for feedback
if (currentEvent.type == EventType.DragUpdated)
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
currentEvent.Use();
return;
}

// Must be drag
if (currentEvent.type != EventType.DragPerform) return;

DragAndDrop.AcceptDrag();

Undo.RegisterFullObjectHierarchyUndo(gameObject,
"Apply material to all");
var meshRenderers =
gameObject.GetComponentsInChildren<MeshRenderer>();
foreach (var meshRenderer in meshRenderers)
meshRenderer.material = material;
EditorUtility.SetDirty(gameObject);

currentEvent.Use();
}

static ApplyMaterialToAll()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
}
}
3 changes: 3 additions & 0 deletions Code/ApplyMaterialToAll.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ce4310a

Please sign in to comment.