Renderer2DUpgrader.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using UnityEditor.SceneManagement;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.Universal;
  4. using UnityEngine.Experimental.Rendering.Universal;
  5. namespace UnityEditor.Experimental.Rendering.Universal
  6. {
  7. static class Renderer2DUpgrader
  8. {
  9. delegate void Upgrader<T>(T toUpgrade) where T : Object;
  10. static void ProcessAssetDatabaseObjects<T>(string searchString, Upgrader<T> upgrader) where T : Object
  11. {
  12. string[] prefabNames = AssetDatabase.FindAssets(searchString);
  13. foreach (string prefabName in prefabNames)
  14. {
  15. string path = AssetDatabase.GUIDToAssetPath(prefabName);
  16. if (path.StartsWith("Assets"))
  17. {
  18. T obj = AssetDatabase.LoadAssetAtPath<T>(path);
  19. if (obj != null)
  20. {
  21. upgrader(obj);
  22. }
  23. }
  24. }
  25. }
  26. static void UpgradeGameObject(GameObject go)
  27. {
  28. Renderer[] spriteRenderers = go.GetComponentsInChildren<Renderer>(true);
  29. Renderer2DData data = Light2DEditorUtility.GetRenderer2DData();
  30. if (data != null)
  31. {
  32. Material defaultMat = data.GetDefaultMaterial(DefaultMaterialType.Sprite);
  33. bool upgraded = false;
  34. foreach (Renderer renderer in spriteRenderers)
  35. {
  36. int materialCount = renderer.sharedMaterials.Length;
  37. Material[] newMaterials = new Material[materialCount];
  38. for (int i = 0; i < materialCount; i++)
  39. {
  40. Material mat = renderer.sharedMaterials[i];
  41. if (mat != null && mat.shader.name == "Sprites/Default")
  42. {
  43. newMaterials[i] = defaultMat;
  44. upgraded = true;
  45. }
  46. else
  47. {
  48. newMaterials[i] = renderer.sharedMaterials[i];
  49. }
  50. }
  51. if (upgraded)
  52. renderer.sharedMaterials = newMaterials;
  53. }
  54. if (upgraded)
  55. {
  56. Debug.Log(go.name + " was upgraded.", go);
  57. EditorSceneManager.MarkSceneDirty(go.scene);
  58. }
  59. }
  60. }
  61. static void UpgradeMaterial(Material mat)
  62. {
  63. Renderer2DData data = Light2DEditorUtility.GetRenderer2DData();
  64. if (data != null)
  65. {
  66. Material defaultMat = data.GetDefaultMaterial(DefaultMaterialType.Sprite);
  67. if (mat.shader.name == "Sprites/Default")
  68. {
  69. mat.shader = defaultMat.shader;
  70. }
  71. }
  72. }
  73. [MenuItem("Edit/Render Pipeline/Universal Render Pipeline/2D Renderer/Upgrade Scene to 2D Renderer (Experimental)", false)]
  74. static void UpgradeSceneTo2DRenderer()
  75. {
  76. if (!EditorUtility.DisplayDialog("2D Renderer Upgrader", "The upgrade will change the material references of Sprite Renderers in currently open scene(s) to a lit material. You can't undo this operation. Make sure you save the scene(s) before proceeding.", "Proceed", "Cancel"))
  77. return;
  78. GameObject[] gameObjects = Object.FindObjectsOfType<GameObject>();
  79. if (gameObjects != null && gameObjects.Length > 0)
  80. {
  81. foreach (GameObject go in gameObjects)
  82. {
  83. UpgradeGameObject(go);
  84. }
  85. }
  86. }
  87. [MenuItem("Edit/Render Pipeline/Universal Render Pipeline/2D Renderer/Upgrade Scene to 2D Renderer (Experimental)", true)]
  88. static bool UpgradeSceneTo2DRendererValidation()
  89. {
  90. return Light2DEditorUtility.IsUsing2DRenderer();
  91. }
  92. [MenuItem("Edit/Render Pipeline/Universal Render Pipeline/2D Renderer/Upgrade Project to 2D Renderer (Experimental)", false)]
  93. static void UpgradeProjectTo2DRenderer()
  94. {
  95. if (!EditorUtility.DisplayDialog("2D Renderer Upgrader", "The upgrade will search for all prefabs in your project that use Sprite Renderers and change the material references of those Sprite Renderers to a lit material. You can't undo this operation. It's highly recommended to backup your project before proceeding.", "Proceed", "Cancel"))
  96. return;
  97. ProcessAssetDatabaseObjects<GameObject>("t: Prefab", UpgradeGameObject);
  98. AssetDatabase.SaveAssets();
  99. Resources.UnloadUnusedAssets();
  100. }
  101. [MenuItem("Edit/Render Pipeline/Universal Render Pipeline/2D Renderer/Upgrade Project to 2D Renderer (Experimental)", true)]
  102. static bool UpgradeProjectTo2DRendererValidation()
  103. {
  104. return Light2DEditorUtility.IsUsing2DRenderer();
  105. }
  106. }
  107. }