UpgradePluginToSRP.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. public class EditPrefabAssetScope : IDisposable
  6. {
  7. public readonly string assetPath;
  8. public readonly GameObject prefabRoot;
  9. public EditPrefabAssetScope(string assetPath)
  10. {
  11. this.assetPath = assetPath;
  12. prefabRoot = PrefabUtility.LoadPrefabContents(assetPath);
  13. }
  14. public void Dispose()
  15. {
  16. PrefabUtility.SaveAsPrefabAsset(prefabRoot, assetPath);
  17. PrefabUtility.UnloadPrefabContents(prefabRoot);
  18. }
  19. }
  20. public class UpgradePluginToSRP
  21. {
  22. [MenuItem("ZED/Upgrade ZED Plugin to URP")]
  23. static void UpgradeToURP()
  24. {
  25. var SRP = GraphicsSettings.renderPipelineAsset;
  26. string zedRigMonoPath = "Assets/ZED/Prefabs/ZED_Rig_Mono.prefab";
  27. string zedRigStereoPath = "Assets/ZED/Prefabs/ZED_Rig_Stereo.prefab";
  28. string zedGreenScreenPath = "Assets/ZED/Examples/GreenScreen/Prefabs/ZED_GreenScreen.prefab";
  29. string zedPlanetariumPath = "Assets/ZED/Examples/Planetarium/Prefabs/Planetarium.prefab";
  30. if (GraphicsSettings.renderPipelineAsset != null)
  31. {
  32. if (SRP.GetType().ToString().Contains("UniversalRenderPipelineAsset"))
  33. {
  34. RenderingPipelineDefines.AddDefine("ZED_URP");
  35. RenderingPipelineDefines.RemoveDefine("ZED_HDRP");
  36. Material Mat_Zed_Forward_Lighting = Resources.Load("Materials/Lighting/Mat_ZED_Forward_Lighting_URP") as Material;
  37. Material Mat_Zed_GreenScreen = Resources.Load("Materials/Mat_ZED_GreenScreen_URP") as Material;
  38. Material Mat_Zed_Sun = Resources.Load("Sun/Materials/Sun_URP") as Material;
  39. // Modify prefab contents and save it back to the Prefab Asset
  40. using (var editScope = new EditPrefabAssetScope(zedRigMonoPath))
  41. {
  42. Transform frame = editScope.prefabRoot.transform.Find("Camera_Left").Find("Frame");
  43. if (frame)
  44. {
  45. frame.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  46. }
  47. else
  48. {
  49. Debug.Log("Frame is not found");
  50. }
  51. }
  52. using (var editScope = new EditPrefabAssetScope(zedRigStereoPath))
  53. {
  54. Transform frame_left = editScope.prefabRoot.transform.Find("Camera_eyes").Find("Left_eye").Find("Frame");
  55. if (frame_left)
  56. {
  57. frame_left.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  58. }
  59. else
  60. {
  61. Debug.Log("Frame Left is not found");
  62. }
  63. Transform frame_right = editScope.prefabRoot.transform.Find("Camera_eyes").Find("Right_eye").Find("Frame");
  64. if (frame_right)
  65. {
  66. frame_right.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  67. }
  68. else
  69. {
  70. Debug.Log("Frame Right is not found");
  71. }
  72. }
  73. // Modify prefab contents and save it back to the Prefab Asset
  74. using (var editScope = new EditPrefabAssetScope(zedGreenScreenPath))
  75. {
  76. Transform frame = editScope.prefabRoot.transform.Find("Camera_Left").Find("Frame");
  77. if (frame)
  78. {
  79. frame.GetComponent<MeshRenderer>().material = Mat_Zed_GreenScreen;
  80. }
  81. else
  82. {
  83. Debug.Log("Frame is not found");
  84. }
  85. }
  86. using (var editScope = new EditPrefabAssetScope(zedPlanetariumPath))
  87. {
  88. Transform sun = editScope.prefabRoot.transform.Find("Sun").Find("sun");
  89. if (sun)
  90. {
  91. sun.GetComponent<MeshRenderer>().material = Mat_Zed_Sun;
  92. }
  93. else
  94. {
  95. Debug.Log("Planetarium is not found");
  96. }
  97. }
  98. }
  99. else
  100. {
  101. Debug.LogWarning("Trying to convert to URP without using URP !!");
  102. }
  103. }
  104. }
  105. [MenuItem("ZED/Upgrade ZED Plugin to HDRP")]
  106. static void UpgradeToHDRP()
  107. {
  108. var SRP = GraphicsSettings.renderPipelineAsset;
  109. string zedRigMonoPath = "Assets/ZED/Prefabs/ZED_Rig_Mono.prefab";
  110. string zedRigStereoPath = "Assets/ZED/Prefabs/ZED_Rig_Stereo.prefab";
  111. string zedGreenScreenPath = "Assets/ZED/Examples/GreenScreen/Prefabs/ZED_GreenScreen.prefab";
  112. string zedPlanetariumPath = "Assets/ZED/Examples/Planetarium/Prefabs/Planetarium.prefab";
  113. if (SRP.GetType().ToString().Contains("HDRenderPipelineAsset"))
  114. {
  115. RenderingPipelineDefines.AddDefine("ZED_HDRP");
  116. RenderingPipelineDefines.RemoveDefine("ZED_URP");
  117. Material Mat_Zed_Forward_Lighting = Resources.Load("Materials/Lighting/Mat_ZED_HDRP_Lit") as Material;
  118. Material Mat_Zed_GreenScreen = Resources.Load("Materials/Mat_ZED_Greenscreen_HDRP_Lit") as Material;
  119. Material Mat_Zed_Sun = Resources.Load("Sun/Materials/Sun_URP") as Material;
  120. // Modify prefab contents and save it back to the Prefab Asset
  121. using (var editScope = new EditPrefabAssetScope(zedRigMonoPath))
  122. {
  123. Transform frame = editScope.prefabRoot.transform.Find("Camera_Left").Find("Frame");
  124. if (frame)
  125. {
  126. frame.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  127. }
  128. else
  129. {
  130. Debug.Log("Frame is not found");
  131. }
  132. }
  133. using (var editScope = new EditPrefabAssetScope(zedRigStereoPath))
  134. {
  135. Transform frame_left = editScope.prefabRoot.transform.Find("Camera_eyes").Find("Left_eye").Find("Frame");
  136. if (frame_left)
  137. {
  138. frame_left.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  139. }
  140. else
  141. {
  142. Debug.Log("Frame Left is not found");
  143. }
  144. Transform frame_right = editScope.prefabRoot.transform.Find("Camera_eyes").Find("Right_eye").Find("Frame");
  145. if (frame_right)
  146. {
  147. frame_right.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  148. }
  149. else
  150. {
  151. Debug.Log("Frame Right is not found");
  152. }
  153. }
  154. // Modify prefab contents and save it back to the Prefab Asset
  155. using (var editScope = new EditPrefabAssetScope(zedGreenScreenPath))
  156. {
  157. Transform frame = editScope.prefabRoot.transform.Find("Camera_Left").Find("Frame");
  158. if (frame)
  159. {
  160. frame.GetComponent<MeshRenderer>().material = Mat_Zed_GreenScreen;
  161. }
  162. else
  163. {
  164. Debug.Log("Frame is not found");
  165. }
  166. }
  167. using (var editScope = new EditPrefabAssetScope(zedPlanetariumPath))
  168. {
  169. Transform sun = editScope.prefabRoot.transform.Find("Sun").Find("sun");
  170. if (sun)
  171. {
  172. sun.GetComponent<MeshRenderer>().material = Mat_Zed_Sun;
  173. }
  174. else
  175. {
  176. Debug.Log("Planetarium is not found");
  177. }
  178. }
  179. }
  180. else
  181. {
  182. Debug.LogWarning("Trying to convert to HDRP without using HDRP !!");
  183. }
  184. }
  185. /*[MenuItem("ZED/Revert ZED Plugin to built-in RP")]
  186. static void RevertToBuiltInRP()
  187. {
  188. RenderingPipelineDefines.RemoveDefine("ZED_HDRP");
  189. RenderingPipelineDefines.RemoveDefine("ZED_URP");
  190. string zedRigMonoPath = "Assets/ZED/Prefabs/ZED_Rig_Mono.prefab";
  191. string zedRigStereoPath = "Assets/ZED/Prefabs/ZED_Rig_Stereo.prefab";
  192. string zedGreenScreenPath = "Assets/ZED/Examples/GreenScreen/Prefabs/ZED_GreenScreen.prefab";
  193. string zedPlanetariumPath = "Assets/ZED/Examples/Planetarium/Prefabs/Planetarium.prefab";
  194. Material Mat_Zed_Forward_Lighting = Resources.Load("Materials/Lighting/Mat_ZED_Forward_Lighting") as Material;
  195. Material Mat_Zed_GreenScreen = Resources.Load("Materials/Mat_ZED_GreenScreen_URP") as Material;
  196. Material Mat_Zed_Sun = Resources.Load("Sun/Materials/Sun_URP") as Material;
  197. // Modify prefab contents and save it back to the Prefab Asset
  198. using (var editScope = new EditPrefabAssetScope(zedRigMonoPath))
  199. {
  200. Transform frame = editScope.prefabRoot.transform.Find("Camera_Left").Find("Frame");
  201. if (frame)
  202. {
  203. frame.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  204. }
  205. else
  206. {
  207. Debug.Log("Frame is not found");
  208. }
  209. }
  210. using (var editScope = new EditPrefabAssetScope(zedRigStereoPath))
  211. {
  212. Transform frame_left = editScope.prefabRoot.transform.Find("Camera_eyes").Find("Left_eye").Find("Frame");
  213. if (frame_left)
  214. {
  215. frame_left.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  216. }
  217. else
  218. {
  219. Debug.Log("Frame Left is not found");
  220. }
  221. Transform frame_right = editScope.prefabRoot.transform.Find("Camera_eyes").Find("Right_eye").Find("Frame");
  222. if (frame_right)
  223. {
  224. frame_right.GetComponent<MeshRenderer>().material = Mat_Zed_Forward_Lighting;
  225. }
  226. else
  227. {
  228. Debug.Log("Frame Right is not found");
  229. }
  230. }
  231. // Modify prefab contents and save it back to the Prefab Asset
  232. using (var editScope = new EditPrefabAssetScope(zedGreenScreenPath))
  233. {
  234. Transform frame = editScope.prefabRoot.transform.Find("Camera_Left").Find("Frame");
  235. if (frame)
  236. {
  237. frame.GetComponent<MeshRenderer>().material = Mat_Zed_GreenScreen;
  238. }
  239. else
  240. {
  241. Debug.Log("Frame is not found");
  242. }
  243. }
  244. using (var editScope = new EditPrefabAssetScope(zedPlanetariumPath))
  245. {
  246. Transform sun = editScope.prefabRoot.transform.Find("Sun").Find("sun");
  247. if (sun)
  248. {
  249. sun.GetComponent<MeshRenderer>().material = Mat_Zed_Sun;
  250. }
  251. else
  252. {
  253. Debug.Log("Planetarium is not found");
  254. }
  255. }
  256. }*/
  257. }