赞
踩
1
2
3
4
5
6
7
8
|
protected
void
SetScaleFactor
(
float
scaleFactor
)
{
if
(
scaleFactor
==
m_PrevScaleFactor
)
return
;
m_Canvas
.
scaleFactor
=
scaleFactor
;
m_PrevScaleFactor
=
scaleFactor
;
}
|
Sprite 在世界座標中大小 = 原圖大小(Pixels) / Pixels Per Unit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
float
pixelsPerUnit
{
get
{
float
spritePixelsPerUnit
=
100
;
if
(
sprite
)
spritePixelsPerUnit
=
sprite
.
pixelsPerUnit
;
float
referencePixelsPerUnit
=
100
;
if
(
canvas
)
referencePixelsPerUnit
=
canvas
.
referencePixelsPerUnit
;
return
spritePixelsPerUnit
/
referencePixelsPerUnit
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
public
override
void
SetNativeSize
(
)
{
if
(
overrideSprite
!=
null
)
{
float
w
=
overrideSprite
.
rect
.
width
/
pixelsPerUnit
;
float
h
=
overrideSprite
.
rect
.
height
/
pixelsPerUnit
;
rectTransform
.
anchorMax
=
rectTransform
.
anchorMin
;
rectTransform
.
sizeDelta
=
new
Vector2
(
w
,
h
)
;
SetAllDirty
(
)
;
}
}
|
Reference Pixels Per Unit | Pixels Per Unit | Image Rect Transform(w*h) |
100 | 100 | 100*100 |
200 | 100 | 200*200 |
100 | 10 | 1000*1000 |
200 | 10 | 2000*2000 |
UI大小 = 原圖大小(Pixels) / (Pixels Per Unit / Reference Pixels Per Unit)
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
|
Vector2
screenSize
=
new
Vector2
(
Screen
.
width
,
Screen
.
height
)
;
float
scaleFactor
=
0
;
switch
(
m_ScreenMatchMode
)
{
case
ScreenMatchMode
.
MatchWidthOrHeight
:
{
// We take the log of the relative width and height before taking the average.
// Then we transform it back in the original space.
// the reason to transform in and out of logarithmic space is to have better behavior.
// If one axis has twice resolution and the other has half, it should even out if widthOrHeight value is at 0.5.
// In normal space the average would be (0.5 + 2) / 2 = 1.25
// In logarithmic space the average is (-1 + 1) / 2 = 0
float
logWidth
=
Mathf
.
Log
(
screenSize
.
x
/
m_ReferenceResolution
.
x
,
kLogBase
)
;
float
logHeight
=
Mathf
.
Log
(
screenSize
.
y
/
m_ReferenceResolution
.
y
,
kLogBase
)
;
float
logWeightedAverage
=
Mathf
.
Lerp
(
logWidth
,
logHeight
,
m_MatchWidthOrHeight
)
;
scaleFactor
=
Mathf
.
Pow
(
kLogBase
,
logWeightedAverage
)
;
break
;
}
case
ScreenMatchMode
.
Expand
:
{
scaleFactor
=
Mathf
.
Min
(
screenSize
.
x
/
m_ReferenceResolution
.
x
,
screenSize
.
y
/
m_ReferenceResolution
.
y
)
;
break
;
}
case
ScreenMatchMode
.
Shrink
:
{
scaleFactor
=
Mathf
.
Max
(
screenSize
.
x
/
m_ReferenceResolution
.
x
,
screenSize
.
y
/
m_ReferenceResolution
.
y
)
;
break
;
}
}
|
1
|
scaleFactor
=
Mathf
.
Min
(
screenSize
.
x
/
m_ReferenceResolution
.
x
,
screenSize
.
y
/
m_ReferenceResolution
.
y
)
;
|
1
|
scaleFactor
=
Mathf
.
Max
(
screenSize
.
x
/
m_ReferenceResolution
.
x
,
screenSize
.
y
/
m_ReferenceResolution
.
y
)
;
|
1
2
3
4
|
float
logWidth
=
Mathf
.
Log
(
screenSize
.
x
/
m_ReferenceResolution
.
x
,
kLogBase
)
;
float
logHeight
=
Mathf
.
Log
(
screenSize
.
y
/
m_ReferenceResolution
.
y
,
kLogBase
)
;
float
logWeightedAverage
=
Mathf
.
Lerp
(
logWidth
,
logHeight
,
m_MatchWidthOrHeight
)
;
scaleFactor
=
Mathf
.
Pow
(
kLogBase
,
logWeightedAverage
)
;
|
單位種類 | 中文 | 與1英吋關係 |
Centimeters | 公分(cm,厘米) | 2.54 |
Millimeters | 公釐(mm,毫米) | 25.4 |
Inches | 英吋 | 1 |
Points | 點 | 72 |
Picas | 皮卡(十二點活字) | 6 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
float
currentDpi
=
Screen
.
dpi
;
float
dpi
=
(
currentDpi
==
0
?
m_FallbackScreenDPI
:
currentDpi
)
;
float
targetDPI
=
1
;
switch
(
m_PhysicalUnit
)
{
case
Unit
.
Centimeters
:
targetDPI
=
2.54f
;
break
;
case
Unit
.
Millimeters
:
targetDPI
=
25.4f
;
break
;
case
Unit
.
Inches
:
targetDPI
=
1
;
break
;
case
Unit
.
Points
:
targetDPI
=
72
;
break
;
case
Unit
.
Picas
:
targetDPI
=
6
;
break
;
}
SetScaleFactor
(
dpi
/
targetDPI
)
;
SetReferencePixelsPerUnit
(
m_ReferencePixelsPerUnit
*
targetDPI
/
m_DefaultSpriteDPI
)
;
|
新的 Reference Pixels Per Unit = Reference Pixels Per Unit * Physical Unit / Default Sprite DPI
UI大小 = 原圖大小(Pixels) / (Pixels Per Unit / 新的 Reference Pixels Per Unit)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。