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. Java基础面试题及答案(三)

    多线程 35. 并行和并发有什么区别? 并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生. 并行是在不同实体上的多个事件,并发是在同一实体上的多个事件. 在一台处理器 ...

  2. Batch Normalization详解

    目录 动机 单层视角 多层视角 什么是Batch Normalization Batch Normalization的反向传播 Batch Normalization的预测阶段 Batch Norma ...

  3. Socket模拟SSH

    Socket模拟SSH 主要思路 客户端发送相关命令到服务端,服务端执行命令(通过subprocess模块实现)然后将结果返回给客户端 小知识 Linux中可以发送空数据,服务端能接受到空数据 win ...

  4. phpStudy搭建PHP服务器

    目录 1 下载 2 安装 3 新建站点 4 配置host phpStudy是一个PHP调试环境的程序集成包. 该程序包集成最新的 Apache+Nginx+LightTPD PHP MySQL+php ...

  5. Python网络爬虫——BeautifulSoup4库的使用

    使用requests库获取html页面并将其转换成字符串之后,需要进一步解析html页面格式,提取有用信息. BeautifulSoup4库,也被成为bs4库(后皆采用简写)用于解析和处理html和x ...

  6. Android View 的绘制流程之 Layout 和 Draw 过程详解 (二)

    View 的绘制系列文章: Android View 的绘制流程之 Measure 过程详解 (一) Android View 绘制流程之 DecorView 与 ViewRootImpl 在上一篇  ...

  7. css实现对勾

    <!DOCTYPE html><html> <head> <meta charset=%;background-color: #2ac845;} %;back ...

  8. Netty实现丢弃服务协议(Netty4.X学习一)

    何为丢弃服务(Discard Protocol),丢弃服务就是一个协议,是最简单的协议,它的作用是接受到什么就丢弃什么,它对调试网路状态有一定的用处.基于TCP的丢弃服务,服务器实现了丢弃丢弃协议,服 ...

  9. MySQL必知必会(Select)

    SELECT prod_name FROM products; SELECT prod_id, prod_name, prod_price FROM products; SELECT * FROM p ...

  10. rbac结合ssm实现权限分配和管理

    RBAC(Role-Based Access Control )基于角色的访问控制. RBAC 认为权限的过程可以抽象概括为: 判断[Who 是否可以对 What 进行 How 的访问操作(Opera ...