-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f872c8
commit ce4310a
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.