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;
}
}

NGUI 源码分析- AnchorPoint的更多相关文章

  1. NGUI 源码分析- UIWidgetInspector

    NGUI Version 3.9.0 //---------------------------------------------- // NGUI: Next-Gen UI kit // Copy ...

  2. ABP源码分析一:整体项目结构及目录

    ABP是一套非常优秀的web应用程序架构,适合用来搭建集中式架构的web应用程序. 整个Abp的Infrastructure是以Abp这个package为核心模块(core)+15个模块(module ...

  3. HashMap与TreeMap源码分析

    1. 引言     在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...

  4. nginx源码分析之网络初始化

    nginx作为一个高性能的HTTP服务器,网络的处理是其核心,了解网络的初始化有助于加深对nginx网络处理的了解,本文主要通过nginx的源代码来分析其网络初始化. 从配置文件中读取初始化信息 与网 ...

  5. zookeeper源码分析之五服务端(集群leader)处理请求流程

    leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...

  6. zookeeper源码分析之四服务端(单机)处理请求流程

    上文: zookeeper源码分析之一服务端启动过程 中,我们介绍了zookeeper服务器的启动过程,其中单机是ZookeeperServer启动,集群使用QuorumPeer启动,那么这次我们分析 ...

  7. zookeeper源码分析之三客户端发送请求流程

    znode 可以被监控,包括这个目录节点中存储的数据的修改,子节点目录的变化等,一旦变化可以通知设置监控的客户端,这个功能是zookeeper对于应用最重要的特性,通过这个特性可以实现的功能包括配置的 ...

  8. java使用websocket,并且获取HttpSession,源码分析

    转载请在页首注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/6238826.html 一:本文使用范围 此文不仅仅局限于spring boot,普通的sprin ...

  9. ABP源码分析二:ABP中配置的注册和初始化

    一般来说,ASP.NET Web应用程序的第一个执行的方法是Global.asax下定义的Start方法.执行这个方法前HttpApplication 实例必须存在,也就是说其构造函数的执行必然是完成 ...

随机推荐

  1. ubuntu 1806 添加 kali 源

    最近需要使用 kali 源安装一些软件: 配置 sources.list,根据如下链接:http://mirrors.ustc.edu.cn/help/kali.html 获取 公钥:apt-key ...

  2. Flink中的CEP复杂事件处理 (源码分析)

    其实CEP复杂事件处理,简单来说你可以用通过类似正则表达式的方式去表示你的逻辑,表现能力非常的强,用过的人都知道 开篇先偷一张图,整体了解Flink中的CEP中的  一种重要的图  NFA非确定有限状 ...

  3. 纯CSS背景噪声.html

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 带着canvas去流浪系列之三 绘制饼图

    [摘要] 用canvas原生API绘制Echarts图表 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvasAPI绘制 ...

  5. three.js各种材质的实现源码

    three.js常用材质:基本材质.兰伯特材质.冯氏材质.标准材质. 我们可以自己使用着色器实现这些材质,用于批量渲染等用途. 为了简单,假设物体只有一张漫反射贴图,场景中只存在一个环境光和一个平行光 ...

  6. [TimLinux] Python 类型与运算

    1. 内建(built-in)数据类型种类 数字类型:int(), float() 顺序(sequence): 字符串:str() 元祖:tuple() 列表:list() 字典:dict() 集合: ...

  7. hdu2546

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  8. Python3 函数基础2

    目录 可变长参数 可变长形参: *args 可变长实参: *容器类 可变长形参: **kwargs 可变长实参: **字典 函数对象 引用 当做容器类型元素 当做参数传给一个函数 当做函数的返回值 函 ...

  9. IP地址的配置

    1.右击 网上邻居 → 属性 2.右击 本地连接 → 属性 3.选择Internet协议(TCP/IP) → 属性 配置如下, 默认网关始终是网段的第一个地址 4.打开cmd → 输入 ipconfi ...

  10. 通过EF操作Sqlite时遇到的问题及解决方法

    1.使用Guid作为字段类型时,能存,能查,但是作为查询条件时查询不到数据 解决方法:连接字符串加上;binaryguid=False