解决NGUI panel使用soft clip时 屏幕缩放问题
unity3D更多资源教程免费下载,群193521697 邀请码:10026.(有问题找管理员)
2014年8月15日起 每周五晚8:00有Unity公开课(具体授课方式群里面有详细说明)
NGUI panel使用softclip时,屏幕缩放问题如何解决呢?
把panel的LocalScale的x,y,z的改成一样的就行了。把他们的LocalScale.x和z都等于了y。再改一下Clipping的大小。代码里面在Start里面运行SetPanel()时,最好用StartCoroutine或者Invoke,或者先yield一秒,我怕UI还没缩放就运行了就不好了。
|
|
01 |
using UnityEngine; |
02 |
using System.Collections; |
03 |
|
04 |
public class SubPanelPosition : MonoBehaviour { |
05 |
public ScreenDirection screenDirection; |
06 |
//horizontal表示水平滑动;vertical表示垂直滑动。 |
07 |
public enum ScreenDirection |
08 |
{ |
09 |
horizontal, |
10 |
vertical |
11 |
} //Unity3D教程手册:www.unitymanual.com |
12 |
public float size; |
13 |
private Transform parent; |
14 |
private Transform child; |
15 |
private float ScaleSize; |
16 |
private float rateX; |
17 |
private float rateY; |
18 |
UIPanel PanelScript; |
19 |
void Start() |
20 |
{ |
21 |
Invoke("SetPanel",0.5f); |
22 |
} |
23 |
void SetPanel() |
24 |
{ |
25 |
parent = transform.parent; |
26 |
child = transform.GetChild(0); |
27 |
PanelScript = transform.GetComponent<UIPanel>(); |
28 |
|
29 |
transform.parent = null; |
30 |
child.parent = null; |
31 |
|
32 |
//Unity3D教程手册:www.unitymanual.com |
33 |
if(screenDirection == ScreenDirection.vertical) |
34 |
{ |
35 |
rateX = Screen.width/size; |
36 |
rateY = 1; |
37 |
ScaleSize = transform.localScale.y; |
38 |
} |
39 |
else if(screenDirection == ScreenDirection.horizontal) |
40 |
{ |
41 |
rateX = 1; |
42 |
rateY = Screen.height/size; |
43 |
ScaleSize = transform.localScale.x; |
44 |
} |
45 |
|
46 |
transform.localScale = new Vector4(ScaleSize,ScaleSize,ScaleSize,ScaleSize); |
47 |
transform.parent = parent; |
48 |
child.parent = transform; |
49 |
PanelScript.clipRange = new Vector4(PanelScript.clipRange.x,PanelScript.clipRange.y,PanelScript.clipRange.z * rateX,PanelScript.clipRange.w * rateY); |
50 |
} |
51 |
|
52 |
} |