forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasScaler.cs
More file actions
355 lines (316 loc) · 8.48 KB
/
Copy pathCanvasScaler.cs
File metadata and controls
355 lines (316 loc) · 8.48 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System;
using UnityEngine.EventSystems;
namespace UnityEngine.UI
{
[AddComponentMenu("Layout/Canvas Scaler", 101), ExecuteInEditMode, RequireComponent(typeof(Canvas))]
public class CanvasScaler : UIBehaviour
{
public enum ScaleMode
{
ConstantPixelSize,
ScaleWithScreenSize,
ConstantPhysicalSize
}
public enum ScreenMatchMode
{
MatchWidthOrHeight,
Expand,
Shrink
}
public enum Unit
{
Centimeters,
Millimeters,
Inches,
Points,
Picas
}
[SerializeField, Tooltip("Determines how UI elements in the Canvas are scaled.")]
private CanvasScaler.ScaleMode m_UiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize;
[SerializeField, Tooltip("If a sprite has this 'Pixels Per Unit' setting, then one pixel in the sprite will cover one unit in the UI.")]
protected float m_ReferencePixelsPerUnit = 100f;
[SerializeField, Tooltip("Scales all UI elements in the Canvas by this factor.")]
protected float m_ScaleFactor = 1f;
[SerializeField, 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.")]
protected Vector2 m_ReferenceResolution = new Vector2(800f, 600f);
[SerializeField, Tooltip("A mode used to scale the canvas area if the aspect ratio of the current resolution doesn't fit the reference resolution.")]
protected CanvasScaler.ScreenMatchMode m_ScreenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
[Range(0f, 1f), SerializeField, Tooltip("Determines if the scaling is using the width or height as reference, or a mix in between.")]
protected float m_MatchWidthOrHeight = 0f;
private const float kLogBase = 2f;
[SerializeField, Tooltip("The physical unit to specify positions and sizes in.")]
protected CanvasScaler.Unit m_PhysicalUnit = CanvasScaler.Unit.Points;
[SerializeField, Tooltip("The DPI to assume if the screen DPI is not known.")]
protected float m_FallbackScreenDPI = 96f;
[SerializeField, Tooltip("The pixels per inch to use for sprites that have a 'Pixels Per Unit' setting that matches the 'Reference Pixels Per Unit' setting.")]
protected float m_DefaultSpriteDPI = 96f;
[SerializeField, Tooltip("The amount of pixels per unit to use for dynamically created bitmaps in the UI, such as Text.")]
protected float m_DynamicPixelsPerUnit = 1f;
private Canvas m_Canvas;
[NonSerialized]
private float m_PrevScaleFactor = 1f;
[NonSerialized]
private float m_PrevReferencePixelsPerUnit = 100f;
public CanvasScaler.ScaleMode uiScaleMode
{
get
{
return this.m_UiScaleMode;
}
set
{
this.m_UiScaleMode = value;
}
}
public float referencePixelsPerUnit
{
get
{
return this.m_ReferencePixelsPerUnit;
}
set
{
this.m_ReferencePixelsPerUnit = value;
}
}
public float scaleFactor
{
get
{
return this.m_ScaleFactor;
}
set
{
this.m_ScaleFactor = Mathf.Max(0.01f, value);
}
}
public Vector2 referenceResolution
{
get
{
return this.m_ReferenceResolution;
}
set
{
this.m_ReferenceResolution = value;
if (this.m_ReferenceResolution.x > -1E-05f && this.m_ReferenceResolution.x < 1E-05f)
{
this.m_ReferenceResolution.x = 1E-05f * Mathf.Sign(this.m_ReferenceResolution.x);
}
if (this.m_ReferenceResolution.y > -1E-05f && this.m_ReferenceResolution.y < 1E-05f)
{
this.m_ReferenceResolution.y = 1E-05f * Mathf.Sign(this.m_ReferenceResolution.y);
}
}
}
public CanvasScaler.ScreenMatchMode screenMatchMode
{
get
{
return this.m_ScreenMatchMode;
}
set
{
this.m_ScreenMatchMode = value;
}
}
public float matchWidthOrHeight
{
get
{
return this.m_MatchWidthOrHeight;
}
set
{
this.m_MatchWidthOrHeight = value;
}
}
public CanvasScaler.Unit physicalUnit
{
get
{
return this.m_PhysicalUnit;
}
set
{
this.m_PhysicalUnit = value;
}
}
public float fallbackScreenDPI
{
get
{
return this.m_FallbackScreenDPI;
}
set
{
this.m_FallbackScreenDPI = value;
}
}
public float defaultSpriteDPI
{
get
{
return this.m_DefaultSpriteDPI;
}
set
{
this.m_DefaultSpriteDPI = Mathf.Max(1f, value);
}
}
public float dynamicPixelsPerUnit
{
get
{
return this.m_DynamicPixelsPerUnit;
}
set
{
this.m_DynamicPixelsPerUnit = value;
}
}
protected CanvasScaler()
{
}
protected override void OnEnable()
{
base.OnEnable();
this.m_Canvas = base.GetComponent<Canvas>();
this.Handle();
}
protected override void OnDisable()
{
this.SetScaleFactor(1f);
this.SetReferencePixelsPerUnit(100f);
base.OnDisable();
}
protected virtual void Update()
{
this.Handle();
}
protected virtual void Handle()
{
if (!(this.m_Canvas == null) && this.m_Canvas.isRootCanvas)
{
if (this.m_Canvas.renderMode == RenderMode.WorldSpace)
{
this.HandleWorldCanvas();
}
else
{
CanvasScaler.ScaleMode uiScaleMode = this.m_UiScaleMode;
if (uiScaleMode != CanvasScaler.ScaleMode.ConstantPixelSize)
{
if (uiScaleMode != CanvasScaler.ScaleMode.ScaleWithScreenSize)
{
if (uiScaleMode == CanvasScaler.ScaleMode.ConstantPhysicalSize)
{
this.HandleConstantPhysicalSize();
}
}
else
{
this.HandleScaleWithScreenSize();
}
}
else
{
this.HandleConstantPixelSize();
}
}
}
}
protected virtual void HandleWorldCanvas()
{
this.SetScaleFactor(this.m_DynamicPixelsPerUnit);
this.SetReferencePixelsPerUnit(this.m_ReferencePixelsPerUnit);
}
protected virtual void HandleConstantPixelSize()
{
this.SetScaleFactor(this.m_ScaleFactor);
this.SetReferencePixelsPerUnit(this.m_ReferencePixelsPerUnit);
}
protected virtual void HandleScaleWithScreenSize()
{
Vector2 vector = new Vector2((float)Screen.width, (float)Screen.height);
int targetDisplay = this.m_Canvas.targetDisplay;
if (targetDisplay > 0 && targetDisplay < Display.displays.Length)
{
Display display = Display.displays[targetDisplay];
vector = new Vector2((float)display.renderingWidth, (float)display.renderingHeight);
}
float scaleFactor = 0f;
CanvasScaler.ScreenMatchMode screenMatchMode = this.m_ScreenMatchMode;
if (screenMatchMode != CanvasScaler.ScreenMatchMode.MatchWidthOrHeight)
{
if (screenMatchMode != CanvasScaler.ScreenMatchMode.Expand)
{
if (screenMatchMode == CanvasScaler.ScreenMatchMode.Shrink)
{
scaleFactor = Mathf.Max(vector.x / this.m_ReferenceResolution.x, vector.y / this.m_ReferenceResolution.y);
}
}
else
{
scaleFactor = Mathf.Min(vector.x / this.m_ReferenceResolution.x, vector.y / this.m_ReferenceResolution.y);
}
}
else
{
float a = Mathf.Log(vector.x / this.m_ReferenceResolution.x, 2f);
float b = Mathf.Log(vector.y / this.m_ReferenceResolution.y, 2f);
float p = Mathf.Lerp(a, b, this.m_MatchWidthOrHeight);
scaleFactor = Mathf.Pow(2f, p);
}
this.SetScaleFactor(scaleFactor);
this.SetReferencePixelsPerUnit(this.m_ReferencePixelsPerUnit);
}
protected virtual void HandleConstantPhysicalSize()
{
float dpi = Screen.dpi;
float num = (dpi != 0f) ? dpi : this.m_FallbackScreenDPI;
float num2 = 1f;
switch (this.m_PhysicalUnit)
{
case CanvasScaler.Unit.Centimeters:
num2 = 2.54f;
break;
case CanvasScaler.Unit.Millimeters:
num2 = 25.4f;
break;
case CanvasScaler.Unit.Inches:
num2 = 1f;
break;
case CanvasScaler.Unit.Points:
num2 = 72f;
break;
case CanvasScaler.Unit.Picas:
num2 = 6f;
break;
}
this.SetScaleFactor(num / num2);
this.SetReferencePixelsPerUnit(this.m_ReferencePixelsPerUnit * num2 / this.m_DefaultSpriteDPI);
}
protected void SetScaleFactor(float scaleFactor)
{
if (scaleFactor != this.m_PrevScaleFactor)
{
this.m_Canvas.scaleFactor = scaleFactor;
this.m_PrevScaleFactor = scaleFactor;
}
}
protected void SetReferencePixelsPerUnit(float referencePixelsPerUnit)
{
if (referencePixelsPerUnit != this.m_PrevReferencePixelsPerUnit)
{
this.m_Canvas.referencePixelsPerUnit = referencePixelsPerUnit;
this.m_PrevReferencePixelsPerUnit = referencePixelsPerUnit;
}
}
protected override void OnValidate()
{
this.m_ScaleFactor = Mathf.Max(0.01f, this.m_ScaleFactor);
this.m_DefaultSpriteDPI = Mathf.Max(1f, this.m_DefaultSpriteDPI);
}
}
}