Skip to content

Commit bdd8e08

Browse files
committed
feat: on-demand UIEffect shader support
close #212, close #271
1 parent 88609f5 commit bdd8e08

13 files changed

+577
-382
lines changed

Assets/ProjectSettings/UIEffectProjectSettings.asset

+5-2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ MonoBehaviour:
300300
m_RuntimePresets:
301301
- {fileID: 1645081683573595507, guid: 3b09ad143990c4c8795c6e93760e132a, type: 3}
302302
- {fileID: 1645081683573595507, guid: 4f70ff0f1e25b4388b9bcb38d619f7bf, type: 3}
303-
m_FallbackVariantBehaviour: 0
304-
m_UnregisteredShaderVariants: []
305303
m_ShaderVariantCollection: {fileID: -8740588384831342173}
304+
m_ShaderVariantRegistry:
305+
m_ErrorOnUnregisteredVariant: 0
306+
m_OptionalShaders: []
307+
m_UnregisteredVariants: []
308+
m_Asset: {fileID: -8740588384831342173}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using UnityEditor;
22
using UnityEngine;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Reflection;
3+
using Coffee.UIEffectInternal;
64
using UnityEditorInternal;
75

86
namespace Coffee.UIEffects.Editors
@@ -11,16 +9,14 @@ namespace Coffee.UIEffects.Editors
119
public class UIEffectProjectSettingsEditor : Editor
1210
{
1311
private ReorderableList _reorderableList;
14-
private SerializedProperty _fallbackVariantBehaviour;
1512
private SerializedProperty _transformSensitivity;
16-
private Editor _editor;
1713
private bool _isInitialized;
14+
private ShaderVariantRegistryEditor _shaderVariantRegistryEditor;
1815

1916
private void InitializeIfNeeded()
2017
{
2118
if (_isInitialized) return;
2219

23-
_fallbackVariantBehaviour = serializedObject.FindProperty("m_FallbackVariantBehaviour");
2420
_transformSensitivity = serializedObject.FindProperty("m_TransformSensitivity");
2521
var runtimePresets = serializedObject.FindProperty("m_RuntimePresets");
2622
_reorderableList = new ReorderableList(serializedObject, runtimePresets, true, true, true, true);
@@ -53,19 +49,11 @@ private void InitializeIfNeeded()
5349
menu.DropDown(rect);
5450
};
5551

56-
var collection = serializedObject.FindProperty("m_ShaderVariantCollection").objectReferenceValue;
57-
_editor = CreateEditor(collection);
5852
_isInitialized = true;
5953
}
6054

6155
private void OnDisable()
6256
{
63-
if (_editor)
64-
{
65-
DestroyImmediate(_editor);
66-
}
67-
68-
_editor = null;
6957
_isInitialized = false;
7058
}
7159

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

81-
// Editor
82-
EditorGUILayout.PropertyField(_fallbackVariantBehaviour);
83-
serializedObject.ApplyModifiedProperties();
84-
85-
// Shader
86-
EditorGUILayout.Space(20);
87-
EditorGUILayout.LabelField("Shader Variants", EditorStyles.boldLabel);
88-
EditorGUILayout.Space(-12);
89-
DrawUnregisteredShaderVariants(UIEffectProjectSettings.instance.m_UnregisteredShaderVariants);
90-
DrawRegisteredShaderVariants(_editor);
91-
GUILayout.FlexibleSpace();
92-
}
93-
94-
private static void DrawUnregisteredShaderVariants(List<string> variants)
95-
{
96-
if (variants.Count == 0) return;
97-
69+
// Shader registry
9870
EditorGUILayout.Space();
99-
var array = variants.ToArray();
100-
var r = EditorGUILayout.GetControlRect(false, 20);
101-
var rLabel = new Rect(r.x, r.y, r.width - 80, r.height);
102-
EditorGUI.LabelField(rLabel, "Registered Shader Variants");
103-
104-
var rButtonClear = new Rect(r.x + r.width - 80, r.y + 2, 80, r.height - 4);
105-
if (GUI.Button(rButtonClear, "Clear All", EditorStyles.miniButton))
71+
EditorGUILayout.LabelField("Shader", EditorStyles.boldLabel);
72+
if (_shaderVariantRegistryEditor == null)
10673
{
107-
UIEffectProjectSettings.ClearUnregisteredShaderVariants();
74+
var property = serializedObject.FindProperty("m_ShaderVariantRegistry");
75+
_shaderVariantRegistryEditor = new ShaderVariantRegistryEditor(property, "(UIEffect)");
10876
}
10977

110-
EditorGUILayout.BeginVertical("RL Background");
111-
for (var i = 0; i < array.Length; i++)
112-
{
113-
var values = array[i].Split(';');
114-
r = EditorGUILayout.GetControlRect();
115-
var rShader = new Rect(r.x, r.y, 150, r.height);
116-
EditorGUI.ObjectField(rShader, Shader.Find(values[0]), typeof(Shader), false);
117-
118-
var rKeywords = new Rect(r.x + 150, r.y, r.width - 150 - 20, r.height);
119-
EditorGUI.TextField(rKeywords, values[1]);
120-
121-
var rButton = new Rect(r.x + r.width - 20 + 2, r.y, 20, r.height);
122-
if (GUI.Button(rButton, EditorGUIUtility.IconContent("icons/toolbar plus.png"), "iconbutton"))
123-
{
124-
UIEffectProjectSettings.RegisterVariant(array[i]);
125-
}
126-
}
127-
128-
EditorGUILayout.EndVertical();
129-
}
130-
131-
private static void DrawRegisteredShaderVariants(Editor editor)
132-
{
133-
var collection = editor.target as ShaderVariantCollection;
134-
if (collection == null) return;
135-
136-
EditorGUILayout.Space();
137-
var r = EditorGUILayout.GetControlRect(false, 20);
138-
var rLabel = new Rect(r.x, r.y, r.width - 80, r.height);
139-
EditorGUI.LabelField(rLabel, "Registered Shader Variants");
140-
141-
var rButton = new Rect(r.x + r.width - 80, r.y + 2, 80, r.height - 4);
142-
if (GUI.Button(rButton, "Clear All", EditorStyles.miniButton))
143-
{
144-
collection.Clear();
145-
}
146-
147-
EditorGUILayout.BeginVertical("RL Background");
148-
editor.serializedObject.Update();
149-
var shaders = editor.serializedObject.FindProperty("m_Shaders");
150-
for (var i = 0; i < shaders.arraySize; i++)
151-
{
152-
s_MiDrawShaderEntry.Invoke(editor, new object[] { i });
153-
}
154-
155-
editor.serializedObject.ApplyModifiedProperties();
156-
EditorGUILayout.EndVertical();
78+
_shaderVariantRegistryEditor.Draw();
79+
GUILayout.FlexibleSpace();
80+
serializedObject.ApplyModifiedProperties();
15781
}
158-
159-
private static readonly MethodInfo s_MiDrawShaderEntry =
160-
Type.GetType("UnityEditor.ShaderVariantCollectionInspector, UnityEditor")
161-
?.GetMethod("DrawShaderEntry", BindingFlags.NonPublic | BindingFlags.Instance);
16282
}
16383
}

Packages/src/README.md

+22-13
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Combine various filters, such as grayscale, blur, and dissolve, to decorate your
3232
- [Install via UPM (with Package Manager UI)](#install-via-upm-with-package-manager-ui)
3333
- [Install via UPM (Manually)](#install-via-upm-manually)
3434
- [Install as Embedded Package](#install-as-embedded-package)
35-
- [Additional Resource Imports](#additional-resource-imports)
35+
- [Import Additional Resources](#import-additional-resources)
3636
- [🔄 Upgrading from v4 to v5](#-upgrading-from-v4-to-v5)
3737
- [🚀 Usage](#-usage)
3838
- [Getting Started](#getting-started)
@@ -122,16 +122,17 @@ _This package requires **Unity 2020.3 or later**._
122122

123123
### Install as Embedded Package
124124

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

132-
### Additional Resource Imports
133+
### Import Additional Resources
133134

134-
UIEffect includes additional resources to import.
135+
Additional resources can be imported to extend functionality.
135136

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

302303
### Project Settings
303304

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

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

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

317326
<br><br>
318327

Packages/src/Runtime/Internal/Extensions/TransformExtensions.cs

-144
This file was deleted.

Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Coffee.UIEffectInternal
1414
public abstract class PreloadedProjectSettings : ScriptableObject
1515
#if UNITY_EDITOR
1616
{
17-
private class MyAllPostprocessor : AssetPostprocessor
17+
private class Postprocessor : AssetPostprocessor
1818
{
1919
private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___, string[] ____)
2020
{
@@ -47,6 +47,11 @@ private static void Initialize()
4747
{
4848
SetDefaultSettings(defaultSettings);
4949
}
50+
51+
if (defaultSettings)
52+
{
53+
defaultSettings.OnInitialize();
54+
}
5055
}
5156
}
5257

@@ -110,6 +115,10 @@ protected static void SetDefaultSettings(PreloadedProjectSettings asset)
110115
protected virtual void OnCreateAsset()
111116
{
112117
}
118+
119+
protected virtual void OnInitialize()
120+
{
121+
}
113122
}
114123
#else
115124
{

0 commit comments

Comments
 (0)