Files
com.misaki.shader-gui/Editor/Helpers/MaterialEditorHelpers.cs
Misaki dc1f1fb4e7 Added GetEnumValue;
Added EnumFlagsDrawer;
Added custom keywordName option to KeywordTexturePropertySingleLine;
2025-02-03 15:44:46 +09:00

125 lines
5.5 KiB
C#

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, string keywordName = null)
{
return KeywordTexturePropertySingleLine(editor, label, textureProperty, null, null, keywordName);
}
/// <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, string keywordName = null)
{
return KeywordTexturePropertySingleLine(editor, label, textureProperty, extraProperty, null, keywordName);
}
/// <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, string keywordName = null)
{
EditorGUI.BeginChangeCheck();
editor.TexturePropertySingleLine(label, textureProperty, extraProperty1, extraProperty2);
if (EditorGUI.EndChangeCheck())
{
editor.SetKeyword(string.IsNullOrEmpty(keywordName) ? textureProperty.name.ToUpper() : keywordName, textureProperty.textureValue != null);
}
return textureProperty.textureValue != null;
}
#endregion
}
}