|
- using System;
- using UnityEngine;
- using UnityEngine.EventSystems;
-
- namespace CIG
- {
- [RequireComponent(typeof(Canvas))]
- [ExecuteInEditMode]
- [AddComponentMenu("Layout/CIG Canvas Scaler", 101)]
- public class CIGCanvasScaler : UIBehaviour
- {
- protected override void Awake()
- {
- base.Awake();
- if (this._canvas == null)
- {
- this._canvas = base.GetComponent<Canvas>();
- }
- }
-
- protected override void Start()
- {
- base.Start();
- this._currentDPI = Screen.dpi;
- }
-
- protected override void OnEnable()
- {
- base.OnEnable();
- this.Handle();
- }
-
- protected override void OnDisable()
- {
- this.SetScaleFactor(1f);
- this.SetReferencePixelsPerUnit(100f);
- base.OnDisable();
- }
-
- private void Update()
- {
- this.Handle();
- }
-
- public CIGCanvasScaler.ScaleMode UIScaleMode
- {
- get
- {
- return this._uiScaleMode;
- }
- set
- {
- this._uiScaleMode = value;
- }
- }
-
- public float ReferencePixelsPerUnit
- {
- get
- {
- return this._referencePixelsPerUnit;
- }
- set
- {
- this._referencePixelsPerUnit = value;
- }
- }
-
- public Vector2 ReferenceResolution
- {
- get
- {
- return this._referenceResolution;
- }
- set
- {
- this._referenceResolution = value;
- }
- }
-
- public float MatchWidthOrHeight
- {
- get
- {
- return this._matchWidthOrHeight;
- }
- set
- {
- this._matchWidthOrHeight = value;
- }
- }
-
- public float ReferenceDPI
- {
- get
- {
- return this._referenceDPI;
- }
- set
- {
- this._referenceDPI = value;
- }
- }
-
- public float ScaleFactor
- {
- get
- {
- return this._scaleFactor;
- }
- set
- {
- this._scaleFactor = value;
- }
- }
-
- public float LargeScreenMatchWidthOrHeight
- {
- get
- {
- return this._largeScreenMatchWidthOrHeight;
- }
- set
- {
- this._largeScreenMatchWidthOrHeight = value;
- }
- }
-
- public float LargeScreenThresholdInches
- {
- get
- {
- return this._largeScreenThresholdInches;
- }
- set
- {
- this._largeScreenThresholdInches = value;
- }
- }
-
- public float MinimumCanvasScale
- {
- get
- {
- return this._minimumCanvasScale;
- }
- set
- {
- this._minimumCanvasScale = value;
- }
- }
-
- public float LargeScreenDampeningFactor
- {
- get
- {
- return this._largeScreenDampeningFactor;
- }
- set
- {
- this._largeScreenDampeningFactor = value;
- }
- }
-
- private void Handle()
- {
- if (this._canvas == null || !this._canvas.isRootCanvas || this._canvas.renderMode == RenderMode.WorldSpace)
- {
- return;
- }
- CIGCanvasScaler.ScaleMode uiScaleMode = this._uiScaleMode;
- switch (uiScaleMode)
- {
- case CIGCanvasScaler.ScaleMode.ConstantPixelSize:
- this.HandleConstantPixelSize();
- break;
- case CIGCanvasScaler.ScaleMode.ScaleWithScreenSize:
- this.HandleScaleWithScreenSize();
- break;
- case CIGCanvasScaler.ScaleMode.ConstantPhysicalSize:
- this.HandleConstantPhysicalSize();
- break;
- default:
- if (uiScaleMode == CIGCanvasScaler.ScaleMode.ScaleWithDPIScreenSize)
- {
- this.HandleScaleWithDPIScreenSize();
- }
- break;
- }
- }
-
- private void HandleConstantPixelSize()
- {
- this.SetScaleFactor(this._scaleFactor);
- this.SetReferencePixelsPerUnit(this._referencePixelsPerUnit);
- }
-
- private void HandleScaleWithScreenSize()
- {
- float scaleFactor = this.CalculateScreenSizeScaleFactor();
- this.SetScaleFactor(scaleFactor);
- this.SetReferencePixelsPerUnit(this._referencePixelsPerUnit);
- }
-
- private float CalculateScreenSizeScaleFactor()
- {
- Vector2 vector = new Vector2((float)Screen.width, (float)Screen.height);
- float a = Mathf.Log(vector.x / this._referenceResolution.x, 2f);
- float b = Mathf.Log(vector.y / this._referenceResolution.y, 2f);
- float p = Mathf.Lerp(a, b, this._matchWidthOrHeight);
- return Mathf.Pow(2f, p);
- }
-
- private void HandleConstantPhysicalSize()
- {
- float num = (this._currentDPI > 0f) ? this._currentDPI : this._referenceDPI;
- float scaleFactor = num / this._referenceDPI * this._scaleFactor;
- this.SetScaleFactor(scaleFactor);
- this.SetReferencePixelsPerUnit(this._referencePixelsPerUnit);
- }
-
- private void HandleScaleWithDPIScreenSize()
- {
- float num = this.CalculateScreenSizeScaleFactor();
- float num2 = (this._currentDPI > 0f) ? this._currentDPI : this._referenceDPI;
- float num3 = Mathf.Lerp((float)Screen.width, (float)Screen.height, this._matchWidthOrHeight) / num2;
- float num4 = 1f;
- if (num3 > this._largeScreenThresholdInches)
- {
- float num5 = num3 - this._largeScreenThresholdInches;
- num4 -= num5 * this._largeScreenDampeningFactor;
- }
- float num6 = num * num4;
- num6 = Mathf.Max(this._minimumCanvasScale, num6);
- this.SetScaleFactor(num6);
- this.SetReferencePixelsPerUnit(this._referencePixelsPerUnit);
- }
-
- private void SetScaleFactor(float scaleFactor)
- {
- if (scaleFactor == this._prevScaleFactor)
- {
- return;
- }
- this._canvas.scaleFactor = scaleFactor;
- this._prevScaleFactor = scaleFactor;
- }
-
- private void SetReferencePixelsPerUnit(float referencePixelsPerUnit)
- {
- if (referencePixelsPerUnit == this._prevReferencePixelsPerUnit)
- {
- return;
- }
- this._canvas.referencePixelsPerUnit = referencePixelsPerUnit;
- this._prevReferencePixelsPerUnit = referencePixelsPerUnit;
- }
-
- private const float LogBase = 2f;
-
- [Tooltip("The Canvas to which the Scale Factor and Reference PPU will be applied.")]
- [SerializeField]
- private Canvas _canvas;
-
- [Tooltip("Determines how UI elements in the Canvas are scaled.")]
- [SerializeField]
- private CIGCanvasScaler.ScaleMode _uiScaleMode = CIGCanvasScaler.ScaleMode.ScaleWithDPIScreenSize;
-
- [Tooltip("If a sprite has this 'Pixels Per Unit' setting, then one pixel in the sprite will cover one unit in the UI.")]
- [SerializeField]
- private float _referencePixelsPerUnit = 100f;
-
- [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]
- private Vector2 _referenceResolution = new Vector2(1280f, 720f);
-
- [Tooltip("The DPI the UI layout is designed for. Or to use when the screen DPI is not known.")]
- [SerializeField]
- private float _referenceDPI = 96f;
-
- [Tooltip("Determines if the scaling is using the width or height as reference, or a mix in between.")]
- [Range(0f, 1f)]
- [SerializeField]
- private float _matchWidthOrHeight = 1f;
-
- [Tooltip("Scales all UI elements in the Canvas by this factor. (Only used for Constant Pixel and Physical Size)")]
- [SerializeField]
- private float _scaleFactor = 1f;
-
- [Tooltip("Determines if the Large Screen Threshold formula is using the width or height as reference, or a mix in between.")]
- [Range(0f, 1f)]
- [SerializeField]
- private float _largeScreenMatchWidthOrHeight = 1f;
-
- [Tooltip("The Threshold (in Inches) used to determine if a screen is considered large enough to have its scaling factor dampened.")]
- [SerializeField]
- private float _largeScreenThresholdInches = 2.69f;
-
- [Tooltip("The Dampening Factor used when calculating the scaling factor for Large Screens.")]
- [SerializeField]
- private float _largeScreenDampeningFactor = 0.025f;
-
- [Tooltip("The scale of the canvas will never go below this value.")]
- [SerializeField]
- private float _minimumCanvasScale = 0.25f;
-
- private float _currentDPI;
-
- private float _prevScaleFactor = 1f;
-
- private float _prevReferencePixelsPerUnit = 100f;
-
- public enum ScaleMode
- {
- ConstantPixelSize,
- ScaleWithScreenSize,
- ConstantPhysicalSize,
- ScaleWithDPIScreenSize = 99
- }
- }
- }
|