NGUI panel使用soft clip时,屏幕缩放问题

我发现把那个panel的LocalScale的x,y,z的改成一样的就行了。我把他们的LocalScale.x和z都等于了y。再改一下Clipping的大小。效果不是最佳的,先将就着吧,呵呵~~~
对了,代码里面在Start里面运行SetPanel()时,最好用StartCoroutine或者Invoke,或者先yield一秒,我怕UI还没缩放就运行了就不好了,呵呵~~~我这里没写,你们自己写哈,呵呵~~~

using UnityEngine;
using System.Collections;

public class SubPanelPosition : MonoBehaviour {
	public ScreenDirection screenDirection;
	//horizontal表示水平滑动;vertical表示垂直滑动。
	public enum ScreenDirection 
	{
		horizontal,
		vertical
	}
	public float size;
	private Transform parent;
	private Transform child;
	private float ScaleSize;
	private float rateX;
	private float rateY;
	UIPanel PanelScript;
	void Start()
	{
		Invoke("SetPanel",0.5f);
	}
	void SetPanel()
	{
		parent = transform.parent;
		child = transform.GetChild(0);
		PanelScript = transform.GetComponent<UIPanel>();
		
		transform.parent = null;
		child.parent = null;
	
		
		if(screenDirection == ScreenDirection.vertical)
		{
		    rateX =  Screen.width/size;
			rateY = 1;
			ScaleSize = transform.localScale.y;
		}
		else if(screenDirection == ScreenDirection.horizontal)
		{
			rateX = 1;
            rateY = Screen.height/size;
			ScaleSize = transform.localScale.x;
		}
		
		transform.localScale = new Vector4(ScaleSize,ScaleSize,ScaleSize,ScaleSize);	
		transform.parent = parent;
		child.parent = transform;
		PanelScript.clipRange = new Vector4(PanelScript.clipRange.x,PanelScript.clipRange.y,PanelScript.clipRange.z * rateX,PanelScript.clipRange.w * rateY); 
	}
	
}

 

NGUI panel使用soft clip时,屏幕缩放问题_第1张图片

注:其中Screen Direction表示水平还是垂直滑动,如果是水平的话Size就是你当前发布时的Screen.width,如果是垂直的话,size就是Screen.height。

 

看看第二种方法,和前面差不多,改了一点~~~

using UnityEngine;
using System.Collections;

public class SubPanelPosition : MonoBehaviour {
	public ScreenDirection screenDirection;
	//horizontal表示水平滑动;vertical表示垂直滑动。
	public enum ScreenDirection 
	{
		horizontal,
		vertical
	}
	private Transform parent;
	private Transform child;
	private float ScaleSize;
	private float rateX;
	private float rateY;
	UIPanel PanelScript;
	void Start()
	{
		parent = transform.parent;
		child = transform.GetChild(0);
		PanelScript = transform.GetComponent<UIPanel>();
	}
	void SetPanel()
	{		
		transform.parent = null;
		child.parent = null;
	
		
		if(screenDirection == ScreenDirection.vertical)
		{
			ScaleSize = transform.localScale.y;
			rateX = ScaleSize/transform.localScale.x;
			rateY = 1;
		}
		else if(screenDirection == ScreenDirection.horizontal)
		{
			ScaleSize = transform.localScale.x;
			rateX = 1;
			rateY = ScaleSize/transform.localScale.y;
		}
		
		transform.localScale = new Vector4(ScaleSize,ScaleSize,ScaleSize,ScaleSize);	
		transform.parent = parent;
		child.parent = transform;
		PanelScript.clipRange = new Vector4(PanelScript.clipRange.x,PanelScript.clipRange.y,PanelScript.clipRange.z * rateX,PanelScript.clipRange.w * rateY); 
	}
	
}


注:手机上如果不行的话就只能用摄像机的方法了~~~~
 

你可能感兴趣的:(NGUI panel使用soft clip时,屏幕缩放问题)