Skip to content

Commit

Permalink
feat: on-demand UIEffect shader support
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Dec 9, 2024
1 parent 9164e14 commit 17604b7
Show file tree
Hide file tree
Showing 15 changed files with 648 additions and 390 deletions.
7 changes: 5 additions & 2 deletions Assets/ProjectSettings/UIEffectProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ MonoBehaviour:
m_RuntimePresets:
- {fileID: 1645081683573595507, guid: 3b09ad143990c4c8795c6e93760e132a, type: 3}
- {fileID: 1645081683573595507, guid: 4f70ff0f1e25b4388b9bcb38d619f7bf, type: 3}
m_FallbackVariantBehaviour: 0
m_UnregisteredShaderVariants: []
m_ShaderVariantCollection: {fileID: -8740588384831342173}
m_ShaderVariantRegistry:
m_ErrorOnUnregisteredVariant: 0
m_OptionalShaders: []
m_UnregisteredVariants: []
m_Asset: {fileID: -8740588384831342173}
100 changes: 10 additions & 90 deletions Packages/src/Editor/UIEffectProjectSettingsEditor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using UnityEditor;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Reflection;
using Coffee.UIEffectInternal;
using UnityEditorInternal;

namespace Coffee.UIEffects.Editors
Expand All @@ -11,16 +9,14 @@ namespace Coffee.UIEffects.Editors
public class UIEffectProjectSettingsEditor : Editor
{
private ReorderableList _reorderableList;
private SerializedProperty _fallbackVariantBehaviour;
private SerializedProperty _transformSensitivity;
private Editor _editor;
private bool _isInitialized;
private ShaderVariantRegistryEditor _shaderVariantRegistryEditor;

private void InitializeIfNeeded()
{
if (_isInitialized) return;

_fallbackVariantBehaviour = serializedObject.FindProperty("m_FallbackVariantBehaviour");
_transformSensitivity = serializedObject.FindProperty("m_TransformSensitivity");
var runtimePresets = serializedObject.FindProperty("m_RuntimePresets");
_reorderableList = new ReorderableList(serializedObject, runtimePresets, true, true, true, true);
Expand Down Expand Up @@ -53,19 +49,11 @@ private void InitializeIfNeeded()
menu.DropDown(rect);
};

var collection = serializedObject.FindProperty("m_ShaderVariantCollection").objectReferenceValue;
_editor = CreateEditor(collection);
_isInitialized = true;
}

private void OnDisable()
{
if (_editor)
{
DestroyImmediate(_editor);
}

_editor = null;
_isInitialized = false;
}

Expand All @@ -78,86 +66,18 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_transformSensitivity);
_reorderableList.DoLayoutList();

// Editor
EditorGUILayout.PropertyField(_fallbackVariantBehaviour);
serializedObject.ApplyModifiedProperties();

// Shader
EditorGUILayout.Space(20);
EditorGUILayout.LabelField("Shader Variants", EditorStyles.boldLabel);
EditorGUILayout.Space(-12);
DrawUnregisteredShaderVariants(UIEffectProjectSettings.instance.m_UnregisteredShaderVariants);
DrawRegisteredShaderVariants(_editor);
GUILayout.FlexibleSpace();
}

private static void DrawUnregisteredShaderVariants(List<string> variants)
{
if (variants.Count == 0) return;

// Shader registry
EditorGUILayout.Space();
var array = variants.ToArray();
var r = EditorGUILayout.GetControlRect(false, 20);
var rLabel = new Rect(r.x, r.y, r.width - 80, r.height);
EditorGUI.LabelField(rLabel, "Registered Shader Variants");

var rButtonClear = new Rect(r.x + r.width - 80, r.y + 2, 80, r.height - 4);
if (GUI.Button(rButtonClear, "Clear All", EditorStyles.miniButton))
EditorGUILayout.LabelField("Shader", EditorStyles.boldLabel);
if (_shaderVariantRegistryEditor == null)
{
UIEffectProjectSettings.ClearUnregisteredShaderVariants();
var property = serializedObject.FindProperty("m_ShaderVariantRegistry");
_shaderVariantRegistryEditor = new ShaderVariantRegistryEditor(property, "(UIEffect)");
}

EditorGUILayout.BeginVertical("RL Background");
for (var i = 0; i < array.Length; i++)
{
var values = array[i].Split(';');
r = EditorGUILayout.GetControlRect();
var rShader = new Rect(r.x, r.y, 150, r.height);
EditorGUI.ObjectField(rShader, Shader.Find(values[0]), typeof(Shader), false);

var rKeywords = new Rect(r.x + 150, r.y, r.width - 150 - 20, r.height);
EditorGUI.TextField(rKeywords, values[1]);

var rButton = new Rect(r.x + r.width - 20 + 2, r.y, 20, r.height);
if (GUI.Button(rButton, EditorGUIUtility.IconContent("icons/toolbar plus.png"), "iconbutton"))
{
UIEffectProjectSettings.RegisterVariant(array[i]);
}
}

EditorGUILayout.EndVertical();
}

private static void DrawRegisteredShaderVariants(Editor editor)
{
var collection = editor.target as ShaderVariantCollection;
if (collection == null) return;

EditorGUILayout.Space();
var r = EditorGUILayout.GetControlRect(false, 20);
var rLabel = new Rect(r.x, r.y, r.width - 80, r.height);
EditorGUI.LabelField(rLabel, "Registered Shader Variants");

var rButton = new Rect(r.x + r.width - 80, r.y + 2, 80, r.height - 4);
if (GUI.Button(rButton, "Clear All", EditorStyles.miniButton))
{
collection.Clear();
}

EditorGUILayout.BeginVertical("RL Background");
editor.serializedObject.Update();
var shaders = editor.serializedObject.FindProperty("m_Shaders");
for (var i = 0; i < shaders.arraySize; i++)
{
s_MiDrawShaderEntry.Invoke(editor, new object[] { i });
}

editor.serializedObject.ApplyModifiedProperties();
EditorGUILayout.EndVertical();
_shaderVariantRegistryEditor.Draw();
GUILayout.FlexibleSpace();
serializedObject.ApplyModifiedProperties();
}

