1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using UnityEngine ;
3
- using System ;
4
+ using UnityEngine . Profiling ;
4
5
#if UNITY_EDITOR
5
6
using System . IO ;
6
- using Object = UnityEngine . Object ;
7
7
using System . Linq ;
8
8
using System . Reflection ;
9
9
using System . Text . RegularExpressions ;
10
10
using UnityEditor ;
11
+ using UnityEditor . PackageManager . UI ;
11
12
using UnityEditorInternal ;
13
+ using PackageInfo = UnityEditor . PackageManager . PackageInfo ;
14
+ using Object = UnityEngine . Object ;
12
15
#endif
13
16
14
17
namespace Coffee . UIEffectInternal
@@ -75,7 +78,7 @@ public Shader FindOptionalShader(Shader shader,
75
78
return Shader . Find ( optionalShaderName ) ;
76
79
}
77
80
78
- // Required shader.
81
+ // The shader has
79
82
if ( shader . name . Contains ( requiredName ) )
80
83
{
81
84
_cachedOptionalShaders [ id ] = shader . name ;
@@ -105,16 +108,70 @@ public Shader FindOptionalShader(Shader shader,
105
108
return optionalShader ;
106
109
}
107
110
111
+ #if UNITY_EDITOR
112
+ ImportFromSample ( optionalShaderName ) ;
113
+ #endif
114
+
108
115
// Find default optional shader.
109
116
_cachedOptionalShaders [ id ] = defaultOptionalShaderName ;
110
117
return Shader . Find ( defaultOptionalShaderName ) ;
111
118
}
112
119
113
120
#if UNITY_EDITOR
114
- private HashSet < StringPair > _logVariants = new HashSet < StringPair > ( ) ;
121
+ private readonly HashSet < StringPair > _logVariants = new HashSet < StringPair > ( ) ;
122
+ private readonly Dictionary < string , string > _sampleNames = new Dictionary < string , string > ( ) ;
123
+
124
+ /// <summary>
125
+ /// Import the sample containing the requested shader.
126
+ /// If choice 'Import' is selected, the sample is imported.
127
+ /// If choice 'Skip in this session' is selected, the sample is skipped in this session.
128
+ /// </summary>
129
+ public void ImportFromSample ( string shaderName )
130
+ {
131
+ if ( Misc . isBatchOrBuilding ) return ;
132
+
133
+ // Find sample name.
134
+ if ( _sampleNames . TryGetValue ( shaderName , out var sampleName ) )
135
+ {
136
+ // Find package info.
137
+ var pInfo = PackageInfo . FindForAssembly ( typeof ( ShaderVariantRegistry ) . Assembly ) ;
138
+ if ( pInfo == null ) return ;
139
+
140
+ // Find sample. If not found (resolvedPath == null), skip.
141
+ var sample = Sample . FindByPackage ( pInfo . name , pInfo . version )
142
+ . FirstOrDefault ( x => x . displayName == sampleName ) ;
143
+ if ( sample . resolvedPath == null ) return ;
144
+
145
+ // Import the sample if selected.
146
+ var importSelected = EditorUtility . DisplayDialog ( $ "Import { sampleName } ",
147
+ $ "Import '{ sampleName } ' to use the shader '{ shaderName } '", "Import" , "Cancel" ) ;
148
+ if ( importSelected )
149
+ {
150
+ EditorApplication . delayCall += ( ) =>
151
+ {
152
+ sample . Import ( ) ;
153
+ } ;
154
+ }
155
+ }
156
+ }
157
+
158
+ public void ClearCache ( )
159
+ {
160
+ _cachedOptionalShaders . Clear ( ) ;
161
+ }
162
+
163
+ public void RegisterSamples ( ( string shaderName , string sampleName ) [ ] samples )
164
+ {
165
+ foreach ( var ( shaderName , sampleName ) in samples )
166
+ {
167
+ _sampleNames [ shaderName ] = sampleName ;
168
+ }
169
+ }
115
170
116
171
public void InitializeIfNeeded ( Object owner , string optionalName )
117
172
{
173
+ Profiler . BeginSample ( "(EDITOR/COF)[ShaderVariantRegistry] InitializeIfNeeded" ) ;
174
+
118
175
// Register optional shader names by shader comment.
119
176
if ( ! string . IsNullOrEmpty ( optionalName ) )
120
177
{
@@ -159,12 +216,16 @@ public void InitializeIfNeeded(Object owner, string optionalName)
159
216
EditorUtility . SetDirty ( owner ) ;
160
217
AssetDatabase . SaveAssets ( ) ;
161
218
}
219
+
220
+ ClearCache ( ) ;
221
+ Profiler . EndSample ( ) ;
162
222
}
163
223
164
224
internal void RegisterVariant ( Material material , string path )
165
225
{
166
226
if ( ! material || ! material . shader || ! m_Asset ) return ;
167
227
228
+ Profiler . BeginSample ( "(EDITOR/COF)[ShaderVariantRegistry] RegisterVariant" ) ;
168
229
var shaderName = material . shader . name ;
169
230
var validKeywords = material . shaderKeywords
170
231
. Where ( x => ! Regex . IsMatch ( x , "(_EDITOR|EDITOR_)" ) )
@@ -181,6 +242,7 @@ internal void RegisterVariant(Material material, string path)
181
242
if ( m_Asset . Contains ( variant ) )
182
243
{
183
244
m_UnregisteredVariants . Remove ( pair ) ;
245
+ Profiler . EndSample ( ) ;
184
246
return ;
185
247
}
186
248
@@ -199,11 +261,13 @@ internal void RegisterVariant(Material material, string path)
199
261
$ "Register it in 'ProjectSettings > { path } ' to use it in player.", m_Asset ) ;
200
262
}
201
263
264
+ Profiler . EndSample ( ) ;
202
265
return ;
203
266
}
204
267
205
268
m_Asset . Add ( variant ) ;
206
269
m_UnregisteredVariants . Remove ( pair ) ;
270
+ Profiler . EndSample ( ) ;
207
271
}
208
272
#endif
209
273
}
0 commit comments