alignToScreen

来源:互联网 发布:巨人网络街篮手游下载 编辑:程序博客网 时间:2024/06/01 19:56
    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;        }    }

0 0