Initial upload

This commit is contained in:
2025-02-01 23:08:14 +09:00
commit 4723c9833a
31 changed files with 660 additions and 0 deletions

View 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
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 686d34db7f97ea24db2499f7fd61b396

View File

@@ -0,0 +1,32 @@
using System;
using UnityEditor;
using UnityEngine;
namespace Misaki.ShaderGUI
{
public static class MaterialPropertyHelpers
{
public static bool GetBooleanValue(this MaterialProperty materialProperty)
{
#if UNITY_6000_1_OR_NEWER
if (materialProperty.propertyType == UnityEngine.Rendering.ShaderPropertyType.Float || materialProperty.propertyType == UnityEngine.Rendering.ShaderPropertyType.Range)
#else
if (materialProperty.type == MaterialProperty.PropType.Float || materialProperty.type == MaterialProperty.PropType.Range)
#endif
{
return !Mathf.Approximately(materialProperty.floatValue, 0.0f);
}
#if UNITY_6000_1_OR_NEWER
if (materialProperty.propertyType == UnityEngine.Rendering.ShaderPropertyType.Int)
#else
if (materialProperty.type == MaterialProperty.PropType.Int)
#endif
{
return materialProperty.intValue != 0;
}
throw new NotSupportedException("Property type is not supported.");
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b34fd47c26d28194a911715dd22a0a1e