public enum EAlign { TopLeft, Top, TopRight, Left, Center, Right, BottomLeft, Bottom, BottomRight }
static void alignToScreen(EAlign side, Camera uiCamera, Transform trans, Vector2 pixelOffset = default(Vector2), Vector2 relativeOffset = default(Vector2))
{
var rect = uiCamera.pixelRect;
var cx = (rect.xMin + rect.xMax) * 0.5f;
var cy = (rect.yMin + rect.yMax) * 0.5f;
var v = new Vector3(cx, cy, 0f);
if (side != EAlign.Center)
{
switch (side)
{
case EAlign.TopRight:
case EAlign.Right:
case EAlign.BottomRight:
v.x = rect.xMax;
break;
case EAlign.Top:
case EAlign.Center:
case EAlign.Bottom:
v.x = cx;
break;
default:
v.x = rect.xMin;
break;
}
switch (side)
{
case EAlign.TopLeft:
case EAlign.Top:
case EAlign.TopRight:
v.y = rect.yMax;
break;
case EAlign.Left:
case EAlign.Center:
case EAlign.Right:
v.y = cy;
break;
default:
v.y = rect.yMin;
break;
}
}
v.x += pixelOffset.x + relativeOffset.x * rect.width;
v.y += pixelOffset.y + relativeOffset.y * rect.height;
if (uiCamera.orthographic)
{
v.x = Mathf.Round(v.x);
v.y = Mathf.Round(v.y);
}
v.z = uiCamera.WorldToScreenPoint(trans.position).z;
v = uiCamera.ScreenToWorldPoint(v);
if (uiCamera.orthographic && trans.parent != default(Transform))
{
v = trans.parent.InverseTransformPoint(v);
v.x = Mathf.RoundToInt(v.x);
v.y = Mathf.RoundToInt(v.y);
if (trans.localPosition != v) trans.localPosition = v;
}
else if (trans.position != v)
{
trans.position = v;
}
}
alignToScreen
最新推荐文章于 2022-04-13 12:08:00 发布