unity 获取UGUI中的Text字的坐标
using System.Collections;
using UnityEngine;
using UnityEngine.UI; public class TextMoveHelper : MonoBehaviour { public Text textComp;
public Canvas canvas; public Text text; public Vector3 GetPosAtText(Canvas canvas, Text text,string strFragment)
{
int strFragmentIndex = text.text.IndexOf(strFragment);//-1表示不包含strFragment
Vector3 stringPos = Vector3.zero;
if (strFragmentIndex>-)
{
Vector3 firstPos = GetPosAtText(canvas, text, strFragmentIndex + );
Vector3 lastPos= GetPosAtText(canvas, text, strFragmentIndex+strFragment.Length);
stringPos = (firstPos + lastPos) * 0.5f;
}
else
{
stringPos= GetPosAtText(canvas, text, strFragmentIndex);
}
return stringPos;
} /// <summary>
/// 得到Text中字符的位置;canvas:所在的Canvas,text:需要定位的Text,charIndex:Text中的字符位置
/// </summary>
public Vector3 GetPosAtText(Canvas canvas,Text text,int charIndex)
{
string textStr = text.text;
Vector3 charPos = Vector3.zero;
if (charIndex <= textStr.Length && charIndex > )
{
TextGenerator textGen = new TextGenerator(textStr.Length);
Vector2 extents = text.gameObject.GetComponent<RectTransform>().rect.size;
textGen.Populate(textStr, text.GetGenerationSettings(extents)); int newLine = textStr.Substring(, charIndex).Split('\n').Length - ;
int whiteSpace = textStr.Substring(, charIndex).Split(' ').Length - ;
int indexOfTextQuad = (charIndex * ) + (newLine * ) - ;
if (indexOfTextQuad < textGen.vertexCount)
{
charPos = (textGen.verts[indexOfTextQuad].position +
textGen.verts[indexOfTextQuad + ].position +
textGen.verts[indexOfTextQuad + ].position +
textGen.verts[indexOfTextQuad + ].position) / 4f; }
}
charPos /= canvas.scaleFactor;//适应不同分辨率的屏幕
charPos = text.transform.TransformPoint(charPos);//转换为世界坐标
return charPos;
} string a = "跟";
string b = "一起";
string c = "学习";
string d = "学习中文";
int i = ;
void OnGUI()
{
if (GUI.Button(new Rect(, , , ), "Test"))
{
switch (i)
{
case :
StartCoroutine(LerpMove(a));
break;
case :
StartCoroutine(LerpMove(b));
break;
case :
StartCoroutine(LerpMove(c));
break;
case :
StartCoroutine(LerpMove(d));
i = -;
break;
}
i++; }
}
IEnumerator LerpMove(string content)
{
text.text = content;
text.rectTransform.position = GetPosAtText(canvas, textComp, content);
Vector3 endPos = canvas.transform.TransformPoint(Vector3.zero);
yield return new WaitForSeconds(0.3f);
yield return new WaitUntil(() =>{
text.rectTransform.position = Vector3.Lerp(text.rectTransform.position, endPos, Time.deltaTime);
return Vector3.Distance(text.rectTransform.position, endPos) <0.1f; }); } }
转载请标明出处:https://www.cnblogs.com/Jason-c/p/10573567.html
unity 获取UGUI中的Text字的坐标的更多相关文章
- js之获取url中"?"后面的字串
url : index.php?id=123 <script type="text/javascript"> function GetRequest() { var u ...
- unity获取ugui上鼠标位置
public class GetMousePos : MonoBehaviour { public Canvas canvas;//画布 private RectTransform rectTrans ...
- Inno Setup:获取isl中的多国语言字串
原文 http://zwkufo.blog.163.com/blog/static/25882512010101041626803/?suggestedreading&wumii 用InnoS ...
- //获取url中"?"符后的字串
//获取url中"?"符后的字串 function getParamByUrl(url) { var theRequest = new Object(); var in ...
- UGUI中Text的换行
通过代码中的\n可以直接执行换行效果,但是我们在平常的工作中一般都是读表,既在Inspector面板中的Text组件中输入同样的内容就达不到换行效果: 其实unity把\n转变成了\\n,我们只需要变 ...
- 如何获取select中的value、text、index相关值 && 如何获取单选框中radio值 && 触发事件 && radio 默认选中
如何获取select中的value.text.index相关值 select还是比较常用的一个标签,如何获取其中的内容呢? 如下所示: <select id="select" ...
- JS中如何获取<Select>中value和text的值
原文地址:JS中如何获取<Select>中value和text的值 html代码: <select id = "city" onchange="chan ...
- html中select标签获取选中value和text
$("#id").find("option:selected").text(); //获取Select选择的Text$("#id").val ...
- Unity GUI(uGUI)使用心得与性能总结
Unity GUI(uGUI)使用心得与性能总结 作者 kingshijie 关注 2015.09.26 15:35 字数 3686 阅读 28031评论 10喜欢 49 背景和目的 小哈接触Unit ...
随机推荐
- CNN实现terecord、数据集、模型训练
AlexNet(Alex Krizhevsky,ILSVRC2012冠军)适合做图像分类.层自左向右.自上向下读取,关联层分为一组,高度.宽度减小,深度增加.深度增加减少网络计算量. 训练模型数据集 ...
- JavaScript之Ajax技术
- 告诉你 SQL 数据库与 NoSQL 数据库的区别
简单来说 SQL 数据库和 NoSQL 数据库有着共同的目标:存储数据,但存储的方式不同 一. 表 SQL中的表结构具有严格的数据模式约束: 存储数据很难出错. NoSQL存储数据更加灵活自由:可能导 ...
- 【手势交互】6. 微动VID
中国 天津 http://www.sharpnow.com/ 微动VID是天津锋时互动科技有限公司开发的中国Leap Motion. 它能够识别并跟踪用户手部的姿态.包含:指尖和掌心的三维空间位置:手 ...
- ios网络学习------3 用非代理方法实现异步post请求
#pragma mark - 这是私有方法.尽量不要再方法中直接使用属性,由于一般来说属性都是和界面关联的,我们能够通过參数的方式来使用属性 #pragma mark post登录方法 -(void) ...
- 防火墙设置对外开放port
今天在部署项目时,遇到项目组其它人重整了server上的iis.结果外部訪问不了所部属的项目,通过一些渠道找到了设置方法 例如以下报错的截图: 原因是"入站ICMP规则"被重整了, ...
- 数组进行多少次OP操作,才干有序
1 题目描写叙述: 有一个数组:2,1,4,3.对于数组,有一种操作op(idx):将该index相应的数字移到首位.比方: op(3): 2 1 43 -> 3 2 1 4 op(1): ...
- 机器学习 LR中的参数迭代公式推导——极大似然和梯度下降
Logistic本质上是一个基于条件概率的判别模型(DiscriminativeModel). 函数图像为: 通过sigma函数计算出最终结果,以0.5为分界线,最终结果大于0.5则属于正类(类别值为 ...
- DB-MySQL:MySQL NULL 值处理
ylbtech-DB-MySQL:MySQL NULL 值处理 1.返回顶部 1. MySQL NULL 值处理 我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数 ...
- c++面向对象程序设计 谭浩强 第一章答案
c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...