• NGUI 源码分析- AnchorPoint


    AnchorPoint 是 UIRect 的一个内部类,此处规定作为基准的那个对象称为锚点对象,基准对象对应的矩形框称为目标框,当前对象对应的矩形框称为源框

    
    public class AnchorPoint  
    {
        public Transform target;        // 锚点对象
        public float relative = 0f;     // 相对位置:用来确定是相对于目标框的哪个点,0、0.5、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;
        }
    }
    
    
  • 相关阅读:
    nyoj 420
    nyoj 46 最少乘法次数
    ACM退役贴
    nyoj 187 快速查找素数
    多校4题目之Trouble
    nyoj 56 阶乘因式分解(一)
    nyoj 70 阶乘因式分解(二)
    nyoj 151 Biorhythms
    nyoj 97 兄弟郊游问题
    多校十 hdoj4393 Throw nails
  • 原文地址:https://www.cnblogs.com/tangyikejun/p/4824487.html
Copyright © 2020-2023  润新知