详情看 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中Button与原生2D精灵的混合使用
一些废话 每一篇的首段都是这个“一些废话”,原因是我太能逼逼了,不逼逼一些废话我就觉得难受.这是我第四篇关于Unity的博文,前两篇还是去年写的,“从一点儿不会开始”系列,类似教程和学习笔记的博文,这 ...
- Unity3D在NGUI中使用mask
过程是这样的:最近一直想做一个头像的mask效果,后来发现原来unity的mask需要用shader来写,网上找了不少资料,也能实现,不过大多数都是用render texture作为相机投影的text ...
- (转)NGUI中深度depth和z轴关系
先列出转载链接: http://game.ceeger.com/forum/read.php?tid=8917 转载原文: 问题源自一个帖子,因为上传的图比较多,就另开了这个贴写下自己的试验结果,原帖 ...
- NGUI制作字体的三种方法
主要参考两篇博文: (1).NGUI制作字体的三种方法 (2).使用位图字体工具BMFont从图片生成自定义字体 1.BMFont下载地址 http://www.angelcode.com/produ ...
- (转)ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
早上同事用PL/SQL连接虚拟机中的Oracle数据库,发现又报了"ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务"错误,帮其解决后,发现很多人遇到过这样的问 ...
- 解析Jquery取得iframe中元素的几种方法
iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作,需要的朋友可以参考下 DOM方法:父窗口操作IFRAME:window.frames[&q ...
- 在html中添加script脚本的方法和注意事项
在html中添加script脚本有两种方法,直接将javascript代码添加到html中与添加外部js文件,这两种方法都比较常用,大家可以根据自己需要自由选择 在html中添加<script& ...
- MVC中使用Entity Framework 基于方法的查询学习笔记 (二)
解释,不解释: 紧接上文,我们在Visual Studio2012中看到系统为我们自动创建的视图(View)文件Index.cshtml中,开头有如下这句话: @model IEnumerable&l ...
- ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法
ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法 (2016-10-21 16:49:53) 今天在做一个查询的时候,报了一个"ORA-01652无法通过 ...
随机推荐
- Annotation方式实现AOP
1.添加其他jar包 2.配置applicationContext.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version=&quo ...
- PHPStorm 使用正则批量查询替换并自动转换大小写的方法
PHPStorm 的项目查询替换功能那是非常非常强大的, 速度也很快, 配合正则更加灵活强大. 一般的正则查询替换没什么太多好说的, 这里主要说说比较少用的 大小写自动转换的问题, 也是比较少用但很有 ...
- yxy-插入formid-并发/压力(入参的变量化)
1.抓取接口如下 2.该接口为插入数据库操作,由于前段操作频繁,就简单压测一下 接口中formId.accountId.openId为变量 formId为随机生成插入:accountId.openId ...
- python中封装
封装 引子 从封装的本身意思去理解,封装就是用一个袋子,把买的水果.书.水杯一起装进袋子里,然后再把袋子的口给封上,照这样的理解来说,封装=隐藏,但是,这种理解是片面的 ## 如何封装 在python ...
- 理解Scala中的Extractor
引言 最近抱着<Programming in Scala>(英文第二版)在死啃Scala.在阅读第26章Extractor时,偶然在Stack Overflow上搜到一个帖子<Sca ...
- 模仿QQ气泡聊天
尝试了几种方案,想模仿QQ的气泡聊天,总是不尽如意.网上倒是大把的Android和Html的例子,Delphi的没找着,只能自己试着折腾. 1. 用WebBrowser加载本地html,屡次折腾,失败 ...
- 【Git】Git的正确学习方式
Git学习笔记 学习资料 git init git status git log 可以查看提交历史 git reset --hard commit_id git reflog查看命令历史 git ad ...
- Hypergraph Models超图模型
最近看了篇Paper(Hyperspectral Image Classification Through Bilayer Graph-Based Learning),里面出现了一个超图(Hyperg ...
- wpf Tree
code using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- BW数据加载
BW数据加载的优先级 1.主数据属性的加载 步骤图 从下到上 1)运行InfoPackage加载到PSA 找到主数据属性的InfoPackage,双击 点击Start按钮 点击监视器,查看运 ...