|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEditor.Rendering.Converter; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.Categorization; |
| 6 | + |
| 7 | +using URPPackage = UnityEngine.Rendering.Universal; |
| 8 | + |
| 9 | +#if PIXEL_PERFECT_2D_EXISTS |
| 10 | +using U2DPackage = UnityEngine.U2D; |
| 11 | +#endif |
| 12 | + |
| 13 | +namespace UnityEditor.Rendering.Universal |
| 14 | +{ |
| 15 | + [Serializable] |
| 16 | + internal class PixelPerfectCameraConverterItem : RenderPipelineConverterAssetItem |
| 17 | + { |
| 18 | + public PixelPerfectCameraConverterItem(string id) : base(id) |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + public PixelPerfectCameraConverterItem(GlobalObjectId gid, string assetPath) : base(gid, assetPath) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + public new Texture2D icon => EditorGUIUtility.ObjectContent(null, typeof(UnityEngine.Camera)).image as Texture2D; |
| 27 | + } |
| 28 | + |
| 29 | + [Serializable] |
| 30 | + [PipelineConverter("Built-in", "Universal Render Pipeline (2D Renderer)")] |
| 31 | + [ElementInfo(Name = "Pixel Perfect Camera Converter", |
| 32 | + Order = 1000, |
| 33 | + Description = "This will upgrade all 2D Pixel Perfect Camera (com.unity.2d.pixelperfect) to the Universal Render Pipeline version.")] |
| 34 | + internal class BuiltInPixelPerfectCameraConverter : IRenderPipelineConverter |
| 35 | + { |
| 36 | + |
| 37 | +#if PIXEL_PERFECT_2D_EXISTS |
| 38 | + public bool isEnabled => true; |
| 39 | + public string isDisabledMessage { get; } |
| 40 | + |
| 41 | + public static bool UpgradePixelPerfectCamera(U2DPackage.PixelPerfectCamera cam) |
| 42 | + { |
| 43 | + if (cam == null) |
| 44 | + return false; |
| 45 | + |
| 46 | + // Copy over serialized data |
| 47 | + var urpCam = cam.gameObject.AddComponent<URPPackage.PixelPerfectCamera>(); |
| 48 | + |
| 49 | + if (urpCam == null) |
| 50 | + return false; |
| 51 | + |
| 52 | + urpCam.assetsPPU = cam.assetsPPU; |
| 53 | + urpCam.refResolutionX = cam.refResolutionX; |
| 54 | + urpCam.refResolutionY = cam.refResolutionY; |
| 55 | + |
| 56 | + if (cam.upscaleRT) |
| 57 | + urpCam.gridSnapping = URPPackage.PixelPerfectCamera.GridSnapping.UpscaleRenderTexture; |
| 58 | + else if (cam.pixelSnapping) |
| 59 | + urpCam.gridSnapping = URPPackage.PixelPerfectCamera.GridSnapping.PixelSnapping; |
| 60 | + |
| 61 | + if (cam.cropFrameX && cam.cropFrameY) |
| 62 | + { |
| 63 | + if (cam.stretchFill) |
| 64 | + urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.StretchFill; |
| 65 | + else |
| 66 | + urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.Windowbox; |
| 67 | + } |
| 68 | + else if (cam.cropFrameX) |
| 69 | + { |
| 70 | + urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.Pillarbox; |
| 71 | + } |
| 72 | + else if (cam.cropFrameY) |
| 73 | + { |
| 74 | + urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.Letterbox; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + urpCam.cropFrame = URPPackage.PixelPerfectCamera.CropFrame.None; |
| 79 | + } |
| 80 | + |
| 81 | + UnityEngine.Object.DestroyImmediate(cam, true); |
| 82 | + |
| 83 | + EditorUtility.SetDirty(urpCam); |
| 84 | + |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + void UpgradeGameObject(GameObject go) |
| 89 | + { |
| 90 | + var cam = go.GetComponentInChildren<U2DPackage.PixelPerfectCamera>(); |
| 91 | + |
| 92 | + if (cam != null) |
| 93 | + UpgradePixelPerfectCamera(cam); |
| 94 | + } |
| 95 | + |
| 96 | +#else |
| 97 | + public bool isEnabled => false; |
| 98 | + public string isDisabledMessage => "Pixel Perfect package is not installed. Please install the Pixel Perfect package to enable this converter."; |
| 99 | +#endif |
| 100 | + |
| 101 | + public void Scan(Action<List<IRenderPipelineConverterItem>> onScanFinish) |
| 102 | + { |
| 103 | +#if PIXEL_PERFECT_2D_EXISTS |
| 104 | + var returnList = new List<IRenderPipelineConverterItem>(); |
| 105 | + void OnSearchFinish() |
| 106 | + { |
| 107 | + onScanFinish?.Invoke(returnList); |
| 108 | + } |
| 109 | + |
| 110 | + var processedIds = new HashSet<string>(); |
| 111 | + |
| 112 | + SearchServiceUtils.RunQueuedSearch |
| 113 | + ( |
| 114 | + SearchServiceUtils.IndexingOptions.DeepSearch, |
| 115 | + new List<(string query, string description)> { ("t:UnityEngine.U2D.PixelPerfectCamera", "Game Objects Referencing a U2D Pixel Perfect Camera") }, |
| 116 | + (item, description) => |
| 117 | + { |
| 118 | + // Direct conversion - works for both assets and scene objects |
| 119 | + var unityObject = item.ToObject(); |
| 120 | + |
| 121 | + if (unityObject == null) |
| 122 | + return; |
| 123 | + |
| 124 | + // Ensure we're always working with GameObjects |
| 125 | + GameObject go = null; |
| 126 | + |
| 127 | + if (unityObject is GameObject gameObject) |
| 128 | + go = gameObject; |
| 129 | + else if (unityObject is Component component) |
| 130 | + go = component.gameObject; |
| 131 | + else |
| 132 | + return; // Not a GameObject or Component |
| 133 | + |
| 134 | + var gid = GlobalObjectId.GetGlobalObjectIdSlow(go); |
| 135 | + if (!processedIds.Add(gid.ToString())) |
| 136 | + return; |
| 137 | + |
| 138 | + int type = gid.identifierType; // 1=Asset, 2=SceneObject |
| 139 | + |
| 140 | + var go = unityObject as GameObject; |
| 141 | + var ppCameraItem = new PixelPerfectCameraConverterItem(gid.ToString()) |
| 142 | + { |
| 143 | + name = $"{unityObject.name} ({(type == 1 ? "Prefab" : "SceneObject")})", |
| 144 | + info = type == 1 ? AssetDatabase.GetAssetPath(unityObject) : go.scene.path, |
| 145 | + }; |
| 146 | + |
| 147 | + returnList.Add(ppCameraItem); |
| 148 | + }, |
| 149 | + OnSearchFinish |
| 150 | + ); |
| 151 | +#else |
| 152 | + throw new InvalidOperationException(); |
| 153 | +#endif |
| 154 | + } |
| 155 | + |
| 156 | + public Status Convert(IRenderPipelineConverterItem item, out string message) |
| 157 | + { |
| 158 | + message = string.Empty; |
| 159 | + |
| 160 | +#if PIXEL_PERFECT_2D_EXISTS |
| 161 | + if (item is PixelPerfectCameraConverterItem ppCameraItem) |
| 162 | + { |
| 163 | + if (ppCameraItem.type == 1) URP2DConverterUtility.UpgradePrefab(ppCameraItem.info, UpgradeGameObject); |
| 164 | + else URP2DConverterUtility.UpgradeScene(ppCameraItem.info, UpgradeGameObject); |
| 165 | + |
| 166 | + return Status.Success; |
| 167 | + } |
| 168 | +#endif |
| 169 | + return Status.Error; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments