Skip to content

Commit

Permalink
feat: support AnimatableProperty for multiple materials
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Sep 1, 2020
1 parent fa271f6 commit 062d988
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
36 changes: 23 additions & 13 deletions Packages/UIParticle/Scripts/Editor/AnimatedPropertiesEditor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
Expand All @@ -8,8 +9,9 @@ namespace Coffee.UIExtensions
{
internal class AnimatedPropertiesEditor
{
static readonly List<string> s_ActiveNames = new List<string>();
static readonly System.Text.StringBuilder s_Sb = new System.Text.StringBuilder();
private static readonly List<string> s_ActiveNames = new List<string>();
private static readonly System.Text.StringBuilder s_Sb = new System.Text.StringBuilder();
private static readonly HashSet<string> s_Names = new HashSet<string>();

private string _name;
private ShaderPropertyType _type;
Expand Down Expand Up @@ -39,10 +41,8 @@ static string CollectActiveNames(SerializedProperty sp, List<string> result)
return s_Sb.ToString();
}

public static void DrawAnimatableProperties(SerializedProperty sp, Material mat)
public static void DrawAnimatableProperties(SerializedProperty sp, Material[] mats)
{
if (!mat || !mat.shader) return;

bool isClicked;
using (new EditorGUILayout.HorizontalScope(GUILayout.ExpandWidth(false)))
{
Expand Down Expand Up @@ -72,17 +72,27 @@ public static void DrawAnimatableProperties(SerializedProperty sp, Material mat)
}
}

for (var i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i++)
s_Names.Clear();
foreach (var mat in mats)
{
var pName = ShaderUtil.GetPropertyName(mat.shader, i);
var type = (ShaderPropertyType) ShaderUtil.GetPropertyType(mat.shader, i);
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName, _type = type}, true);
if (!mat || !mat.shader) continue;

for (var i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i++)
{
var pName = ShaderUtil.GetPropertyName(mat.shader, i);
var type = (ShaderPropertyType) ShaderUtil.GetPropertyType(mat.shader, i);
var name = string.Format("{0} ({1})", pName, type);
if (s_Names.Contains(name)) continue;
s_Names.Add(name);

if (type != ShaderPropertyType.Texture) continue;
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName, _type = type}, true);

AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_ST", _type = ShaderPropertyType.Vector}, true);
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_HDR", _type = ShaderPropertyType.Vector}, true);
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_TexelSize", _type = ShaderPropertyType.Vector}, true);
if (type != ShaderPropertyType.Texture) continue;

AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_ST", _type = ShaderPropertyType.Vector}, true);
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_HDR", _type = ShaderPropertyType.Vector}, true);
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_TexelSize", _type = ShaderPropertyType.Vector}, true);
}
}

gm.ShowAsContext();
Expand Down
8 changes: 7 additions & 1 deletion Packages/UIParticle/Scripts/Editor/UIParticleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEditor.UI;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using UnityEditorInternal;
using UnityEngine.UI;

Expand Down Expand Up @@ -100,7 +101,12 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_spScale);

// AnimatableProperties
AnimatedPropertiesEditor.DrawAnimatableProperties(_spAnimatableProperties, current.material);
var mats = current.particles
.Where(x => x)
.Select(x => x.GetComponent<ParticleSystemRenderer>().sharedMaterial)
.Where(x => x)
.ToArray();
AnimatedPropertiesEditor.DrawAnimatableProperties(_spAnimatableProperties, mats);

_ro.DoLayoutList();

Expand Down

0 comments on commit 062d988

Please sign in to comment.