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 ...
随机推荐
- RE:ゼロから始める文化課生活
觉得有必要在NOI之前开一篇学习内容记录. 至于为什么要取这个标题呢?也许并没有什么特殊的借口吧. 5.23 在LOJ上搬了三道原题给大家考了考,然后大家都在考试就我一个人在划水. SSerxhs 和 ...
- PHP学习总结(2)——PHP入门篇之PHP代码标识
认识PHP代码标识 想在页面中编写PHP代码非常容易,如下面代码: <?php echo "想学习php吗?来慕课网吧";?> 就像你可以编写JavaScript脚本 ...
- Global UNIX file system cylinder group cache
A global cylinder group (CG) cache is stored in file server memory and shared by a plurality of file ...
- Weblogic常用监控指标
http://blog.csdn.net/konglongaa/article/details/53897562
- Linux下ffmpeg的wav与amr相互转换
转载:http://blog.csdn.net/sanshipianyezi/article/details/78742621 转载:http://blog.csdn.net/szfhy/articl ...
- 【java项目实战】代理模式(Proxy Pattern),静态代理 VS 动态代理
这篇博文,我们主要以类图和代码的形式来对照学习一下静态代理和动态代理.重点解析各自的优缺点. 定义 代理模式(Proxy Pattern)是对象的结构型模式,代理模式给某一个对象提供了一个代理对象,并 ...
- hdu 4603 Color the Tree
这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...
- IOS写一个能够支持全屏的WebView
这样来写布局 一个TitleView作为顶部搜索栏: @implementation TitleView - (id)initWithFrame:(CGRect)frame { self = [sup ...
- USACO 2.2 Preface Numbering
Preface Numbering A certain book's prefaces are numbered in upper case Roman numerals. Traditional R ...
- ubuntu中不能远程连接解决
今天装好ubuntu19.04之后不能远程连接,网上找了很久终于自己解决了.ap 步骤如下:希望对各位有用,哪里不对请指出 第一步我们需要加载openssh-server 等待加载完毕后, ...