unity ngui自适应

using UnityEngine;
using System.Collections;

public class TestChildPosition : MonoBehaviour {
	Bounds bounds;
	// Use this for initialization
	void Start () {
		bounds = NGUIMath.CalculateRelativeWidgetBounds(transform.parent, transform);
		//Debug.Log(bounds.min);
		
	}
	
	// Update is called once per frame
	void Update () {
		
		transform.localPosition = new Vector3(Screen.width/2-bounds.max.x, 
			transform.localPosition.y,
			transform.localPosition.z);
		//Debug.Log(bounds.max.x-bounds.min.x);
	}
}
NGUIMath.CalculateRelativeWidgetBounds

这个方法可以得到嵌套 的 gameobject 最外围边框,从而实现自适应


还有一种情况,是摄像机的

Camera.orthographicSize 正交大小 


假如不为1的情况下布局还是受影响
解决方法是屏幕的宽度*比例
(Screen.width/2*nguiCamera.orthographicSize)-bounds.max.x
这样图片可完全的靠右对齐,其它对齐方式同理,这里不多讲了


猜你喜欢

转载自blog.csdn.net/c332937586/article/details/23370505
今日推荐