详情看 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. 自定义控件-滑动条SeekBar

    一.效果图 二.实现思路 1.控件继承自View 2.重写两个方法onDraw() 绘制页面和 onTouch() 添加监听 3.onDraw(Canvas ca)中     a.new Canvas ...

  2. linux安装jdk1.6

    本来打算安装jdk1.8的 从官网下载来的jdk1.8的tar.gz的jar包. 使用tar命令解压,复制到指定文件夹后,配置完环境变量后一直报错,什么CGLIB2.4的.查询得到结果好像是linux ...

  3. 2017北京赛区H题

    题目链接 题意:在n*m的矩阵中选择变换或者不变换一个数变成p,使得最大子矩阵和最小 1<=n,m<=150, -1000<=p<=1000; 题解: 他人题解链接 涉及到知识 ...

  4. 使用lets encrypt证书加密

    1    git clone https://github.com/letsencrypt/letsencrypt 2     ./letsencrypt-auto certonly -d 域名

  5. PHPStorm 添加支持 PSR-4 命名空间前缀设置

    许久没有更新博客啦, 太忙了, 七月这最后一天来写点自己在使用 PHPStorm 上的小却很有用的功能吧. PHPStorm 默认是使用 PSR-0 命名空间规范的, 前提是你需要标记好项目中的源码根 ...

  6. 大牛deep learning入门教程

    雷锋网(搜索"雷锋网"公众号关注)按:本文由Zouxy责编,全面介绍了深度学习的发展历史及其在各个领域的应用,并解释了深度学习的基本思想,深度与浅度学习的区别和深度学习与神经网络之 ...

  7. 4. 什么是应用服务器? - JavaEE基础系列

    本文是JavaEE基础系列的第四节. Java EE简介 - JavaEE基础系列 JSR简介 - JavaEE基础系列 什么是JSR参考实现? - JavaEE基础系列 上一节介绍了什么是JSR参考 ...

  8. iOS开发CocoaPods使用

    一.为什么用CocoaPods iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三 ...

  9. Goroutine是如何工作的?

    翻译原文链接 转帖/转载请注明出处英文原文链接 发表于2014/02/24 Go语言 如果你刚刚接触Go语言,或者说你并不理解“并发不等于并行”这句话的含义,那么Rob Pike的讲座值得一看(在yo ...

  10. Encode Adjacent Letters

    Encode a string by counting the consecutive letter. (i.e., "aaaabbxxxyyz" might become &qu ...