Initial upload
This commit is contained in:
125
Editor/Helpers/MaterialEditorHelpers.cs
Normal file
125
Editor/Helpers/MaterialEditorHelpers.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace Misaki.ShaderGUI
|
||||
{
|
||||
public static class MaterialEditorHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets all the materials from the material editor.
|
||||
/// </summary>
|
||||
/// <param name="editor">The material editor.</param>
|
||||
/// <returns>All the materials from the material editor.</returns>
|
||||
public static IEnumerable<Material> GetMaterials(this MaterialEditor editor)
|
||||
{
|
||||
foreach (var target in editor.targets)
|
||||
{
|
||||
if (target is Material material)
|
||||
{
|
||||
yield return material;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a keyword on all the materials in the material editor.
|
||||
/// </summary>
|
||||
/// <param name="editor">The material editor.</param>
|
||||
/// <param name="keywordName">The name of the keyword</param>
|
||||
/// <param name="value">The value of the keyword.</param>
|
||||
public static void SetKeyword(this MaterialEditor editor, string keywordName, bool value)
|
||||
{
|
||||
foreach (var material in editor.GetMaterials())
|
||||
{
|
||||
material.SetKeyword(new LocalKeyword(material.shader, keywordName), value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a change to all the materials in the material editor.
|
||||
/// </summary>
|
||||
/// <param name="editor">The material editor.</param>
|
||||
/// <param name="predicate">The predicate to determine if the change should be applied to the material.</param>
|
||||
/// <param name="changeAction">The action to apply the change.</param>
|
||||
public static void ApplyChange(this MaterialEditor editor, Predicate<Material> predicate, Action changeAction)
|
||||
{
|
||||
foreach (var material in editor.GetMaterials())
|
||||
{
|
||||
if (!predicate(material))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
changeAction();
|
||||
editor.RegisterPropertyChangeUndo(material.name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a change to all the materials in the material editor.
|
||||
/// </summary>
|
||||
/// <param name="editor">The material editor.</param>
|
||||
/// <param name="changeAction">The action to apply the change.</param>
|
||||
public static void ApplyChange(this MaterialEditor editor, Action<Material> changeAction)
|
||||
{
|
||||
foreach (var material in editor.GetMaterials())
|
||||
{
|
||||
changeAction(material);
|
||||
editor.RegisterPropertyChangeUndo(material.name);
|
||||
}
|
||||
}
|
||||
|
||||
#region GUI
|
||||
|
||||
/// <summary>
|
||||
/// Draws a single line texture property with a keyword toggle.
|
||||
/// </summary>
|
||||
/// <param name="editor">The material editor.</param>
|
||||
/// <param name="label">The label of the texture property.</param>
|
||||
/// <param name="textureProperty">The texture property to draw.</param>
|
||||
/// <returns>True if the texture property is not null. Otherwise, false.</returns>
|
||||
public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty)
|
||||
{
|
||||
return KeywordTexturePropertySingleLine(editor, label, textureProperty, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws a single line texture property with a keyword toggle.
|
||||
/// </summary>
|
||||
/// <param name="editor">The material editor. </param>
|
||||
/// <param name="label">The label of the texture property.</param>
|
||||
/// <param name="textureProperty">The texture property to draw.</param>
|
||||
/// <param name="extraProperty">The first extra property to draw.</param>
|
||||
/// <returns>True if the texture property is not null. Otherwise, false.</returns>
|
||||
public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, MaterialProperty extraProperty)
|
||||
{
|
||||
return KeywordTexturePropertySingleLine(editor, label, textureProperty, extraProperty, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws a single line texture property with a keyword toggle.
|
||||
/// </summary>
|
||||
/// <param name="editor">The material editor. </param>
|
||||
/// <param name="label">The label of the texture property.</param>
|
||||
/// <param name="textureProperty">The texture property to draw.</param>
|
||||
/// <param name="extraProperty1">The first extra property to draw.</param>
|
||||
/// <param name="extraProperty2">The second extra property to draw.</param>
|
||||
/// <returns>True if the texture property is not null. Otherwise, false.</returns>
|
||||
public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, MaterialProperty extraProperty1, MaterialProperty extraProperty2)
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
editor.TexturePropertySingleLine(label, textureProperty, extraProperty1, extraProperty2);
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
editor.SetKeyword(textureProperty.name.ToUpper(), textureProperty.textureValue != null);
|
||||
}
|
||||
|
||||
return textureProperty.textureValue != null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user