NGUI 源碼分析- AnchorPoint

AnchorPoint 是 UIRect 的一個內部類,此處規定做爲基準的那個對象稱爲錨點對象,基準對象對應的矩形框稱爲目標框,當前對象對應的矩形框稱爲源框ide

public class AnchorPoint  
{
    public Transform target;        // 錨點對象
    public float relative = 0f;     // 相對位置:用來肯定是相對於目標框的哪一個點,0、0.五、1分別對應目標框的下中上或者是左中右點。
    public int absolute = 0;        // 距離,是個整數:源點與目標點的距離(例如AnchorPoint是topAnchor,那麼就是兩點在y軸方向上的距離)。

    [System.NonSerialized]          // 不容許序列化 也不顯示在檢視器中。// 關於序列化能夠參考 Vector3 中的使用
    public UIRect rect;             // 錨點對象的 rect

    [System.NonSerialized]
    public Camera targetCam;        // 錨點對象的Camera

    public AnchorPoint() { }
    public AnchorPoint(float relative) { this.relative = relative; }

    /// <summary>
    /// Convenience function that sets the anchor's values.
    /// </summary>

    public void Set(float relative, float absolute)
    {
        this.relative = relative;
        this.absolute = Mathf.FloorToInt(absolute + 0.5f); // 看起來是四捨五入
    }

    /// <summary>
    /// Convenience function that sets the anchor's values.
    /// </summary>

    public void Set(Transform target, float relative, float absolute)
    {
        this.target = target;
        this.relative = relative;
        this.absolute = Mathf.FloorToInt(absolute + 0.5f);
    }

    /// <summary>
    /// Set the anchor's value to the nearest of the 3 possible choices of (left, center, right) or (bottom, center, top).
    /// 傳入源點到三個可能目標點的距離,將最近的點做爲目標點。要按左中右或者下中上的順序依次輸入。
    /// </summary>

    public void SetToNearest(float abs0, float abs1, float abs2) { SetToNearest(0f, 0.5f, 1f, abs0, abs1, abs2); }

    /// <summary>
    /// Set the anchor's value given the 3 possible anchor combinations. Chooses the one with the smallest absolute offset.
    /// </summary>

    public void SetToNearest(float rel0, float rel1, float rel2, float abs0, float abs1, float abs2)
    {
        float a0 = Mathf.Abs(abs0);
        float a1 = Mathf.Abs(abs1);
        float a2 = Mathf.Abs(abs2);

        if (a0 < a1 && a0 < a2) Set(rel0, abs0);
        else if (a1 < a0 && a1 < a2) Set(rel1, abs1);
        else Set(rel2, abs2);
    }

    /// <summary>
    /// Set the anchor's absolute coordinate relative to the specified parent, keeping the relative setting intact.
    /// 目標點不變的狀況下設置源點和目標點的距離,至關因而個更新距離的操做。
    /// </summary>

    public void SetHorizontal(Transform parent, float localPos)
    {
        if (rect)
        {
            Vector3[] sides = rect.GetSides(parent); // 獲取成相對座標下的邊
            float targetPos = Mathf.Lerp(sides[0].x, sides[2].x, relative);
            absolute = Mathf.FloorToInt(localPos - targetPos + 0.5f); 
        }
        else
        {
            Vector3 targetPos = target.position;
            if (parent != null) targetPos = parent.InverseTransformPoint(targetPos);
            absolute = Mathf.FloorToInt(localPos - targetPos.x + 0.5f);
        }
    }

    /// <summary>
    /// Set the anchor's absolute coordinate relative to the specified parent, keeping the relative setting intact.
    /// </summary>

    public void SetVertical(Transform parent, float localPos)
    {
        if (rect)
        {
            Vector3[] sides = rect.GetSides(parent);
            float targetPos = Mathf.Lerp(sides[3].y, sides[1].y, relative);
            absolute = Mathf.FloorToInt(localPos - targetPos + 0.5f);
        }
        else
        {
            Vector3 targetPos = target.position;
            if (parent != null) targetPos = parent.InverseTransformPoint(targetPos);
            absolute = Mathf.FloorToInt(localPos - targetPos.y + 0.5f);
        }
    }

    /// <summary>
    /// Convenience function that returns the sides the anchored point is anchored to.
    /// 獲取目標框的四條邊
    /// </summary>

    public Vector3[] GetSides(Transform relativeTo)
    {
        if (target != null)
        {
            if (rect != null) return rect.GetSides(relativeTo);
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
            if (target.camera != null) return target.camera.GetSides(relativeTo);
#else
            if (target.GetComponent<Camera>() != null) return target.GetComponent<Camera>().GetSides(relativeTo);
#endif
        }
        return null;
    }
}
相關文章
相關標籤/搜索