Skip to content

Commit

Permalink
feat: shader properties show is based on texture channel
Browse files Browse the repository at this point in the history
  • Loading branch information
rYuuk committed Jan 5, 2024
1 parent db234c8 commit bfb4ec4
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions Editor/Core/Scripts/UI/CustomEditors/AvatarConfigEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class AvatarConfigEditor : UnityEditor.Editor
private bool previousMeshOptCompressionValue;

private VisualElement root;
private Action textureChannelChanged;

public override VisualElement CreateInspectorGUI()
{
Expand Down Expand Up @@ -215,15 +216,17 @@ void BindItem(VisualElement e, int i)
{
if (x.newValue)
{
var textureChannels = new List<TextureChannel>();
var textureChannels = avatarConfigTarget.TextureChannel.ToList();
textureChannels.Add((TextureChannel) i);
avatarConfigTarget.TextureChannel = textureChannels.ToArray();
textureChannelChanged?.Invoke();
}
else
{
var textureChannels = avatarConfigTarget.TextureChannel.ToList();
textureChannels.Remove((TextureChannel) i);
avatarConfigTarget.TextureChannel = textureChannels.ToArray();
textureChannelChanged?.Invoke();
}
Save();
});
Expand All @@ -247,25 +250,55 @@ private void SetupShader()

var shaderPropertiesContainer = root.Q<VisualElement>("ShaderProperties");
CreateShaderProperties(shaderPropertiesContainer);

textureChannelChanged += () => ShowShaderProperties(shaderPropertiesContainer);
if (shader.value == null)
{
shaderPropertiesContainer.style.display = DisplayStyle.None;
}
else
{
ShowShaderProperties(shaderPropertiesContainer);
}

shader.RegisterValueChangedCallback(x =>
{
avatarConfigTarget.Shader = (Shader) x.newValue;
Save();
shaderPropertiesContainer.style.display = x.previousValue == null && x.newValue != null ? DisplayStyle.Flex : DisplayStyle.None;
if (x.newValue == null)
{
shaderPropertiesContainer.style.display = DisplayStyle.None;
}
else
{
ShowShaderProperties(shaderPropertiesContainer);
}
}
);
}

private void ShowShaderProperties(VisualElement shaderPropertiesContainer)
{
shaderPropertiesContainer.style.display = DisplayStyle.Flex;
foreach (var child in shaderPropertiesContainer.Children())
{
if (avatarConfigTarget.TextureChannel.Contains((TextureChannel) Enum.Parse(typeof(TextureChannel), child.name)))
{
child.style.display = DisplayStyle.Flex;
}
else
{
child.style.display = DisplayStyle.None;
}
}
}

private void CreateShaderProperties(VisualElement shaderPropertiesContainer)
{
foreach (TextureChannel textureChannel in Enum.GetValues(typeof(TextureChannel)))
{
var field = new TextField(textureChannel.ToString());
field.name = textureChannel.ToString();
var property = avatarConfigTarget.ShaderProperties.FindIndex(x => x.TextureChannel == textureChannel);
field.SetValueWithoutNotify(avatarConfigTarget.ShaderProperties[property].PropertyName);
field.RegisterValueChangedCallback(x =>
Expand Down

0 comments on commit bfb4ec4

Please sign in to comment.