File size: 12,098 Bytes
d883ffe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | using System;
using UnityEngine;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
[CustomEditor(typeof(PixelPerfectCamera))]
class PixelPerfectCameraEditor : Editor
{
private class Style
{
public GUIContent x = new GUIContent("X");
public GUIContent y = new GUIContent("Y");
public GUIContent assetsPPU = new GUIContent("Assets Pixels Per Unit", "The amount of pixels that make up one unit of the Scene. Set this value to match the PPU value of Sprites in the Scene.");
public GUIContent refRes = new GUIContent("Reference Resolution", "The original resolution the Assets are designed for.");
public GUIContent gridSnapping = new GUIContent("Grid Snapping", "Sets the snapping behavior for the camera and sprites.");
public GUIContent cropFrame = new GUIContent("Crop Frame", "Crops the viewport to match the Reference Resolution, along the checked axis. Black bars will be added to fit the screen aspect ratio.");
public GUIContent filterMode = new GUIContent("Filter Mode", "Use selected Filter Mode when using Stretch Fill to upscale from Reference Resolution.");
public GUIContent stretchFill = new GUIContent("Stretch Fill", "If enabled, expands the viewport to fit the screen resolution while maintaining the viewport aspect ratio.");
public GUIContent currentPixelRatio = new GUIContent("Current Pixel Ratio", "Ratio of the rendered Sprites compared to their original size.");
public GUIContent runInEditMode = new GUIContent("Run In Edit Mode", "Enable this to preview Camera setting changes in Edit Mode. This will cause constant changes to the Scene while active.");
public const string cameraStackingWarning = "Pixel Perfect Camera won't function properly if stacked with another camera.";
public const string nonRenderer2DWarning = "URP Pixel Perfect Camera requires a camera using a 2D Renderer. Some features, such as Upscale Render Texture, are not supported with other Renderers.";
public const string nonRenderer2DError = "URP Pixel Perfect Camera requires a camera using a 2D Renderer.";
public GUIStyle centeredLabel;
public Style()
{
centeredLabel = new GUIStyle(EditorStyles.label);
centeredLabel.alignment = TextAnchor.MiddleCenter;
}
}
private static Style m_Style;
private const float k_SingleLetterLabelWidth = 15.0f;
private const float k_DottedLineSpacing = 2.5f;
private SerializedProperty m_AssetsPPU;
private SerializedProperty m_RefResX;
private SerializedProperty m_RefResY;
private SerializedProperty m_CropFrame;
private SerializedProperty m_FilterMode;
private SerializedProperty m_GridSnapping;
private Vector2 m_GameViewSize = Vector2.zero;
private GUIContent m_CurrentPixelRatioValue;
bool m_CameraStacking;
private void LazyInit()
{
if (m_Style == null)
m_Style = new Style();
if (m_CurrentPixelRatioValue == null)
m_CurrentPixelRatioValue = new GUIContent();
}
UniversalAdditionalCameraData GetCameraData()
{
PixelPerfectCamera obj = target as PixelPerfectCamera;
UniversalAdditionalCameraData cameraData = null;
obj?.TryGetComponent(out cameraData);
return cameraData;
}
bool UsingSRP()
{
var cameraData = GetCameraData();
return cameraData?.scriptableRenderer != null;
}
bool UsingRenderer2D()
{
var cameraData = GetCameraData();
if (cameraData != null)
{
Renderer2D renderer2D = cameraData.scriptableRenderer as Renderer2D;
if (renderer2D != null)
return true;
}
return false;
}
void CheckForCameraStacking()
{
m_CameraStacking = false;
var cameraData = GetCameraData();
if (cameraData == null || cameraData.scriptableRenderer == null)
return;
if (cameraData.renderType == CameraRenderType.Base)
{
var cameraStack = cameraData.cameraStack;
m_CameraStacking = cameraStack != null ? cameraStack.Count > 0 : false;
}
else if (cameraData.renderType == CameraRenderType.Overlay)
m_CameraStacking = true;
}
public void OnEnable()
{
m_AssetsPPU = serializedObject.FindProperty("m_AssetsPPU");
m_RefResX = serializedObject.FindProperty("m_RefResolutionX");
m_RefResY = serializedObject.FindProperty("m_RefResolutionY");
m_CropFrame = serializedObject.FindProperty("m_CropFrame");
m_FilterMode = serializedObject.FindProperty("m_FilterMode");
m_GridSnapping = serializedObject.FindProperty("m_GridSnapping");
}
public override bool RequiresConstantRepaint()
{
PixelPerfectCamera obj = target as PixelPerfectCamera;
if (obj == null || !obj.enabled)
return false;
// If game view size changes, we need to force a repaint of the inspector as the pixel ratio value may change accordingly.
Vector2 gameViewSize = Handles.GetMainGameViewSize();
if (gameViewSize != m_GameViewSize)
{
m_GameViewSize = gameViewSize;
return true;
}
else
return false;
}
public override void OnInspectorGUI()
{
LazyInit();
if (!UsingSRP())
{
EditorGUILayout.HelpBox(Style.nonRenderer2DError, MessageType.Error);
return;
}
else if (!UsingRenderer2D())
{
EditorGUILayout.HelpBox(Style.nonRenderer2DWarning, MessageType.Warning);
EditorGUILayout.Space();
}
float originalLabelWidth = EditorGUIUtility.labelWidth;
serializedObject.Update();
if (Event.current.type == EventType.Layout)
CheckForCameraStacking();
if (m_CameraStacking)
EditorGUILayout.HelpBox(Style.cameraStackingWarning, MessageType.Warning);
EditorGUILayout.PropertyField(m_AssetsPPU, m_Style.assetsPPU);
if (m_AssetsPPU.intValue <= 0)
m_AssetsPPU.intValue = 1;
EditorGUILayout.BeginHorizontal();
{
EditorGUILayout.PrefixLabel(m_Style.refRes);
EditorGUIUtility.labelWidth = k_SingleLetterLabelWidth * (EditorGUI.indentLevel + 1);
EditorGUILayout.PropertyField(m_RefResX, m_Style.x);
if (m_RefResX.intValue <= 0)
m_RefResX.intValue = 1;
EditorGUILayout.PropertyField(m_RefResY, m_Style.y);
if (m_RefResY.intValue <= 0)
m_RefResY.intValue = 1;
EditorGUIUtility.labelWidth = originalLabelWidth;
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(m_CropFrame, m_Style.cropFrame);
EditorGUILayout.PropertyField(m_GridSnapping, m_Style.gridSnapping);
if (m_CropFrame.enumValueIndex == (int)PixelPerfectCamera.CropFrame.StretchFill)
{
EditorGUILayout.PropertyField(m_FilterMode, m_Style.filterMode);
}
serializedObject.ApplyModifiedProperties();
PixelPerfectCamera obj = target as PixelPerfectCamera;
if (obj != null)
{
if (obj.isActiveAndEnabled && (EditorApplication.isPlaying || obj.runInEditMode))
{
if (Event.current.type == EventType.Layout)
m_CurrentPixelRatioValue.text = string.Format("{0}:1", obj.pixelRatio);
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.LabelField(m_Style.currentPixelRatio, m_CurrentPixelRatioValue);
EditorGUI.EndDisabledGroup();
}
}
}
void OnSceneGUI()
{
PixelPerfectCamera obj = target as PixelPerfectCamera;
if (obj == null)
return;
Camera camera = obj.GetComponent<Camera>();
// Show a green rect in scene view that represents the visible area when the pixel perfect correction takes effect in play mode.
Vector2 gameViewSize = Handles.GetMainGameViewSize();
int gameViewWidth = (int)gameViewSize.x;
int gameViewHeight = (int)gameViewSize.y;
int zoom = Math.Max(1, Math.Min(gameViewHeight / obj.refResolutionY, gameViewWidth / obj.refResolutionX));
float verticalOrthoSize;
float horizontalOrthoSize;
if (obj.cropFrame == PixelPerfectCamera.CropFrame.StretchFill || obj.cropFrame == PixelPerfectCamera.CropFrame.Windowbox)
{
verticalOrthoSize = obj.refResolutionY * 0.5f / obj.assetsPPU;
horizontalOrthoSize = verticalOrthoSize * ((float)obj.refResolutionX / obj.refResolutionY);
}
else if (obj.cropFrame == PixelPerfectCamera.CropFrame.Letterbox)
{
verticalOrthoSize = obj.refResolutionY * 0.5f / obj.assetsPPU;
horizontalOrthoSize = verticalOrthoSize * ((float)gameViewWidth / (zoom * obj.refResolutionY));
}
else if (obj.cropFrame == PixelPerfectCamera.CropFrame.Pillarbox)
{
horizontalOrthoSize = obj.refResolutionX * 0.5f / obj.assetsPPU;
verticalOrthoSize = horizontalOrthoSize / (zoom * obj.refResolutionX / (float)gameViewHeight);
}
else
{
verticalOrthoSize = gameViewHeight * 0.5f / (zoom * obj.assetsPPU);
horizontalOrthoSize = verticalOrthoSize * camera.aspect;
}
Handles.color = Color.green;
Vector3 cameraPosition = camera.transform.position;
Vector3 p1 = cameraPosition + new Vector3(-horizontalOrthoSize, verticalOrthoSize, 0.0f);
Vector3 p2 = cameraPosition + new Vector3(horizontalOrthoSize, verticalOrthoSize, 0.0f);
Handles.DrawLine(p1, p2);
p1 = cameraPosition + new Vector3(horizontalOrthoSize, -verticalOrthoSize, 0.0f);
Handles.DrawLine(p2, p1);
p2 = cameraPosition + new Vector3(-horizontalOrthoSize, -verticalOrthoSize, 0.0f);
Handles.DrawLine(p1, p2);
p1 = cameraPosition + new Vector3(-horizontalOrthoSize, verticalOrthoSize, 0.0f);
Handles.DrawLine(p2, p1);
// Show a green dotted rect in scene view that represents the area defined by the reference resolution.
horizontalOrthoSize = obj.refResolutionX * 0.5f / obj.assetsPPU;
verticalOrthoSize = obj.refResolutionY * 0.5f / obj.assetsPPU;
p1 = cameraPosition + new Vector3(-horizontalOrthoSize, verticalOrthoSize, 0.0f);
p2 = cameraPosition + new Vector3(horizontalOrthoSize, verticalOrthoSize, 0.0f);
Handles.DrawDottedLine(p1, p2, k_DottedLineSpacing);
p1 = cameraPosition + new Vector3(horizontalOrthoSize, -verticalOrthoSize, 0.0f);
Handles.DrawDottedLine(p2, p1, k_DottedLineSpacing);
p2 = cameraPosition + new Vector3(-horizontalOrthoSize, -verticalOrthoSize, 0.0f);
Handles.DrawDottedLine(p1, p2, k_DottedLineSpacing);
p1 = cameraPosition + new Vector3(-horizontalOrthoSize, verticalOrthoSize, 0.0f);
Handles.DrawDottedLine(p2, p1, k_DottedLineSpacing);
}
}
}
|