Skip to content

Commit

Permalink
fix: make several APIs virtual for inheritance purposes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Sep 29, 2024
1 parent f97cca0 commit a6d57d2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 37 deletions.
29 changes: 6 additions & 23 deletions Packages/src/Editor/InjectionPropertyListDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ internal class InjectionPropertyListDrawer : ReorderableList
public Action resetCallback;

private static readonly Regex s_RegexOthers =
new Regex("_ST$|_HDR$|_TexelSize$|^_Stencil|^_MainTex$|^_ClipRect$|^_UseUIAlphaClip$|^_ColorMask$" +
"|^_TextureSampleAdd$|^_UIMaskSoftnessX$|^_UIMaskSoftnessY$");
new Regex(
"_ST$|_HDR$|_TexelSize$|^_Stencil|^_MainTex$|^_Color$|^_ClipRect$|^_UseUIAlphaClip$|^_ColorMask$" +
"|^_TextureSampleAdd$|^_UIMaskSoftnessX$|^_UIMaskSoftnessY$");

public InjectionPropertyListDrawer(SerializedProperty prop) : base(prop.serializedObject, prop)
{
Expand Down Expand Up @@ -56,10 +57,10 @@ private void OnAddDropdownCallback(Rect r, ReorderableList _)
var included = new HashSet<string>(Enumerable.Range(0, propArray.arraySize)
.Select(i => propArray.GetArrayElementAtIndex(i).FindPropertyRelative("m_PropertyName").stringValue));
var shader = current.defaultMaterialForRendering.shader;
var properties = GetAllProperties(shader)
var properties = shader.GetAllProperties()
.Where(p => !included.Contains(p.name))
.Append((name: "", type: PropertyType.Undefined)) // Separator
.OrderBy(p => s_RegexOthers.IsMatch(p.name) ? 1 : 0);
.Append(new ShaderProperty("", PropertyType.Undefined)) // Separator
.OrderBy(p => s_RegexOthers.IsMatch(p.name));
var menu = new GenericMenu();
foreach (var p in properties)
{
Expand All @@ -69,24 +70,6 @@ private void OnAddDropdownCallback(Rect r, ReorderableList _)
menu.DropDown(r);
}

private static IEnumerable<(string name, PropertyType type)> GetAllProperties(Shader shader)
{
var propCount = shader.GetPropertyCount();
for (var i = 0; i < propCount; i++)
{
var propertyName = shader.GetPropertyName(i);
var type = (PropertyType)shader.GetPropertyType(i);
yield return (propertyName, type);

if (type == PropertyType.Texture)
{
yield return ($"{propertyName}_ST", PropertyType.Vector);
yield return ($"{propertyName}_HDR", PropertyType.Vector);
yield return ($"{propertyName}_TexelSize", PropertyType.Vector);
}
}
}

private static void AddToMenu(GenericMenu menu, SerializedProperty propArray, string propertyName,
PropertyType type, Action<SerializedProperty, string> postAddCallback)
{
Expand Down
59 changes: 59 additions & 0 deletions Packages/src/Editor/ShaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

namespace Coffee.UIExtensions
{
public class ShaderProperty
{
public string name { get; private set; }
public string description { get; private set; }
public PropertyType type { get; private set; }
public Vector2 range { get; private set; }
public ShaderPropertyFlags flags { get; private set; }
public string[] attributes { get; private set; }

public ShaderProperty(string name, PropertyType type)
{
this.name = name;
this.type = type;
description = string.Empty;
attributes = Array.Empty<string>();
}

public ShaderProperty(Shader shader, int index)
{
name = shader.GetPropertyName(index);
type = (PropertyType)shader.GetPropertyType(index);
attributes = shader.GetPropertyAttributes(index);
flags = shader.GetPropertyFlags(index);
description = shader.GetPropertyDescription(index);
range = type == PropertyType.Range
? shader.GetPropertyRangeLimits(index)
: Vector2.zero;
}
}

public static class ShaderExtensions
{
public static IEnumerable<ShaderProperty> GetAllProperties(this Shader self)
{
if (!self) yield break;

var count = self.GetPropertyCount();
for (var i = 0; i < count; i++)
{
var p = new ShaderProperty(self, i);
yield return p;

if (p.type == PropertyType.Texture)
{
yield return new ShaderProperty($"{p.name}_ST", PropertyType.Vector);
yield return new ShaderProperty($"{p.name}_HDR", PropertyType.Vector);
yield return new ShaderProperty($"{p.name}_TexelSize", PropertyType.Vector);
}
}
}
}
}
3 changes: 3 additions & 0 deletions Packages/src/Editor/ShaderExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 33 additions & 14 deletions Packages/src/Runtime/UIMaterialPropertyInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ public class UIMaterialPropertyInjector : MonoBehaviour, IMaterialModifier, ISer
private static readonly List<Material> s_Materials = new List<Material>();

[Tooltip("Reset all properties with the material properties when enabled.")]
[HideInInspector]
[SerializeField]
private bool m_ResetValuesOnEnable;

[Tooltip("Makes it animatable in the Animation view.")]
[HideInInspector]
[SerializeField]
private bool m_Animatable = true;

[Tooltip("Sharing group ID. If set, it shares the same material with the same group ID.\n" +
"NOTE: The material instances cannot be shared if the mask depth is different.")]
[SerializeField]
[HideInInspector]
private uint m_SharingGroupId;

[Tooltip("Properties to inject to the material.")]
[HideInInspector]
[SerializeField]
private List<InjectionProperty> m_Properties = new List<InjectionProperty>();

Expand All @@ -53,7 +57,7 @@ public class UIMaterialPropertyInjector : MonoBehaviour, IMaterialModifier, ISer

private bool canInject => _parent ? _parent.canInject : isActiveAndEnabled && 0 < m_Properties.Count;

private List<InjectionProperty> properties
public List<InjectionProperty> properties
{
get
{
Expand All @@ -65,7 +69,7 @@ private List<InjectionProperty> properties
/// <summary>
/// Makes it animatable in the Animation view.
/// </summary>
public bool animatable
public virtual bool animatable
{
get => m_Animatable;
set
Expand Down Expand Up @@ -94,7 +98,7 @@ public uint sharingGroupId
}
}

private Graphic graphic => _graphic ? _graphic : _graphic = GetComponent<Graphic>();
public Graphic graphic => _graphic ? _graphic : _graphic = GetComponent<Graphic>();

public Material material => 0 < graphic.canvasRenderer.materialCount
? graphic.canvasRenderer.GetMaterial()
Expand Down Expand Up @@ -173,9 +177,10 @@ public UnityEngine.Texture textureValue
}
}

private void OnEnable()
protected virtual void OnEnable()
{
Profiler.BeginSample("(MPI)[MPInjector] OnEnable");
UpdateProperties();
SetDirty();
SetMaterialDirty();
ShouldRebuild();
Expand All @@ -194,7 +199,7 @@ private void OnEnable()
Profiler.EndSample();
}

private void OnDisable()
protected virtual void OnDisable()
{
Profiler.BeginSample("(MPI)[MPInjector] OnDisable");
UIExtraCallbacks.onAfterCanvasRebuild -= _injectIfNeeded;
Expand All @@ -206,7 +211,7 @@ private void OnDisable()
Profiler.EndSample();
}

private void OnDestroy()
protected virtual void OnDestroy()
{
Profiler.BeginSample("(MPI)[MPInjector] OnDestroy");
var childCount = transform.childCount;
Expand All @@ -227,11 +232,19 @@ private void OnDestroy()
}

#if UNITY_EDITOR
private void OnValidate()
protected virtual void OnValidate()
{
UpdateProperties();
SetDirty();
SetMaterialDirty();
ShouldRebuild();

#if UNITY_EDITOR
if (!Application.isPlaying)
{
EditorApplication.QueuePlayerLoopUpdate();
}
#endif
}
#endif

Expand Down Expand Up @@ -289,6 +302,11 @@ void ISerializationCallbackReceiver.OnAfterDeserialize()
m_Properties.Rebuild(this, false, false);
}

private void OnDidApplyAnimationProperties()
{
UpdateProperties();
}

internal void RebuildPropertiesIfNeeded()
{
// If parent exists, rebuild properties in parent.
Expand Down Expand Up @@ -449,19 +467,13 @@ public void SetDirty()
}
}

#if UNITY_EDITOR
if (!Application.isPlaying)
{
EditorApplication.QueuePlayerLoopUpdate();
}
#endif
Profiler.EndSample();
}

/// <summary>
/// Inject properties should be rebuilt because the properties added or removed.
/// </summary>
private void ShouldRebuild()
protected void ShouldRebuild()
{
_shouldRebuild = true;
}
Expand Down Expand Up @@ -539,5 +551,12 @@ public void ResetPropertiesToDefault()
SetMaterialDirty();
Profiler.EndSample();
}

/// <summary>
/// Update properties.
/// </summary>
protected virtual void UpdateProperties()
{
}
}
}

0 comments on commit a6d57d2

Please sign in to comment.