详情看 http://note.youdao.com/noteshare?id=ec901d56341207052b2d19233b5ddba3

这里仅仅贴出文字,完整内容请看上面链接。

有这样一个需求:让LabelA停靠在LabelB的某个位置,同时要适应labelA的变化的长度。
 
1. LabelA和LabelB的OverFlow方式是ResizeFreely,使Label的尺寸能够变化。
2. 根据公式得到labelB的位置:
labelB.anchoredposition
= labelA.anchoredposition - labelA.pivot*labelA.size (此时指向labelA的左下角位置)
+ harborAnchor*labelA.size (harborAnchor是归一化的位置值,(1,1)labelA的右上角)
+ AbsoluteOffset(绝对偏移)
3. 需要设置labelB的pivot,例如如果想要让labelB如上图所示停靠在labelA的底线上且是右侧且有一个绝对偏移值(10,0),则将labelB的widget的pivot设置为左下。将harborAnchor设置为(1,0)。
 
▼代码开始
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
public class UILabelDock: MonoBehaviour
{
    public const string Specification = "将某UI停靠在某个Label(称为HarborLabel)的HarborAnchor处,这两个UI需要在同一个GameObject下";
 
    public UILabel m_HarborLabel = null;
 
    [Tooltip("设置港口Widget的锚点,港口Widget内容区域的左下角是(0,0),右上角是(1,1)")]
    public Vector2 m_HarborAnchor = new Vector2(1, 0.5f);
 
    RectTransform m_MyRectTransform = null;
    RectTransform m_HarborTransform = null;
    UIWidget m_HarborWidget = null;
 
    [Tooltip("绝对偏移值")]
    public Vector2 m_AbsoluteOffset = new Vector2(0, 0);
 
    private void Awake()
    {
        m_MyRectTransform = GetComponent<RectTransform>();
        m_HarborTransform = m_HarborLabel.GetComponent<RectTransform>();
        m_HarborWidget = m_HarborLabel.GetComponent<UIWidget>();
    }
   
    public void RefreshDock()
    {
        Awake();
        if (m_HarborLabel == null || m_MyRectTransform == null || m_HarborWidget == null || m_HarborTransform)
        {
            Debug.LogError("LabelDock Error");
            return;
        }
 
        //需要确保
        m_MyRectTransform.anchorMax = m_HarborTransform.anchorMax;
        m_MyRectTransform.anchorMin = m_HarborTransform.anchorMin;
        m_MyRectTransform.offsetMax = m_HarborTransform.offsetMax;
        m_MyRectTransform.offsetMin = m_HarborTransform.offsetMin;
        m_MyRectTransform.pivot = m_HarborTransform.pivot;
        m_MyRectTransform.sizeDelta = m_HarborTransform.sizeDelta;
 
        var normalizedOffset = new Vector2(0, 0);
        switch (m_HarborWidget.pivot)
        {
            case UIWidget.Pivot.Bottom:
                {
                    normalizedOffset = new Vector2(0.5f, 0);
                    break;
                }
            case UIWidget.Pivot.BottomLeft:
                {
                    normalizedOffset = new Vector2(0, 0);
                    break;
                }
            case UIWidget.Pivot.BottomRight:
                {
                    normalizedOffset = new Vector2(1, 0);
                    break;
                }
            case UIWidget.Pivot.Center:
                {
                    normalizedOffset = new Vector2(.5f, .5f);
                    break;
                }
            case UIWidget.Pivot.Left:
                {
                    normalizedOffset = new Vector2(0, .5f);
                    break;
                }
            case UIWidget.Pivot.Right:
                {
                    normalizedOffset = new Vector2(1, .5f);
                    break;
                }
            case UIWidget.Pivot.Top:
                {
                    normalizedOffset = new Vector2(.5f, 1);
                    break;
                }
            case UIWidget.Pivot.TopLeft:
                {
                    normalizedOffset = new Vector2(0, 1);
                    break;
                }
            case UIWidget.Pivot.TopRight:
                {
                    normalizedOffset = new Vector2(1, 1);
                    break;
                }
        }
 
        normalizedOffset = m_HarborAnchor - normalizedOffset;
        m_MyRectTransform.anchoredPosition = m_HarborTransform.anchoredPosition + new Vector2(normalizedOffset.x * m_HarborLabel.printedSize.x, normalizedOffset.y * m_HarborLabel.printedSize.y) + m_AbsoluteOffset;
    }
}
 
 
▲代码结束
 

