1
+ using System ;
1
2
using UnityEditor . Build ;
2
3
using UnityEngine ;
3
- using UnityEngine . SceneManagement ;
4
4
using UnityEngine . Rendering . PostProcessing ;
5
5
6
6
namespace UnityEditor . Rendering . PostProcessing
7
7
{
8
8
public sealed class PostProcessResourceStripper : ScriptableObject
9
9
{
10
- public const string DefaultStrippingConfigAssetPath = "Assets/PostProcessStrippingConfig.asset" ;
11
-
12
- PostProcessStrippingConfig stripping ;
13
- PostProcessStrippingConfig defaultConfig ;
10
+ [ SerializeField ] private PostProcessResources resources ;
11
+ [ SerializeField ] private PostProcessResources unstrippedResources ;
12
+ [ SerializeField ] private PostProcessStrippingConfig stripping ;
14
13
15
- [ SerializeField ]
16
- PostProcessResources unstrippedResources ;
14
+ public const string DefaultStrippingConfigAssetPath = "Assets/PostProcessStrippingConfig.asset" ;
15
+ bool enabled = true ;
17
16
18
17
static PostProcessResourceStripper s_Instance ;
19
18
@@ -24,52 +23,14 @@ public static PostProcessResourceStripper instance
24
23
if ( s_Instance == null )
25
24
{
26
25
s_Instance = CreateInstance < PostProcessResourceStripper > ( ) ;
27
- s_Instance . defaultConfig = CreateInstance < PostProcessStrippingConfig > ( ) ;
26
+ s_Instance . unstrippedResources . changeHandler = Update ;
28
27
}
29
28
30
29
return s_Instance ;
31
30
}
32
31
}
33
32
34
- void Awake ( )
35
- {
36
- SceneManager . sceneLoaded += OnSceneLoaded ;
37
- }
38
-
39
- void OnDestroy ( )
40
- {
41
- SceneManager . sceneLoaded -= OnSceneLoaded ;
42
- }
43
-
44
- static void StripMultiScaleAO ( PostProcessResources resources )
45
- {
46
- resources . computeShaders . multiScaleAODownsample1 = null ;
47
- resources . computeShaders . multiScaleAODownsample2 = null ;
48
- resources . computeShaders . multiScaleAORender = null ;
49
- resources . computeShaders . multiScaleAOUpsample = null ;
50
- resources . shaders . multiScaleAO = null ;
51
- }
52
-
53
- static void StripScreenSpaceReflections ( PostProcessResources resources )
54
- {
55
- resources . shaders . screenSpaceReflections = null ;
56
- resources . computeShaders . gaussianDownsample = null ;
57
- }
58
-
59
- static void StripDebugShaders ( PostProcessResources resources )
60
- {
61
- resources . shaders . lightMeter = null ;
62
- resources . shaders . gammaHistogram = null ;
63
- resources . shaders . waveform = null ;
64
- resources . shaders . vectorscope = null ;
65
- resources . shaders . debugOverlays = null ;
66
-
67
- resources . computeShaders . gammaHistogram = null ;
68
- resources . computeShaders . waveform = null ;
69
- resources . computeShaders . vectorscope = null ;
70
- }
71
-
72
- static string FindPostProcessStrippingConfigGUID ( )
33
+ static private string FindPostProcessStrippingConfigGUID ( )
73
34
{
74
35
var guids = AssetDatabase . FindAssets ( "t:PostProcessStrippingConfig" , null ) ;
75
36
if ( guids . Length > 0 )
@@ -78,65 +39,82 @@ static string FindPostProcessStrippingConfigGUID()
78
39
return null ;
79
40
}
80
41
81
- static public void EnsurePostProcessStrippingConfigAssetExists ( )
42
+ static public string EnsurePostProcessStrippingConfigAssetExists ( )
82
43
{
83
44
var guid = FindPostProcessStrippingConfigGUID ( ) ;
84
45
if ( guid != null )
85
- return ;
46
+ return guid ;
86
47
48
+ bool wasEnabled = instance . enabled ;
49
+ instance . enabled = false ;
87
50
AssetDatabase . CreateAsset ( CreateInstance < PostProcessStrippingConfig > ( ) , DefaultStrippingConfigAssetPath ) ;
88
51
AssetDatabase . SaveAssets ( ) ;
89
52
AssetDatabase . Refresh ( ) ;
53
+ instance . enabled = wasEnabled ;
54
+ return FindPostProcessStrippingConfigGUID ( ) ;
90
55
}
91
56
92
- void LazyLoadStrippingConfig ( )
57
+ private void LazyLoadStrippingConfig ( )
93
58
{
94
59
if ( stripping != null )
95
60
return ;
96
61
97
- var guid = FindPostProcessStrippingConfigGUID ( ) ;
62
+ var guid = EnsurePostProcessStrippingConfigAssetExists ( ) ;
98
63
if ( guid != null )
99
64
{
100
- stripping = ( PostProcessStrippingConfig ) AssetDatabase . LoadAssetAtPath ( AssetDatabase . GUIDToAssetPath ( guid ) , typeof ( PostProcessStrippingConfig ) ) ;
65
+ bool wasEnabled = instance . enabled ;
66
+ instance . enabled = false ;
67
+ stripping = ( PostProcessStrippingConfig ) AssetDatabase . LoadAssetAtPath ( AssetDatabase . GUIDToAssetPath ( guid ) , typeof ( PostProcessStrippingConfig ) ) ;
68
+ instance . enabled = wasEnabled ;
101
69
}
102
-
103
- if ( stripping == null )
104
- stripping = defaultConfig ;
105
70
}
106
71
107
- void SetConfig ( PostProcessStrippingConfig config )
72
+ void OnDestroy ( )
108
73
{
109
- if ( config == stripping )
110
- return ;
74
+ unstrippedResources . changeHandler = null ;
75
+ }
111
76
112
- if ( defaultConfig == null )
113
- return ;
77
+ private void StripMultiScaleAO ( )
78
+ {
79
+ resources . computeShaders . multiScaleAODownsample1 = null ;
80
+ resources . computeShaders . multiScaleAODownsample2 = null ;
81
+ resources . computeShaders . multiScaleAORender = null ;
82
+ resources . computeShaders . multiScaleAOUpsample = null ;
83
+ resources . shaders . multiScaleAO = null ;
84
+ }
114
85
115
- if ( config == defaultConfig )
116
- return ;
86
+ private void StripScreenSpaceReflections ( )
87
+ {
88
+ resources . shaders . screenSpaceReflections = null ;
89
+ resources . computeShaders . gaussianDownsample = null ;
90
+ }
117
91
118
- if ( config == null )
119
- {
120
- stripping = defaultConfig ;
121
- return ;
122
- }
92
+ private void StripDebugShaders ( )
93
+ {
94
+ resources . shaders . lightMeter = null ;
95
+ resources . shaders . gammaHistogram = null ;
96
+ resources . shaders . waveform = null ;
97
+ resources . shaders . vectorscope = null ;
98
+ resources . shaders . debugOverlays = null ;
123
99
124
- stripping = config ;
100
+ resources . computeShaders . gammaHistogram = null ;
101
+ resources . computeShaders . waveform = null ;
102
+ resources . computeShaders . vectorscope = null ;
125
103
}
126
104
127
- void Apply ( BuildTarget target , PostProcessResources resources )
105
+ private void Apply ( BuildTarget target )
128
106
{
129
- if ( defaultConfig == null )
107
+ if ( ! enabled )
130
108
return ;
131
109
132
- LazyLoadStrippingConfig ( ) ;
133
- if ( stripping == null )
110
+ if ( resources == null )
134
111
return ;
135
112
136
113
if ( unstrippedResources == null )
137
114
return ;
138
115
139
- if ( resources == null )
116
+ LazyLoadStrippingConfig ( ) ;
117
+ if ( stripping == null )
140
118
return ;
141
119
142
120
resources . computeShaders = unstrippedResources . computeShaders . Clone ( ) ;
@@ -146,26 +124,26 @@ void Apply(BuildTarget target, PostProcessResources resources)
146
124
if ( stripping . stripUnsupportedShaders &&
147
125
( target == BuildTarget . Android || target == BuildTarget . iOS || target == BuildTarget . tvOS ) )
148
126
{
149
- StripMultiScaleAO ( resources ) ;
127
+ StripMultiScaleAO ( ) ;
150
128
}
151
129
152
130
if ( stripping . stripDebugShaders )
153
131
{
154
- StripDebugShaders ( resources ) ;
132
+ StripDebugShaders ( ) ;
155
133
}
156
134
157
135
if ( stripping . stripComputeShaders )
158
136
{
159
137
resources . computeShaders = new PostProcessResources . ComputeShaders ( ) ;
160
138
resources . shaders . autoExposure = null ;
161
- StripScreenSpaceReflections ( resources ) ;
162
- StripMultiScaleAO ( resources ) ;
163
- StripDebugShaders ( resources ) ;
139
+ StripScreenSpaceReflections ( ) ;
140
+ StripMultiScaleAO ( ) ;
141
+ StripDebugShaders ( ) ;
164
142
}
165
143
166
144
if ( stripping . stripUnsupportedShaders && ! RuntimeUtilities . supportsDeferredShading )
167
145
{
168
- StripScreenSpaceReflections ( resources ) ;
146
+ StripScreenSpaceReflections ( ) ;
169
147
resources . shaders . deferredFog = null ;
170
148
if ( ! RuntimeUtilities . supportsDepthNormals )
171
149
resources . shaders . scalableAO = null ;
@@ -180,35 +158,14 @@ void Apply(BuildTarget target, PostProcessResources resources)
180
158
}
181
159
}
182
160
183
- void OnSceneLoaded ( Scene scene , LoadSceneMode mode )
161
+ public static void Update ( )
184
162
{
185
- StripAll ( ) ;
163
+ Update ( EditorUserBuildSettings . activeBuildTarget ) ;
186
164
}
187
165
188
- public static void Strip ( PostProcessResources resources )
166
+ public static void Update ( BuildTarget target )
189
167
{
190
- instance . Apply ( EditorUserBuildSettings . activeBuildTarget , resources ) ;
191
- }
192
-
193
- public static void StripAll ( BuildTarget target )
194
- {
195
- var allResources = PostProcessResourcesFactory . AllResources ( ) ;
196
- if ( allResources == null )
197
- return ;
198
-
199
- foreach ( var resources in allResources )
200
- instance . Apply ( EditorUserBuildSettings . activeBuildTarget , resources ) ;
201
- }
202
-
203
- public static void StripAll ( )
204
- {
205
- StripAll ( EditorUserBuildSettings . activeBuildTarget ) ;
206
- }
207
-
208
- public static void StripAll ( PostProcessStrippingConfig config )
209
- {
210
- instance . SetConfig ( config ) ;
211
- StripAll ( EditorUserBuildSettings . activeBuildTarget ) ;
168
+ instance . Apply ( EditorUserBuildSettings . activeBuildTarget ) ;
212
169
}
213
170
}
214
171
@@ -218,7 +175,7 @@ sealed class UpdateStrippingOnBuildTargetChange : IActiveBuildTargetChanged
218
175
public int callbackOrder { get { return 0 ; } }
219
176
public void OnActiveBuildTargetChanged ( BuildTarget previousTarget , BuildTarget newTarget )
220
177
{
221
- PostProcessResourceStripper . StripAll ( newTarget ) ;
178
+ PostProcessResourceStripper . Update ( newTarget ) ;
222
179
}
223
180
}
224
181
@@ -229,24 +186,26 @@ sealed class UpdateStrippingBeforeBuild : IPreprocessBuild
229
186
#if UNITY_2018_1_OR_NEWER
230
187
public void OnPreprocessBuild ( Build . Reporting . BuildReport report )
231
188
{
232
- PostProcessResourceStripper . StripAll ( report . summary . platform ) ;
189
+ PostProcessResourceStripper . Update ( report . summary . platform ) ;
233
190
}
191
+
234
192
#else
235
193
public void OnPreprocessBuild ( BuildTarget target , string path )
236
194
{
237
- PostProcessResourceStripper . StripAll ( target ) ;
195
+ PostProcessResourceStripper . Update ( target ) ;
238
196
}
197
+
239
198
#endif
240
199
}
241
200
#endif
242
201
202
+
243
203
[ InitializeOnLoad ]
244
- public class SetupStripping
204
+ public class SetupStrippingConfig
245
205
{
246
- static SetupStripping ( )
206
+ static SetupStrippingConfig ( )
247
207
{
248
208
PostProcessResourceStripper . EnsurePostProcessStrippingConfigAssetExists ( ) ;
249
- PostProcessResourcesFactory . Init ( PostProcessResourceStripper . Strip ) ;
250
209
}
251
210
}
252
211
}
0 commit comments