|
1 | 1 | using UnityEngine; |
2 | 2 | using UnityEditor; |
3 | 3 | using System.Collections.Generic; |
| 4 | +using System.Linq; |
4 | 5 | #if CONVERT_MATERIALS |
5 | 6 | using UnityEditor.Rendering; |
6 | 7 | using UnityEngine.Rendering; |
|
14 | 15 | #endif |
15 | 16 | #endif |
16 | 17 |
|
| 18 | +[InitializeOnLoad] |
17 | 19 | public static class MaterialPipelineConverter |
18 | 20 | { |
19 | | - const string _partialPath = "LoadingSceneExamples/Materials"; |
| 21 | + const string _partialPath = "Loading Scene Examples/Materials"; |
| 22 | + |
| 23 | + static MaterialPipelineConverter() |
| 24 | + { |
| 25 | + EditorApplication.delayCall += ConvertMaterials; |
| 26 | + } |
20 | 27 |
|
21 | 28 | public static void ConvertMaterials() |
22 | 29 | { |
| 30 | + RenderPipelineAsset renderPipeline = GraphicsSettings.currentRenderPipeline; |
| 31 | + if (!renderPipeline) |
| 32 | + return; |
| 33 | + |
23 | 34 | #if CONVERT_MATERIALS |
24 | | - string fullPath = FindFolderByPartialPath(_partialPath); |
25 | | - if (string.IsNullOrEmpty(fullPath)) |
| 35 | + string[] materialPaths = AssetDatabase.FindAssets("t:Material") |
| 36 | + .Select(AssetDatabase.GUIDToAssetPath) |
| 37 | + .Where(path => path.Contains(_partialPath)) |
| 38 | + .ToArray(); |
| 39 | + |
| 40 | + if (materialPaths.Length == 0) |
| 41 | + return; |
| 42 | + |
| 43 | + Material[] materials = materialPaths |
| 44 | + .Select(path => AssetDatabase.LoadAssetAtPath<Material>(path)) |
| 45 | + .Where(material => material != null && material.shader.name == "Standard") |
| 46 | + .ToArray(); |
| 47 | + |
| 48 | + if (materials.Length == 0) |
26 | 49 | return; |
27 | 50 |
|
28 | | - List<MaterialUpgrader> upgraders = GetMaterialUpgraders(); |
29 | | - if (upgraders.Count > 0) |
| 51 | + List<MaterialUpgrader> upgraders = GetMaterialUpgraders(renderPipeline); |
| 52 | + if (upgraders.Count > 0 && EditorUtility.DisplayDialog("Sample Material Upgrader", "The sample materials have to be upgraded to match your render pipeline. Would you like to upgrade them now?", "Upgrade", "Ignore")) |
30 | 53 | { |
31 | | - MaterialUpgrader.UpgradeProjectFolder(GetMaterialUpgraders(), fullPath); |
| 54 | + foreach (Material material in materials) |
| 55 | + { |
| 56 | + MaterialUpgrader.Upgrade(material, upgraders, MaterialUpgrader.UpgradeFlags.None); |
| 57 | + } |
32 | 58 | AssetDatabase.Refresh(); |
33 | 59 | } |
34 | 60 | #endif |
35 | 61 | } |
36 | 62 |
|
37 | 63 | #if CONVERT_MATERIALS |
38 | | - static List<MaterialUpgrader> GetMaterialUpgraders() |
| 64 | + static List<MaterialUpgrader> GetMaterialUpgraders(RenderPipelineAsset renderPipeline) |
39 | 65 | { |
40 | | - RenderPipelineAsset renderPipeline = GraphicsSettings.currentRenderPipeline; |
41 | 66 | List<MaterialUpgrader> upgraders = new(); |
42 | | -#if CONVERT_HDRP |
43 | | - if (renderPipeline is HDRenderPipelineAsset) |
44 | | - upgraders.Add(new StandardsToHDLitMaterialUpgrader("Standard", "HDRP/Lit")); |
45 | | -#endif |
46 | 67 | #if CONVERT_URP |
47 | 68 | if (renderPipeline is UniversalRenderPipelineAsset) |
48 | 69 | upgraders.Add(new StandardUpgrader("Standard")); |
| 70 | +#endif |
| 71 | +#if CONVERT_HDRP |
| 72 | + if (renderPipeline is HDRenderPipelineAsset) |
| 73 | + upgraders.Add(new StandardsToHDLitMaterialUpgrader("Standard", "HDRP/Lit")); |
49 | 74 | #endif |
50 | 75 | return upgraders; |
51 | 76 | } |
52 | 77 | #endif |
53 | | - |
54 | | - static string FindFolderByPartialPath(string partialPath) |
55 | | - { |
56 | | - string[] allFolders = AssetDatabase.GetAllAssetPaths(); |
57 | | - foreach (string folder in allFolders) |
58 | | - { |
59 | | - if (folder.EndsWith(partialPath)) |
60 | | - return folder; |
61 | | - } |
62 | | - return null; |
63 | | - } |
64 | 78 | } |
65 | 79 |
|
66 | 80 | #if CONVERT_HDRP |
|
0 commit comments