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