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

最新文章

  1. PHP环境配置
  2. Mongodb profile(慢查询日志)
  3. 浅析C语言指针问题
  4. Java IO4:字符编码
  5. VS2015上又一必备免费插件:Refactoring Essentials
  6. XML工程配置文件的读写
  7. [fedora21]给fedora21安装fcitx输入法
  8. OkHttp 源码分析
  9. JFinal的Shiro权限管理插件--玛雅牛 / JFinalShiro
  10. IE6下不能定义1px高度的容器和IE6 双边距
  11. ASP.NET使用ajax实现分页局部刷新页面
  12. 【Java基础】【16List集合】
  13. Python中的短路计算
  14. 内置函数-max、min、round、sorted、ord、chr、any、all、dir、eval、exec、map、filter、reduce
  15. DRF 视图和路由
  16. 洛谷P3354 Riv河流 [IOI2005] 树型dp
  17. @Value(&quot;#{}&quot;)与@Value(&quot;${}&quot;)的区别以及用法
  18. php代码覆盖率执行
  19. JVM知识(五):垃圾回收算法
  20. 封装简单的mvc框架

热门文章

  1. 通过 Python 理解 Mixin 概念
  2. 【Android - 控件】之MD - TextInputLayout的使用
  3. php弹出确认框
  4. Android Activity生命周期及启动模式
  5. 2019-2020-1 20199304《Linux内核原理与分析》第三周作业
  6. nginx、redis在Centos7中,加入开机自启动
  7. 【读一本书】《昇腾AI处理器架构与编程》--神经网络基础知识(2)
  8. 工作流Activity组件值数据传递获取问题
  9. 趁热来一波,WWDC 2016 iMessage App开发
  10. Zookeeper Watcher接口