NGUI中LabelA停靠LabelB的方法的更多相关文章

  1. NGUI中Button与原生2D精灵的混合使用

    一些废话 每一篇的首段都是这个“一些废话”,原因是我太能逼逼了,不逼逼一些废话我就觉得难受.这是我第四篇关于Unity的博文,前两篇还是去年写的,“从一点儿不会开始”系列,类似教程和学习笔记的博文,这 ...

  2. Unity3D在NGUI中使用mask

    过程是这样的:最近一直想做一个头像的mask效果,后来发现原来unity的mask需要用shader来写,网上找了不少资料,也能实现,不过大多数都是用render texture作为相机投影的text ...

  3. (转)NGUI中深度depth和z轴关系

    先列出转载链接: http://game.ceeger.com/forum/read.php?tid=8917 转载原文: 问题源自一个帖子,因为上传的图比较多,就另开了这个贴写下自己的试验结果,原帖 ...

  4. NGUI制作字体的三种方法

    主要参考两篇博文: (1).NGUI制作字体的三种方法 (2).使用位图字体工具BMFont从图片生成自定义字体 1.BMFont下载地址 http://www.angelcode.com/produ ...

  5. (转)ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法

    早上同事用PL/SQL连接虚拟机中的Oracle数据库,发现又报了"ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务"错误,帮其解决后,发现很多人遇到过这样的问 ...

  6. 解析Jquery取得iframe中元素的几种方法

    iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作,需要的朋友可以参考下   DOM方法:父窗口操作IFRAME:window.frames[&q ...

  7. 在html中添加script脚本的方法和注意事项

    在html中添加script脚本有两种方法,直接将javascript代码添加到html中与添加外部js文件,这两种方法都比较常用,大家可以根据自己需要自由选择 在html中添加<script& ...

  8. MVC中使用Entity Framework 基于方法的查询学习笔记 (二)

    解释,不解释: 紧接上文,我们在Visual Studio2012中看到系统为我们自动创建的视图(View)文件Index.cshtml中,开头有如下这句话: @model IEnumerable&l ...

  9. ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法

    ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法 (2016-10-21 16:49:53)   今天在做一个查询的时候,报了一个"ORA-01652无法通过 ...

随机推荐

  1. 2018.11.15 RF antenna impedance-matching

    We have studied the impedance-matching of RF transmission line between the antenna and the RX / TX m ...

  2. css移动元素的几种方法

    一.当然是元素设定为postion: absolute, 然后控制 left, top 位置 二.元素增加overflow属性,然后设置元素的scrollLeft, scrollRight当做滚动条来 ...

  3. Openlayers3中实现地图的切割

    概述: 本文讲述如何在Openlayers3中结合canvas实现对地图的切割. 效果: 全图 切割北京区域 切割河北区域 实现: <html xmlns="http://www.w3 ...

  4. Redis的高可用技术方案

    引言: redis是广为使用的缓存解决方案,本文将给出基于Sequential的高可用方案,自动进行主从的切换,在master节点down机之后,透明的进行切换. 主从节点的设置方案 设置主节点red ...

  5. 移动端 css 禁止长按屏幕选中

    *{ -webkit-touch-callout:none; -webkit-user-select:none; -khtml-user-select:none; -moz-user-select:n ...

  6. php实现彩票走势图组选图用颜色区分

    找了好久都没有关于这个的东西,我也是一步一步从百度知道上问出思路来的 $xxx = $row['bai'] ; $yyy = $row['shi'] ; $zzz = $row['ge'] ; $zu ...

  7. C++中的友元函数的总结

    1.友元函数的简单介绍 1.1为什么要使用友元函数 在实现类之间数据共享时,减少系统开销,提高效率.如果类A中的函数要访问类B中的成员(例如:智能指针类的实现),那么类A中该函数要是类B的友元函数.具 ...

  8. 值类型struct在foreach中的陷阱

    最近踩了一个坑,为了优化代码,把class改为了struct,结果发现原来的初始化语句没有预期的运行,伪代码如下: public struct A { bool _isActive; public v ...

  9. js 值和引用

    js对值和引用的赋值/传递在语法上没有区别,完全根据值得类型决定 简单值(即标量基本类型值),总是通过值复制的方式来赋值/传递,包括null,undefined,字符串,数字,布尔值和ES6中的sym ...

  10. POJ2891 Strange Way to Express Integers【扩展中国剩余定理】

    题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...