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;
}
}
NGUI 源码分析- AnchorPoint的更多相关文章
- NGUI 源码分析- UIWidgetInspector
NGUI Version 3.9.0 //---------------------------------------------- // NGUI: Next-Gen UI kit // Copy ...
- ABP源码分析一:整体项目结构及目录
ABP是一套非常优秀的web应用程序架构,适合用来搭建集中式架构的web应用程序. 整个Abp的Infrastructure是以Abp这个package为核心模块(core)+15个模块(module ...
- HashMap与TreeMap源码分析
1. 引言 在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...
- nginx源码分析之网络初始化
nginx作为一个高性能的HTTP服务器,网络的处理是其核心,了解网络的初始化有助于加深对nginx网络处理的了解,本文主要通过nginx的源代码来分析其网络初始化. 从配置文件中读取初始化信息 与网 ...
- zookeeper源码分析之五服务端(集群leader)处理请求流程
leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...
- zookeeper源码分析之四服务端(单机)处理请求流程
上文: zookeeper源码分析之一服务端启动过程 中,我们介绍了zookeeper服务器的启动过程,其中单机是ZookeeperServer启动,集群使用QuorumPeer启动,那么这次我们分析 ...
- zookeeper源码分析之三客户端发送请求流程
znode 可以被监控,包括这个目录节点中存储的数据的修改,子节点目录的变化等,一旦变化可以通知设置监控的客户端,这个功能是zookeeper对于应用最重要的特性,通过这个特性可以实现的功能包括配置的 ...
- java使用websocket,并且获取HttpSession,源码分析
转载请在页首注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/6238826.html 一:本文使用范围 此文不仅仅局限于spring boot,普通的sprin ...
- ABP源码分析二:ABP中配置的注册和初始化
一般来说,ASP.NET Web应用程序的第一个执行的方法是Global.asax下定义的Start方法.执行这个方法前HttpApplication 实例必须存在,也就是说其构造函数的执行必然是完成 ...
随机推荐
- Kotlin协程通信机制: Channel
Coroutines Channels Java中的多线程通信, 总会涉及到共享状态(shared mutable state)的读写, 有同步, 死锁等问题要处理. 协程中的Channel用于协程间 ...
- php: $$str
这种写法称为可变变量有时候使用可变变量名是很方便的.就是说,一个变量的变量名可以动态的设置和使用.一个普通的变量通过声明来设置,例如: <?php$a = "hello";? ...
- RDS关系型数据库 入门 01 创建关系型数据库实例【华为云分享】
[摘要] 关系型数据库(Relational Database Service,简称RDS)是一种基于云计算平台的即开即用.稳定可靠.弹性伸缩.便捷管理的在线关系型数据库服务.RDS具有完善的性能监控 ...
- 王晶:华为云OCR文字识别服务技术实践、底层框架及应用场景 | AI ProCon 2019
演讲嘉宾 | 王晶(华为云人工智能高级算法工程师王晶) 出品 | AI科技大本营(ID:rgznai100) 近期,由 CSDN 主办的 2019 中国AI 开发者大会(AI ProCon 2019) ...
- 你真的了解JMM吗?
引言 在现代计算机中,cpu的指令速度远超内存的存取速度,由于计算机的存储设备与处理器的运算速度有几个数量级的差距,所以现代计算机系统都不得不加入一层读写速度尽可能接近处理器运算速度的高速缓存(Cac ...
- JavaScript的DOM对象和jQuery对象的对比
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 简单http和https服务器python脚本
欢迎加入python学习交流群 667279387 工作经常要用到测试http和https协议,这里写了两个简单的脚本实现简单的http服务器和https服务器. http服务器代码 import s ...
- Spring Cloud第七篇 | 声明式服务调用Feign
本文是Spring Cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...
- 基于iCamera测试模拟摄像头-TVP5150模块小结
基于iCamera测试模拟摄像头-TVP5150模块小结 首先先认识下模拟摄像头,相信大家都不陌生, CCD模拟摄像头 CMOS模拟摄像头 可以看到,ccd摄像头电路较复杂,接口少,而cmos摄像头, ...
- PYTHON5000:CMOS图象传感器演示
http://www.onsemi.cn/PowerSolutions/supportVideo.do?docId=1002896