Skip to content

Commit 037f562

Browse files
committed
【AotuChangeMatShader工具】
1 parent 642b498 commit 037f562

File tree

5 files changed

+363
-2
lines changed

5 files changed

+363
-2
lines changed

Assets/Editor/AotuChangeMatShader.cs

+342
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text.RegularExpressions;
6+
//using Miscellaneous;
7+
//using Sirenix.Utilities;
8+
using UnityEditor;
9+
using UnityEditor.Experimental.AssetImporters;
10+
using UnityEngine;
11+
using Object = UnityEngine.Object;
12+
13+
/// <summary>
14+
/// 自动修改特定路径下Mat的Shader
15+
/// </summary>
16+
public class AutoChangeMatShader : AssetPostprocessor
17+
{
18+
19+
//static readonly AssetProcessor[] assetProcessors =
20+
// {
21+
// new DefaultProcessor(),
22+
// new EffectProcessor(),
23+
// new ModelProcessor(),
24+
// new SceneProcessor(),
25+
// new ShaderProcessor(),
26+
// new AnimatorProcessor(),
27+
// new UIImageProcessor(),
28+
// new UILayoutProcessor(),
29+
// new ConfigAssetProcessor(),
30+
// new PlayableProcessor(),
31+
// };
32+
33+
//public void ResetAllAssetBundleName()
34+
//{
35+
// foreach (var assetPath in AssetDatabase.GetAllAssetPaths())
36+
// {
37+
// ResetAssetBundleName(assetPath);
38+
// }
39+
//}
40+
41+
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
42+
{
43+
//var iterAssets = importedAssets.Union(movedAssets);
44+
//foreach (var assetPath in iterAssets)
45+
//{
46+
// ResetAssetBundleName(assetPath);
47+
// foreach (var processor in assetProcessors)
48+
// {
49+
// if (processor.Filter(assetPath))
50+
// {
51+
// processor.DoProcessor(assetPath);
52+
// break;
53+
// }
54+
// }
55+
//}
56+
// trick (lixiaolu)
57+
string path = "Resources/Materials"; //需要替换的路径
58+
foreach (string asset in importedAssets)
59+
{
60+
61+
if (asset.EndsWith(".mat") && asset.Contains(path))
62+
{
63+
OnPostprocessMaterial(AssetDatabase.LoadAssetAtPath<Material>(asset));
64+
}
65+
// asset => asset.EndsWith(".mat")).ForEach(material => OnPostprocessMaterial(AssetDatabase.LoadAssetAtPath<Material>(material))
66+
67+
}
68+
// importedAssets.Where(asset => asset.EndsWith(".mat")).ForEach(material => OnPostprocessMaterial(AssetDatabase.LoadAssetAtPath<Material>(material)));
69+
}
70+
//private static void ResetAssetBundleName(string assetPath)
71+
//{
72+
// if (!assetPath.StartsWith("Assets/GameAssets/"))
73+
// {
74+
// if (!assetPath.EndsWith(".cs"))
75+
// {
76+
// var importer = AssetImporter.GetAtPath(assetPath);
77+
// importer.assetBundleName = null;
78+
// importer.userData = null;
79+
// }
80+
// return;
81+
// }
82+
// foreach (var processor in assetProcessors)
83+
// {
84+
// if (processor.Filter(assetPath))
85+
// {
86+
// AssetImporter.GetAtPath(assetPath).assetBundleName = processor.GenerateAssetBundleName(assetPath);
87+
// return;
88+
// }
89+
// }
90+
// AssetImporter.GetAtPath(assetPath).assetBundleName = null;
91+
//}
92+
93+
static void OnPostprocessMaterial(Material material)
94+
{
95+
var shader = Shader.Find("FX/Flare"); //要替换成的Shader
96+
var discards = new List<string> //如果监测到有这些shader,都会替换成上面的shader
97+
{
98+
"Standard",
99+
"Standard (Specular setup)",
100+
"Standard (Roughness setup)",
101+
"Custom/SceneObject",
102+
"Custom/SceneObject (Specular setup)"
103+
};
104+
if (discards.Any(s => s.Equals(material.shader.name)))
105+
{
106+
Debug.Log(string.Format("replace shader {0} to 我的SHADER", material.shader.name));
107+
material.shader = shader;
108+
}
109+
if (material.shader.name.Contains("InternalErrorShader"))
110+
{
111+
Debug.Log(string.Format("material [{0}] shader error: InternalErrorShader", material.name));
112+
}
113+
}
114+
115+
//void OnPostprocessTexture(Texture2D texture)
116+
//{
117+
// string configPath = "Assets/Editor/Miscellaneous/AssetProcessor/ResourcesPostProcessingConfig.asset";
118+
// ResourcesPostProcessingConfig rppConfig =
119+
// AssetDatabase.LoadAssetAtPath<ResourcesPostProcessingConfig>(configPath);
120+
121+
// List<string> specialPaths = new List<string>();
122+
// if (rppConfig != null)
123+
// {
124+
// for (int i = 0; i < rppConfig.SpecialPaths.Length; i++)
125+
// {
126+
// specialPaths.Add(rppConfig.SpecialPaths[i].DirPath.Replace("\\", "/"));
127+
// }
128+
// }
129+
130+
// bool isInSpecialDir = false;
131+
// for (int i = 0; i < specialPaths.Count; i++)
132+
// {
133+
// if (assetPath.Contains(specialPaths[i]))
134+
// {
135+
// isInSpecialDir = true;
136+
// break;
137+
// }
138+
// }
139+
140+
// var inSceneDir = assetPath.Contains("Assets/GameAssets/Scenes");
141+
// var inModelDir = assetPath.Contains("Assets/GameAssets/Models");
142+
// var inPlayerDir = assetPath.Contains("Assets/GameAssets/Models/Player");
143+
// var isTerrainControl = inSceneDir && assetPath.Contains("/Terrains/");
144+
// var inGroundDir = assetPath.Contains("Assets/GameAssets/Scenes/Sce_Models/Common/Groudmap");
145+
// var inBossDir = assetPath.Contains("Assets/GameAssets/Models/Monster/BOSS");
146+
// var importer = assetImporter as TextureImporter;
147+
// if (!isInSpecialDir && !isTerrainControl && (inSceneDir || inModelDir))
148+
// {
149+
// if (importer != null)
150+
// {
151+
// importer.isReadable = false;
152+
// var lightmapDir = new Regex("Lightmap-(\\d+)_comp_dir.png");
153+
// var isLigthmapDir = lightmapDir.IsMatch(Path.GetFileName(assetPath));
154+
// var lightmapBase = new Regex("Lightmap-(\\d+)_comp_light.exr");
155+
// var isLightmapColor = lightmapBase.IsMatch(Path.GetFileName(assetPath));
156+
// var lightmapShadowMask = new Regex("Lightmap-(\\d+)_comp_shadowmask.png");
157+
// var isLightmapShadowMask = lightmapShadowMask.IsMatch(Path.GetFileName(assetPath));
158+
159+
// var isLightmap = isLigthmapDir || isLightmapColor || isLightmapShadowMask;
160+
// var medium = inPlayerDir || isLightmapColor || inGroundDir || inBossDir;
161+
// importer.mipmapEnabled = (inSceneDir && !isLightmap) || inPlayerDir || inBossDir;
162+
// var platforms = new List<string> { "iPhone", "Android", "Standalone" };
163+
// platforms.ForEach(platform =>
164+
// {
165+
// var setting = new TextureImporterPlatformSettings
166+
// {
167+
// name = platform,
168+
// overridden = true,
169+
// maxTextureSize = isLightmap ? 1024 : (medium ? 512 : 256),
170+
// crunchedCompression = true,
171+
// textureCompression = TextureImporterCompression.Compressed
172+
// };
173+
// importer.SetPlatformTextureSettings(setting);
174+
// });
175+
// }
176+
// }
177+
178+
// if (isTerrainControl)
179+
// {
180+
// importer.isReadable = false;
181+
// var platforms = new List<string> { "iPhone", "Standalone" };
182+
// platforms.ForEach(platform =>
183+
// {
184+
// var settings = new TextureImporterPlatformSettings
185+
// {
186+
// name = platform,
187+
// overridden = true,
188+
// maxTextureSize = 512,
189+
// crunchedCompression = true,
190+
// textureCompression = TextureImporterCompression.Compressed
191+
// };
192+
// importer.SetPlatformTextureSettings(settings);
193+
// });
194+
195+
// var androidSetting = new TextureImporterPlatformSettings
196+
// {
197+
// name = "Android",
198+
// overridden = true,
199+
// maxTextureSize = 512,
200+
// crunchedCompression = false,
201+
// textureCompression = TextureImporterCompression.Uncompressed
202+
// };
203+
// importer.SetPlatformTextureSettings(androidSetting);
204+
// }
205+
206+
// var ui = "Assets/GameAssets/UI/SpriteSheet";
207+
// if (assetPath.Contains(ui))
208+
// {
209+
// importer.isReadable = false;
210+
// var platforms = new List<string> { "iPhone", "Android", "Standalone" };
211+
// platforms.ForEach(platform =>
212+
// {
213+
// var setting = new TextureImporterPlatformSettings
214+
// {
215+
// name = platform,
216+
// overridden = true,
217+
// textureCompression = TextureImporterCompression.Compressed
218+
// };
219+
// importer.SetPlatformTextureSettings(setting);
220+
// });
221+
// }
222+
//}
223+
224+
//void OnPostprocessModel(GameObject o)
225+
//{
226+
// var importer = assetImporter as ModelImporter;
227+
// importer.importMaterials = true;
228+
// importer.materialLocation = ModelImporterMaterialLocation.External;
229+
// var path = "Assets/GameAssets/Materials/default.mat";
230+
// var mat = new[] { AssetDatabase.LoadAssetAtPath<Material>(path) };
231+
// Renderer[] renders = o.GetComponentsInChildren<Renderer>();
232+
// for (int i = 0; i < renders.Length; i++)
233+
// {
234+
// for (int j = 0; j < renders[i].sharedMaterials.Length; j++)
235+
// {
236+
// renders[i].sharedMaterials[j] = mat[0];
237+
// }
238+
// }
239+
//}
240+
241+
//[MenuItem("Build/AssetBundle/CheckNamesForSelection")]
242+
//public static void CheckNamesForSelection()
243+
//{
244+
// var selectedAsset = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
245+
246+
// for (var i = 0; i < selectedAsset.Length; i++)
247+
// {
248+
// var item = selectedAsset[i];
249+
// var path = AssetDatabase.GetAssetPath(item.GetInstanceID());
250+
// EditorUtility.DisplayProgressBar("Check Asset Bundle Names", path, (float)i / selectedAsset.Length);
251+
252+
// if (Directory.Exists(path))
253+
// continue;
254+
255+
// if (path.StartsWith("Assets/GameAssets/")
256+
// && !path.Contains("Main.unity")
257+
// && !path.Contains("DlgFactory.unity"))
258+
// {
259+
// var importer = AssetImporter.GetAtPath(path);
260+
// if (string.IsNullOrEmpty(importer.assetBundleName))
261+
// {
262+
// Debug.LogError(path + " is not set AssetBundle Name. You must reimport the asset.");
263+
// }
264+
// }
265+
// }
266+
// EditorUtility.ClearProgressBar();
267+
// Debug.Log("CheckNamesForSelection Finished.");
268+
//}
269+
270+
//[MenuItem("Build/AssetBundle/ClearBundleNameForSelection")]
271+
//public static void ClearBundleNameForSelection()
272+
//{
273+
// var selectedAsset = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
274+
// for (var i = 0; i < selectedAsset.Length; i++)
275+
// {
276+
// var item = selectedAsset[i];
277+
// var path = AssetDatabase.GetAssetPath(item.GetInstanceID());
278+
// EditorUtility.DisplayProgressBar("Clear Bundle Names", path, (float)i / selectedAsset.Length);
279+
// var importer = AssetImporter.GetAtPath(path);
280+
// if (importer)
281+
// {
282+
// try
283+
// {
284+
// importer.assetBundleName = string.Empty;
285+
// }
286+
// catch (Exception ex)
287+
// {
288+
// }
289+
// }
290+
// }
291+
// EditorUtility.ClearProgressBar();
292+
// Debug.Log("ClearBundleNameForSelection Finished.");
293+
// AssetDatabase.RemoveUnusedAssetBundleNames();
294+
//}
295+
296+
//[MenuItem("Build/Textures/InitDefaultConfig")]
297+
//public static void InitDefaultTextureConfig()
298+
//{
299+
// var selectedAsset = Selection.GetFiltered(typeof(UnityEngine.Texture), SelectionMode.DeepAssets);
300+
// for (int i = 0; i < selectedAsset.Length; i++)
301+
// {
302+
// Texture2D tex = selectedAsset[i] as Texture2D;
303+
// TextureImporter ti = (TextureImporter)TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(tex));
304+
// var scene = "Assets/GameAssets/Scenes";
305+
// var model = "Assets/GameAssets/Models";
306+
// var path = ti.assetPath;
307+
// if (path.Contains(scene) || path.Contains(model))
308+
// {
309+
// if (ti != null)
310+
// {
311+
// var lightmapDir = new Regex("Lightmap-(\\d+)_comp_dir.png");
312+
// var lightmapBase = new Regex("Lightmap-(\\d+)_comp_light.exr");
313+
// var lightmapShadowMask = new Regex("Lightmap-(\\d+)_comp_shadowmask.png");
314+
315+
// var usedForLightmap = lightmapDir.IsMatch(Path.GetFileName(path)) ||
316+
// lightmapBase.IsMatch(Path.GetFileName(path)) ||
317+
// lightmapShadowMask.IsMatch(Path.GetFileName(path));
318+
// var uncompress = lightmapDir.IsMatch(Path.GetFileName(path));
319+
// var platforms = new List<string> { "iPhone", "Android", "Standalone" };
320+
// platforms.ForEach(platform =>
321+
// {
322+
// var setting = new TextureImporterPlatformSettings
323+
// {
324+
// name = platform,
325+
// overridden = true,
326+
// maxTextureSize = usedForLightmap ? 1024 : 512,
327+
// crunchedCompression = true,
328+
// textureCompression = uncompress
329+
// ? TextureImporterCompression.Uncompressed
330+
// : TextureImporterCompression.Compressed
331+
// };
332+
// ti.SetPlatformTextureSettings(setting);
333+
// });
334+
// AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tex));
335+
// }
336+
337+
// }
338+
// }
339+
340+
//}
341+
342+
}

Assets/Editor/AotuChangeMatShader.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Resources/Materials.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Resources/Textures/zed.jpg.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"com.unity.ads": "2.0.8",
44
"com.unity.analytics": "3.2.3",
55
"com.unity.collab-proxy": "1.2.15",
6-
"com.unity.package-manager-ui": "2.0.8",
6+
"com.unity.package-manager-ui": "2.0.13",
77
"com.unity.purchasing": "2.0.3",
88
"com.unity.textmeshpro": "1.4.1",
99
"com.unity.modules.ai": "1.0.0",

0 commit comments

Comments
 (0)