No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

U2DToURPPixelPerfectConverter.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #if ENABLE_URP_14_0_0_OR_NEWER
  2. using System;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using U2DPackage = UnityEngine.U2D;
  7. using URPPackage = UnityEngine.Experimental.Rendering.Universal;
  8. namespace UnityEditor.Rendering.Universal
  9. {
  10. internal sealed class U2DToURPPixelPerfectConverter : RenderPipelineConverter
  11. {
  12. public override string name => "2D to URP Pixel Perfect Camera Converter";
  13. public override string info => "This will upgrade all 2D Pixel Perfect Camera to the URP version.";
  14. public override int priority => -2000;
  15. public override Type container => typeof(UpgradeURP2DAssetsContainer);
  16. List<string> m_AssetsToConvert = new List<string>();
  17. public static bool UpgradePixelPerfectCamera(U2DPackage.PixelPerfectCamera cam)
  18. {
  19. if (cam == null)
  20. return false;
  21. // Copy over serialized data
  22. var urpCam = cam.gameObject.AddComponent<URPPackage.PixelPerfectCamera>();
  23. if (urpCam == null)
  24. return false;
  25. urpCam.assetsPPU = cam.assetsPPU;
  26. urpCam.refResolutionX = cam.refResolutionX;
  27. urpCam.refResolutionY = cam.refResolutionY;
  28. if (cam.upscaleRT)
  29. urpCam.gridSnapping = URPPackage.PixelPerfectCamera.GridSnapping.UpscaleRenderTexture;
  30. else if(cam.pixelSnapping)
  31. urpCam.gridSnapping = URPPackage.PixelPerfectCamera.GridSnapping.PixelSnapping;
  32. if (cam.cropFrameX && cam.cropFrameY)
  33. {
  34. if (cam.stretchFill)
  35. urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.StretchFill;
  36. else
  37. urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.Windowbox;
  38. }
  39. else if (cam.cropFrameX)
  40. {
  41. urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.Pillarbox;
  42. }
  43. else if (cam.cropFrameY)
  44. {
  45. urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.Letterbox;
  46. }
  47. else
  48. {
  49. urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.None;
  50. }
  51. UnityEngine.Object.DestroyImmediate(cam, true);
  52. EditorUtility.SetDirty(urpCam);
  53. return true;
  54. }
  55. void UpgradeGameObject(GameObject go)
  56. {
  57. var cam = go.GetComponentInChildren<U2DPackage.PixelPerfectCamera>();
  58. if(cam != null)
  59. UpgradePixelPerfectCamera(cam);
  60. }
  61. public override void OnInitialize(InitializeConverterContext context, Action callback)
  62. {
  63. string[] allAssetPaths = AssetDatabase.GetAllAssetPaths();
  64. foreach (string path in allAssetPaths)
  65. {
  66. if (URP2DConverterUtility.IsPrefabOrScenePath(path, "m_AssetsPPU:"))
  67. {
  68. ConverterItemDescriptor desc = new ConverterItemDescriptor()
  69. {
  70. name = Path.GetFileNameWithoutExtension(path),
  71. info = path,
  72. warningMessage = String.Empty,
  73. helpLink = String.Empty
  74. };
  75. // Each converter needs to add this info using this API.
  76. m_AssetsToConvert.Add(path);
  77. context.AddAssetToConvert(desc);
  78. }
  79. }
  80. callback.Invoke();
  81. }
  82. public override void OnRun(ref RunItemContext context)
  83. {
  84. string result = string.Empty;
  85. string ext = Path.GetExtension(context.item.descriptor.info);
  86. if (ext == ".prefab")
  87. result = URP2DConverterUtility.UpgradePrefab(context.item.descriptor.info, UpgradeGameObject);
  88. else if (ext == ".unity")
  89. URP2DConverterUtility.UpgradeScene(context.item.descriptor.info, UpgradeGameObject);
  90. if (result != string.Empty)
  91. {
  92. context.didFail = true;
  93. context.info = result;
  94. }
  95. else
  96. {
  97. context.hasConverted = true;
  98. }
  99. }
  100. public override void OnClicked(int index)
  101. {
  102. EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(m_AssetsToConvert[index]));
  103. }
  104. public override void OnPostRun()
  105. {
  106. Resources.UnloadUnusedAssets();
  107. }
  108. }
  109. }
  110. #endif