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字的坐标的更多相关文章

  1. js之获取url中"?"后面的字串

    url : index.php?id=123 <script type="text/javascript"> function GetRequest() { var u ...

  2. unity获取ugui上鼠标位置

    public class GetMousePos : MonoBehaviour { public Canvas canvas;//画布 private RectTransform rectTrans ...

  3. Inno Setup:获取isl中的多国语言字串

    原文 http://zwkufo.blog.163.com/blog/static/25882512010101041626803/?suggestedreading&wumii 用InnoS ...

  4. //获取url中"?"符后的字串

    //获取url中"?"符后的字串 function getParamByUrl(url) {    var theRequest = new Object();    var in ...

  5. UGUI中Text的换行

    通过代码中的\n可以直接执行换行效果,但是我们在平常的工作中一般都是读表,既在Inspector面板中的Text组件中输入同样的内容就达不到换行效果: 其实unity把\n转变成了\\n,我们只需要变 ...

  6. 如何获取select中的value、text、index相关值 && 如何获取单选框中radio值 && 触发事件 && radio 默认选中

    如何获取select中的value.text.index相关值 select还是比较常用的一个标签,如何获取其中的内容呢? 如下所示: <select id="select" ...

  7. JS中如何获取<Select>中value和text的值

    原文地址:JS中如何获取<Select>中value和text的值 html代码: <select id = "city" onchange="chan ...

  8. html中select标签获取选中value和text

    $("#id").find("option:selected").text(); //获取Select选择的Text$("#id").val ...

  9. Unity GUI(uGUI)使用心得与性能总结

    Unity GUI(uGUI)使用心得与性能总结 作者 kingshijie 关注 2015.09.26 15:35 字数 3686 阅读 28031评论 10喜欢 49 背景和目的 小哈接触Unit ...

随机推荐

  1. acgis地图初始化并根据经纬度进行标注

    根据项目要求,需要对指定的位置进行标注. 1.初始化地图服务 2.根据地图服务接口信息进行标注 3.根据经纬度信息进行标注 展示接口截图: 以下代码并不能直接粘贴跑起来,因为代码所涉及的文件地址都是内 ...

  2. hdu 2435 dinic算法模板+最小割性质

    #include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...

  3. 今天又犯了Java/Scala里面substring的错误

    每次都误以为是 substring(startIndex, length) 其实是 substring(startIndex, endIndex) 嗯 Java/Scala 跟 C++ 是不一样的.

  4. Android ViewGroup拦截触摸事件具体解释

    前言 在自己定义ViewGroup中.有时候须要实现触摸事件拦截.比方ListView下拉刷新就是典型的触摸事件拦截的样例. 触摸事件拦截就是在触摸事件被parent view拦截,而不会分发给其ch ...

  5. 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)

    题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...

  6. linux块设备的IO调度算法和回写机制

    ************************************************************************************** 參考: <Linux ...

  7. 《学习opencv》笔记——基本数据结构,CvMat,矩阵訪问

        老板让让做一个东东.输入端要用到opencv顺便就来学习一下.买了本书<学习opencv>翻来一看,opencv1.0,去官网上一看.opencv2.49,瞬间有种蛋碎的赶脚.看着 ...

  8. UVA - 1471 Defense Lines 树状数组/二分

                                  Defense Lines After the last war devastated your country, you - as the ...

  9. java 中的静态(static)代码块

    类字面常量 final 静态域不会触发类的初始化操作 非 final static 静态域(以及构造器其实是一种隐式的静态方法) Class.forName():会自动的初始化: 使用 .class来 ...

  10. 转:centos下升级git版本的操作记录

    https://www.cnblogs.com/kevingrace/p/8252517.html 在使用git pull.git push.git clone的时候,或者在使用jenkins发版的时 ...