URPMaterialSwitcher.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. #if UNITY_EDITOR && UNITY_URP
  5. using UnityEditor;
  6. #endif
  7. [ExecuteInEditMode]
  8. public class URPMaterialSwitcher : MonoBehaviour
  9. {
  10. public bool children = false;
  11. #if UNITY_EDITOR && UNITY_URP
  12. private const string searchTemplate = "URP{0} t:material";
  13. void Start()
  14. {
  15. Renderer renderer;
  16. if (children)
  17. renderer = this.GetComponentInChildren<Renderer>();
  18. else
  19. renderer = this.GetComponent<Renderer>();
  20. if (renderer.sharedMaterial.name.StartsWith("URP") == false)
  21. {
  22. string[] mats = UnityEditor.AssetDatabase.FindAssets(string.Format(searchTemplate, renderer.sharedMaterial.name));
  23. if (mats.Length > 0)
  24. {
  25. string path = UnityEditor.AssetDatabase.GUIDToAssetPath(mats[0]);
  26. if (PrefabUtility.IsPartOfPrefabInstance(this))
  27. {
  28. string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(this);
  29. GameObject myPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  30. URPMaterialSwitcher[] switchers = myPrefab.GetComponentsInChildren<URPMaterialSwitcher>(true);
  31. foreach (var switcher in switchers)
  32. {
  33. switcher.Execute();
  34. }
  35. EditorUtility.SetDirty(myPrefab);
  36. }
  37. else
  38. {
  39. this.Execute();
  40. }
  41. }
  42. }
  43. }
  44. public void Execute()
  45. {
  46. if (children)
  47. {
  48. Renderer[] renderers = this.GetComponentsInChildren<Renderer>();
  49. foreach (var renderer in renderers)
  50. SwitchRenderer(renderer);
  51. }
  52. else
  53. {
  54. SwitchRenderer(this.GetComponent<Renderer>());
  55. }
  56. }
  57. private void SwitchRenderer(Renderer renderer)
  58. {
  59. if (renderer != null && renderer.sharedMaterial.name.StartsWith("URP") == false)
  60. {
  61. string[] foundMaterials = UnityEditor.AssetDatabase.FindAssets(string.Format(searchTemplate, renderer.sharedMaterial.name));
  62. if (foundMaterials.Length > 0)
  63. {
  64. string urpMaterialPath = UnityEditor.AssetDatabase.GUIDToAssetPath(foundMaterials[0]);
  65. Material urpMaterial = UnityEditor.AssetDatabase.LoadAssetAtPath<Material>(urpMaterialPath);
  66. SerializedObject serializedRenderer = new SerializedObject(renderer);
  67. serializedRenderer.Update();
  68. SerializedProperty materialProp = serializedRenderer.FindProperty("m_Materials");
  69. materialProp.ClearArray();
  70. materialProp.InsertArrayElementAtIndex(0);
  71. materialProp.GetArrayElementAtIndex(0).objectReferenceValue = urpMaterial;
  72. serializedRenderer.ApplyModifiedProperties();
  73. if (PrefabUtility.IsPartOfPrefabInstance(renderer))
  74. {
  75. PrefabUtility.RecordPrefabInstancePropertyModifications(renderer);
  76. UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(this.gameObject.scene);
  77. }
  78. }
  79. }
  80. }
  81. #endif
  82. }