private static readonly MethodInfo s_MiDrawShaderEntry =
Type.GetType("UnityEditor.ShaderVariantCollectionInspector, UnityEditor")
?.GetMethod("DrawShaderEntry", BindingFlags.NonPublic | BindingFlags.Instance);
}
}
35 changes: 22 additions & 13 deletions Packages/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Combine various filters, such as grayscale, blur, and dissolve, to decorate your
- [Install via UPM (with Package Manager UI)](#install-via-upm-with-package-manager-ui)
- [Install via UPM (Manually)](#install-via-upm-manually)
- [Install as Embedded Package](#install-as-embedded-package)
- [Additional Resource Imports](#additional-resource-imports)
- [Import Additional Resources](#import-additional-resources)
- [🔄 Upgrading from v4 to v5](#-upgrading-from-v4-to-v5)
- [🚀 Usage](#-usage)
- [Getting Started](#getting-started)
Expand Down Expand Up @@ -122,16 +122,17 @@ _This package requires **Unity 2020.3 or later**._

### Install as Embedded Package

1. Download a source code zip file from [Releases](https://github.com/mob-sakai/UIEffect/releases) and extract it.
2. Place `<extracted_dir>/Packages/src` directory in your project's `Packages` directory.
1. Download the `Source code (zip)` file from [Releases](https://github.com/mob-sakai/UIEffect/releases) and
extract it.
2. Move the `<extracted_dir>/Packages/src` directory into your project's `Packages` directory.
![](https://github.com/user-attachments/assets/187cbcbe-5922-4ed5-acec-cf19aa17d208)
- You can rename the `src` directory.
- If you want to fix bugs or add features, install it as an embedded package.
- To update the package, you need to re-download it and replace the contents.
- You can rename the `src` directory if needed.
- If you intend to fix bugs or add features, installing it as an embedded package is recommended.
- To update the package, re-download it and replace the existing contents.

### Additional Resource Imports
### Import Additional Resources

UIEffect includes additional resources to import.
Additional resources can be imported to extend functionality.

- [🔄 Upgrading from v4 to v5](#-upgrading-from-v4-to-v5)
- [Usage with TextMeshPro](#usage-with-textmeshpro)
Expand Down Expand Up @@ -301,18 +302,26 @@ UIEffectProjectSettings.shaderVariantCollection.WarmUp();

### Project Settings

![](https://github.com/user-attachments/assets/33b01665-0893-4460-a220-62f5f08b2eec)
![](https://github.com/user-attachments/assets/54dd42cf-099d-4fb1-b699-cad29bf211b6)

You can adjust the project-wide settings for UIEffect. (`Edit > Project Settings > UI > UIEffect`)

- **Transform Sensitivity**: `Low`, `Medium`, `High`
- Set the sensitivity of the transformation when `Use Target Transform` is enabled in `UIEffectReplica` component.
- **Runtime Presets**: A list of presets that can be loaded at runtime. Load them using `UIEffect.LoadPreset(presetName)` method.
- **Fallback Variant Behavior**: Specifies the behavior when an unregistered shader variant is used in the editor.
- `Register Variant`: Adds the variant to `Registered Variants` for runtime use.
- `LogError`: Outputs a error and adds it to `Unregistered Variants`.
- **Optional Shaders (UIEffect)**: A list of shaders that will be prioritized when a ui-effect shader is
requested.
- If the shader is included in the list, that shader will be used.
- If it is not in the list, the following shaders will be used in order:
- If the shader name contains `(UIEffect)`, that shader will be used.
- If `Hidden/<shader_name> (UIEffect)` exists, that shader will be used.
- As a fallback, `UI/Default (UIEffect)` will be used.
- **Registered Variants**: A list of shader variants available at runtime. Use "-" button to remove unused variants,
reducing build time and file size.
- By default, the used ui-effect shaders will be included in the build. You can remove them if you don't need.
- **Unregistered Variants**: A list of shader variants that are not registered. Use "+" button to add variants.
- **Registered Variants**: A list of shader variants available at runtime. Use "-" button to remove unused variants, reducing build time and file size.
- **Error On Unregistered Variant**: If enabled, an error will be displayed when an unregistered shader variant is used.
- The shader variant will be automatically added to the `Unregistered Variants` list.

<br><br>

Expand Down
144 changes: 0 additions & 144 deletions Packages/src/Runtime/Internal/Extensions/TransformExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Coffee.UIEffectInternal
public abstract class PreloadedProjectSettings : ScriptableObject
#if UNITY_EDITOR
{
private class MyAllPostprocessor : AssetPostprocessor
private class Postprocessor : AssetPostprocessor
{
private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___, string[] ____)
{
Expand Down Expand Up @@ -47,6 +47,11 @@ private static void Initialize()
{
SetDefaultSettings(defaultSettings);
}

if (defaultSettings)
{
defaultSettings.OnInitialize();
}
}
}

Expand Down Expand Up @@ -110,6 +115,10 @@ protected static void SetDefaultSettings(PreloadedProjectSettings asset)
protected virtual void OnCreateAsset()
{
}

protected virtual void OnInitialize()
{
}
}
#else
{
Expand Down
Loading

0 comments on commit 17604b7

Please sign in to comment.