using System;
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace UnityEngine.UI
{
[RequireComponent(typeof(Canvas))]
[ExecuteAlways]
[AddComponentMenu("Layout/Canvas Scaler", 101)]
[DisallowMultipleComponent]
///
/// The Canvas Scaler component is used for controlling the overall scale and pixel density of UI elements in the Canvas. This scaling affects everything under the Canvas, including font sizes and image borders.
///
///
/// For a Canvas set to 'Screen Space - Overlay' or 'Screen Space - Camera', the Canvas Scaler UI Scale Mode can be set to Constant Pixel Size, Scale With Screen Size, or Constant Physical Size.
///
/// Using the Constant Pixel Size mode, positions and sizes of UI elements are specified in pixels on the screen. This is also the default functionality of the Canvas when no Canvas Scaler is attached. However, With the Scale Factor setting in the Canvas Scaler, a constant scaling can be applied to all UI elements in the Canvas.
///
/// Using the Scale With Screen Size mode, positions and sizes can be specified according to the pixels of a specified reference resolution. If the current screen resolution is larger than the reference resolution, the Canvas will keep having only the resolution of the reference resolution, but will scale up in order to fit the screen. If the current screen resolution is smaller than the reference resolution, the Canvas will similarly be scaled down to fit. If the current screen resolution has a different aspect ratio than the reference resolution, scaling each axis individually to fit the screen would result in non-uniform scaling, which is generally undesirable. Instead of this, the ReferenceResolution component will make the Canvas resolution deviate from the reference resolution in order to respect the aspect ratio of the screen. It is possible to control how this deviation should behave using the ::ref::screenMatchMode setting.
///
/// Using the Constant Physical Size mode, positions and sizes of UI elements are specified in physical units, such as millimeters, points, or picas. This mode relies on the device reporting its screen DPI correctly. You can specify a fallback DPI to use for devices that do not report a DPI.
///
/// For a Canvas set to 'World Space' the Canvas Scaler can be used to control the pixel density of UI elements in the Canvas.
///
public class CanvasScaler : UIBehaviour
{
///
/// Determines how UI elements in the Canvas are scaled.
///
public enum ScaleMode
{
///
/// Using the Constant Pixel Size mode, positions and sizes of UI elements are specified in pixels on the screen.
///
ConstantPixelSize,
///
/// Using the Scale With Screen Size mode, positions and sizes can be specified according to the pixels of a specified reference resolution.
/// If the current screen resolution is larger than the reference resolution, the Canvas will keep having only the resolution of the reference resolution, but will scale up in order to fit the screen. If the current screen resolution is smaller than the reference resolution, the Canvas will similarly be scaled down to fit.
///
ScaleWithScreenSize,
///
/// Using the Constant Physical Size mode, positions and sizes of UI elements are specified in physical units, such as millimeters, points, or picas.
///
ConstantPhysicalSize
}
[Tooltip("Determines how UI elements in the Canvas are scaled.")]
[SerializeField] private ScaleMode m_UiScaleMode = ScaleMode.ConstantPixelSize;
///
///Determines how UI elements in the Canvas are scaled.
///
public ScaleMode uiScaleMode { get { return m_UiScaleMode; } set { m_UiScaleMode = value; } }
[Tooltip("If a sprite has this 'Pixels Per Unit' setting, then one pixel in the sprite will cover one unit in the UI.")]
[SerializeField] protected float m_ReferencePixelsPerUnit = 100;
///
/// If a sprite has this 'Pixels Per Unit' setting, then one pixel in the sprite will cover one unit in the UI.
///
public float referencePixelsPerUnit { get { return m_ReferencePixelsPerUnit; } set { m_ReferencePixelsPerUnit = value; } }
// Constant Pixel Size settings
[Tooltip("Scales all UI elements in the Canvas by this factor.")]
[SerializeField] protected float m_ScaleFactor = 1;
///
/// Scales all UI elements in the Canvas by this factor.
///
///
/// Scales all UI elements in the Canvas by this factor.
///
public float scaleFactor { get { return m_ScaleFactor; } set { m_ScaleFactor = Mathf.Max(0.01f, value); } }
/// Scale the canvas area with the width as reference, the height as reference, or something in between.
///
/// Scale the canvas area with the width as reference, the height as reference, or something in between.
///
public enum ScreenMatchMode
{
///
/// Scale the canvas area with the width as reference, the height as reference, or something in between.
///
MatchWidthOrHeight = 0,
///
/// Expand the canvas area either horizontally or vertically, so the size of the canvas will never be smaller than the reference.
///
Expand = 1,
///
/// Crop the canvas area either horizontally or vertically, so the size of the canvas will never be larger than the reference.
///
Shrink = 2
}
[Tooltip("The resolution the UI layout is designed for. If the screen resolution is larger, the UI will be scaled up, and if it's smaller, the UI will be scaled down. This is done in accordance with the Screen Match Mode.")]
[SerializeField] protected Vector2 m_ReferenceResolution = new Vector2(800, 600);
///
/// The resolution the UI layout is designed for.
///
///
/// If the screen resolution is larger, the UI will be scaled up, and if it's smaller, the UI will be scaled down. This is done in accordance with the Screen Match Mode.
///
public Vector2 referenceResolution
{
get
{
return m_ReferenceResolution;
}
set
{
m_ReferenceResolution = value;
const float k_MinimumResolution = 0.00001f;
if (m_ReferenceResolution.x > -k_MinimumResolution && m_ReferenceResolution.x < k_MinimumResolution) m_ReferenceResolution.x = k_MinimumResolution * Mathf.Sign(m_ReferenceResolution.x);
if (m_ReferenceResolution.y > -k_MinimumResolution && m_ReferenceResolution.y < k_MinimumResolution) m_ReferenceResolution.y = k_MinimumResolution * Mathf.Sign(m_ReferenceResolution.y);
}
}
[Tooltip("A mode used to scale the canvas area if the aspect ratio of the current resolution doesn't fit the reference resolution.")]
[SerializeField] protected ScreenMatchMode m_ScreenMatchMode = ScreenMatchMode.MatchWidthOrHeight;
///
/// A mode used to scale the canvas area if the aspect ratio of the current resolution doesn't fit the reference resolution.
///
public ScreenMatchMode screenMatchMode { get { return m_ScreenMatchMode; } set { m_ScreenMatchMode = value; } }
[Tooltip("Determines if the scaling is using the width or height as reference, or a mix in between.")]
[Range(0, 1)]
[SerializeField] protected float m_MatchWidthOrHeight = 0;
///
/// Setting to scale the Canvas to match the width or height of the reference resolution, or a combination.
///
///
/// If the setting is set to 0, the Canvas is scaled according to the difference between the current screen resolution width and the reference resolution width. If the setting is set to 1, the Canvas is scaled according to the difference between the current screen resolution height and the reference resolution height.
///
/// For values in between 0 and 1, the scaling is based on a combination of the relative width and height.
///
/// Consider an example where the reference resolution of 640x480, and the current screen resolution is a landscape mode of 480x640.
///
/// If the scaleWidthOrHeight setting is set to 0, the Canvas is scaled by 0.75 because the current resolution width of 480 is 0.75 times the reference resolution width of 640. The Canvas resolution gets a resolution of 640x853.33. This resolution has the same width as the reference resolution width, but has the aspect ratio of the current screen resolution. Note that the Canvas resolution of 640x853.33 is the current screen resolution divided by the scale factor of 0.75.
///
/// If the scaleWidthOrHeight setting is set to 1, the Canvas is scaled by 1.33 because the current resolution height of 640 is 1.33 times the reference resolution height of 480. The Canvas resolution gets a resolution of 360x480. This resolution has the same height as the reference resolution width, but has the aspect ratio of the current screen resolution. Note that the Canvas resolution of 360x480 is the current screen resolution divided by the scale factor of 1.33.
///
/// If the scaleWidthOrHeight setting is set to 0.5, we find the horizontal scaling needed (0.75) and the vertical scaling needed (1.33) and find the average. However, we do the average in logarithmic space. A regular average of 0.75 and 1.33 would produce a result of 1.04. However, since multiplying by 1.33 is the same as diving by 0.75, the two scale factor really corresponds to multiplying by 0.75 versus dividing by 0.75, and the average of those two things should even out and produce a neutral result. The average in logarithmic space of 0.75 and 1.33 is exactly 1.0, which is what we want. The Canvas resolution hence ends up being 480x640 which is the current resolution divided by the scale factor of 1.0.
///
/// The logic works the same for all values. The average between the horizontal and vertical scale factor is a weighted average based on the matchWidthOrHeight value.
///
public float matchWidthOrHeight { get { return m_MatchWidthOrHeight; } set { m_MatchWidthOrHeight = value; } }
// The log base doesn't have any influence on the results whatsoever, as long as the same base is used everywhere.
private const float kLogBase = 2;
///
/// The possible physical unit types
///
public enum Unit
{
///
/// Use centimeters.
/// A centimeter is 1/100 of a meter
///
Centimeters,
///
/// Use millimeters.
/// A millimeter is 1/10 of a centimeter, and 1/1000 of a meter.
///
Millimeters,
///
/// Use inches.
///
Inches,
///
/// Use points.
/// One point is 1/12 of a pica, and 1/72 of an inch.
///
Points,
///
/// Use picas.
/// One pica is 1/6 of an inch.
///
Picas
}
[Tooltip("The physical unit to specify positions and sizes in.")]
[SerializeField] protected Unit m_PhysicalUnit = Unit.Points;
///
/// The physical unit to specify positions and sizes in.
///
public Unit physicalUnit { get { return m_PhysicalUnit; } set { m_PhysicalUnit = value; } }
[Tooltip("The DPI to assume if the screen DPI is not known.")]
[SerializeField] protected float m_FallbackScreenDPI = 96;
///
/// The DPI to assume if the screen DPI is not known.
///
public float fallbackScreenDPI { get { return m_FallbackScreenDPI; } set { m_FallbackScreenDPI = value; } }
[Tooltip("The pixels per inch to use for sprites that have a 'Pixels Per Unit' setting that matches the 'Reference Pixels Per Unit' setting.")]
[SerializeField] protected float m_DefaultSpriteDPI = 96;
///
/// The pixels per inch to use for sprites that have a 'Pixels Per Unit' setting that matches the 'Reference Pixels Per Unit' setting.
///
public float defaultSpriteDPI { get { return m_DefaultSpriteDPI; } set { m_DefaultSpriteDPI = Mathf.Max(1, value); } }
// World Canvas settings
[Tooltip("The amount of pixels per unit to use for dynamically created bitmaps in the UI, such as Text.")]
[SerializeField] protected float m_DynamicPixelsPerUnit = 1;
///
/// The amount of pixels per unit to use for dynamically created bitmaps in the UI, such as Text.
///
public float dynamicPixelsPerUnit { get { return m_DynamicPixelsPerUnit; } set { m_DynamicPixelsPerUnit = value; } }
// General variables
private Canvas m_Canvas;
[System.NonSerialized]
private float m_PrevScaleFactor = 1;
[System.NonSerialized]
private float m_PrevReferencePixelsPerUnit = 100;
[SerializeField] protected bool m_PresetInfoIsWorld = false;
protected CanvasScaler() {}
protected override void OnEnable()
{
base.OnEnable();
m_Canvas = GetComponent