NGUI中UIWidget提供了锚点Anchor选项,可以方便地设置一个Widget相对于另一个Widget的位置和大小。
我用的Unity版本:3.5.6;NGUI版本:3.5.9
今天做项目时遇到一个问题:在SceneA和SceneB中都有UIRoot,由于某些原因,我必须以LoadLevelAdditiveAsync的方式加载SceneB,并在进入SceneB后销毁SceneA。我们知道在一个场景下最多只能存在一个UIRoot,但从SceneA跳转到SceneB后,场景中同时存在了两个UIRoot,这导致某些使用了Anchor的UI的位置和大小不正确(实测Unity Editor中不会出问题,但在手机上基本都会出问题)。
此时有两种解决办法:
1.在SceneA销毁后再呈现SceneB,也就是不要用Additive的方式加载,但由于我的项目中两个Scene的耦合太高了(接的前人的盘),这么做会非常费时,老板一直在催版本,没时间重构。
2.自己实现Anchor功能。
代码很简单,大致思路如下:1.根据Target的位置和大小计算出Target的左右上下四个点坐标(实际上只需要用到左tLeftBoardPos和下tBottomBoardPos);2.根据输入的左右上下的偏移量,计算出当前Widget的左右X坐标(mLeftBoardX, mRightBoardX)和上下Y坐标(mTopBoardY, mBottomBoardY);3.根据2求得的四个值计算当前Widget的位置和大小。
需要注意的是:这里用的始终是世界坐标,且需要考虑UIRoot本身的缩放系数scaleRatio。
==注:暂时没有考虑Center的情况,有需要了再加上。==
下面是具体实现:
SimpleAnchor.cs
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(UIWidget))]
public class SimpleAnchor : MonoBehaviour
{
public enum HorizPivot
{
Left,
Right,
Center
}
public enum VertPivot
{
Top,
Bottom,
Center
}
public Transform Target;
public int Left;
public HorizPivot LeftPivot;
public int Right;
public HorizPivot RightPivot;
public int Bottom;
public VertPivot BottomPivot;
public int Top;
public VertPivot TopPivot;
void OnEnable()
{
if(!Target)return;
UIWidget targetWidget = Target.GetComponent<UIWidget>();
UIPanel targetPanel = Target.GetComponent<UIPanel>();
if(!targetPanel && !targetWidget) return;
if (targetWidget)
{
SetPosAndSize(targetWidget.transform.position, targetWidget.width, targetWidget.height);
}
else
{
SetPosAndSize(targetPanel.transform.position, (int)targetPanel.width, (int)targetPanel.height);
}
}
void SetPosAndSize(Vector3 targetPos, int targetWidth, int targetHeight)
{
Debug.LogError("Target width: " + targetWidth);
Debug.LogError("Target height: " + targetHeight);
float scaleRatio = NGUITools.FindInParents<UIRoot>(transform).transform.localScale.x;
Vector3 tLeftBoardPos = new Vector3(targetPos.x - scaleRatio * targetWidth / 2, targetPos.y, targetPos.z);
Vector3 tRightBoardPos = new Vector3(targetPos.x + scaleRatio * targetWidth / 2, targetPos.y, targetPos.z);
Vector3 tTopBoardPos = new Vector3(targetPos.x, targetPos.y + scaleRatio * targetHeight / 2, targetPos.z);
Vector3 tBottomBoardPos = new Vector3(targetPos.x, targetPos.y - scaleRatio * targetHeight / 2, targetPos.z);
float mLeftBoardX = 0;
if (LeftPivot == HorizPivot.Left)
{
mLeftBoardX = tLeftBoardPos.x + scaleRatio * Left;
}
else if (LeftPivot == HorizPivot.Right)
{
mLeftBoardX = tLeftBoardPos.x + scaleRatio * (targetWidth + Left);
}
float mRightBoardX = 0;
if (RightPivot == HorizPivot.Left)
{
mRightBoardX = tLeftBoardPos.x + scaleRatio * Right;
}
else if (LeftPivot == HorizPivot.Right)
{
mRightBoardX = tLeftBoardPos.x + scaleRatio * (targetWidth + Right);
}
float mBottomBoardY = 0;
if (BottomPivot == VertPivot.Bottom)
{
mBottomBoardY = tBottomBoardPos.y + scaleRatio * Bottom;
}
else if (BottomPivot == VertPivot.Top)
{
mBottomBoardY = tBottomBoardPos.y + scaleRatio * (targetHeight + Bottom);
}
float mTopBoardY = 0;
if (TopPivot == VertPivot.Bottom)
{
mTopBoardY = tBottomBoardPos.y + scaleRatio * Top;
}
else if (TopPivot == VertPivot.Top)
{
mTopBoardY = tBottomBoardPos.y + scaleRatio * (targetHeight + Top);
}
transform.position = new Vector3((mLeftBoardX + mRightBoardX) / 2, (mTopBoardY + mBottomBoardY) / 2, targetPos.z);
UIWidget mWidget = GetComponent<UIWidget>();
if (mWidget)
{
mWidget.width = (int)(Mathf.Abs(mLeftBoardX - mRightBoardX) / scaleRatio);
mWidget.height = (int)(Mathf.Abs(mTopBoardY - mBottomBoardY) / scaleRatio);
}
}
}
使用方法:将SimpleAnchor挂载到需要打锚点的Widget上,为方便填写数值,可以先使用UIWidget自带的Anchor调好数值,再Copy到SimpleAnchor对应项即可,最后别忘了把自带Anchor的Type调整为None。
见下面两个图:
自带Anchor先调整好大小 Copy到相应位置并将自带Anchor设置为None目前还不完善,后续会将Center的情况考虑进去,并写个Editor脚本方便调整相应数值。
网友评论