`

NGUI 组件 UIRoot和UICamera

    博客分类:
  • U3D
阅读更多

UIRoot:

 

public class UIRoot : MonoBehaviour
{
	static public List<UIRoot> list = new List<UIRoot>();

	public enum Scaling
	{
		PixelPerfect, // 像素不变,大小变
		FixedSize, // 大小不变,像素变
		FixedSizeOnMobiles,
	}
	public Scaling scalingStyle = Scaling.PixelPerfect;
	public int manualHeight = 720;
	public int minimumHeight = 320;
	public int maximumHeight = 1536;
	public bool adjustByDPI = false;
	public bool shrinkPortraitUI = false;

	public int activeHeight
	{
		get
		{
			if (scalingStyle == Scaling.FixedSize) return manualHeight;

#if UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_BLACKBERRY
			if (scalingStyle == Scaling.FixedSizeOnMobiles)
				return manualHeight;
#endif
			Vector2 screen = NGUITools.screenSize;
			float aspect = screen.x / screen.y;
			
			if (screen.y < minimumHeight)
			{
				screen.y = minimumHeight;
				screen.x = screen.y * aspect;
			}
			else if (screen.y > maximumHeight)
			{
				screen.y = maximumHeight;
				screen.x = screen.y * aspect;
			}

			// Portrait mode uses the maximum of width or height to shrink the UI
			int height = Mathf.RoundToInt((shrinkPortraitUI && screen.y > screen.x) ? screen.y / aspect : screen.y);

			// Adjust the final value by the DPI setting
			return adjustByDPI ? NGUIMath.AdjustByDPI(height) : height;
		}
	}

	static public void Broadcast (string funcName)
	{
#if UNITY_EDITOR
		if (Application.isPlaying)
#endif
		{
			for (int i = 0, imax = list.Count; i < imax; ++i)
			{
				UIRoot root = list[i];
				if (root != null) root.BroadcastMessage(funcName, SendMessageOptions.DontRequireReceiver);
			}
		}
	}

}

 1.这个组件以静态的方式管理了所有的组件在一个list里面UIRoot,并且可以使用Broadcast方法进行消息广播。

2.这个组件是UI的根节点,它主要是通过缩放自己的方式来控制整个UI的大小,两种方式:PixelPerfect 和 FixedSize。

3.如果adjustByDPI = true,则高度为屏幕当前高度。反之,为manualHeight。

http://blog.csdn.net/asd237241291/article/details/8126619 这是一篇关于自适应屏幕大小的文章,我个人觉得没必要搞的这么复杂。一般使用FixedSize即可。

整个UIRoot最核心的功能就是整体缩放,所以必须把这块理解清楚。

activeHeight:1.类型为FixedSize时,它的值为manualHeight。2.adjustByDPI = true时,它为当前屏幕的高度。3.否则,activeHeight = 屏幕高度 的值会夹在一个范围内[minimum,maximum],如果超出这个范围,屏幕的宽度会等比缩放。

FixedSize:这种模式下,activeHeight的值跟屏幕本身的高度是无关的,你可以理解为固定大小。

pixelperfect:这种模式下有两种选择,一种是没有范围约束直接获取屏幕的高度,另一种是有范围约束。

可以肯定的是,无论是那种模式,U3D都不希望出现高宽非等比例缩放的情况,因为这样的话界面就会变形。

 

 UIOrthoCamera:

public class UIOrthoCamera : MonoBehaviour
{
	Camera mCam;
	Transform mTrans;

	void Start ()
	{
		mCam = camera;
		mTrans = transform;
		mCam.orthographic = true;
	}

	void Update ()
	{
		float y0 = mCam.rect.yMin * Screen.height;
		float y1 = mCam.rect.yMax * Screen.height;

		float size = (y1 - y0) * 0.5f * mTrans.lossyScale.y;
		if (!Mathf.Approximately(mCam.orthographicSize, size)) mCam.orthographicSize = size;
	}
}

很多文章都只是说用了这玩意就不能用UIRoot,可以简单的理解为它是PixelPerfect 适应方案,并且采用的是屏幕的高度,而不是人为设置的高度。

 

UICamera:

Event Type:四种类型,决定相机以什么样的方式碰撞检测,也可以理解为相机只检测什么样的控件。

	public enum EventType : int
	{
		World_3D,	// Perform a Physics.Raycast and sort by distance to the point that was hit.
		UI_3D,		// Perform a Physics.Raycast and sort by widget depth.
		World_2D,	// Perform a Physics2D.OverlapPoint
		UI_2D,		// Physics2D.OverlapPoint then sort by widget depth
	}

Event Mask:决定触发什么类型的事件,可以是所有类型。

allow multi touch:勾选,就表示支持多点触控。

sticky tooltip:是否使用tooltip,在NGUI里面有个UITooltip专门用来处理tip这个需求的。

tooltip Delay:这个地方设置tip停留的时间

http://blog.csdn.net/tinyhum3d/article/details/9069639 这篇文章详细介绍NGUI里面制作tip相关的东西。

UICamera主要是负责事件的,其他的还有一些跟事件相关的选项

 

 

  • 大小: 26.6 KB
  • 大小: 25.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics