using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace Misaki.ShaderGUI
{
public static class MaterialEditorHelpers
{
///
/// Gets all the materials from the material editor.
///
/// The material editor.
/// All the materials from the material editor.
public static IEnumerable GetMaterials(this MaterialEditor editor)
{
foreach (var target in editor.targets)
{
if (target is Material material)
{
yield return material;
}
}
}
///
/// Sets a keyword on all the materials in the material editor.
///
/// The material editor.
/// The name of the keyword
/// The value of the keyword.
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);
}
}
///
/// Applies a change to all the materials in the material editor.
///
/// The material editor.
/// The predicate to determine if the change should be applied to the material.
/// The action to apply the change.
public static void ApplyChange(this MaterialEditor editor, Predicate predicate, Action changeAction)
{
foreach (var material in editor.GetMaterials())
{
if (!predicate(material))
{
continue;
}
changeAction();
editor.RegisterPropertyChangeUndo(material.name);
}
}
///
/// Applies a change to all the materials in the material editor.
///
/// The material editor.
/// The action to apply the change.
public static void ApplyChange(this MaterialEditor editor, Action changeAction)
{
foreach (var material in editor.GetMaterials())
{
changeAction(material);
editor.RegisterPropertyChangeUndo(material.name);
}
}
#region GUI
///
/// Draws a single line texture property with a keyword toggle.
///
/// The material editor.
/// The label of the texture property.
/// The texture property to draw.
/// True if the texture property is not null. Otherwise, false.
public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, string keywordName = null)
{
return KeywordTexturePropertySingleLine(editor, label, textureProperty, null, null, keywordName);
}
///
/// Draws a single line texture property with a keyword toggle.
///
/// The material editor.
/// The label of the texture property.
/// The texture property to draw.
/// The first extra property to draw.
/// True if the texture property is not null. Otherwise, false.
public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, MaterialProperty extraProperty, string keywordName = null)
{
return KeywordTexturePropertySingleLine(editor, label, textureProperty, extraProperty, null, keywordName);
}
///
/// Draws a single line texture property with a keyword toggle.
///
/// The material editor.
/// The label of the texture property.
/// The texture property to draw.
/// The first extra property to draw.
/// The second extra property to draw.
/// True if the texture property is not null. Otherwise, false.
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
}
}