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. C# List源码分析(一)

    事件原因,之前在公司写代码的时候,带我的师傅建议我对List的长度最好在初始化的时候进行优化,这样对GC更加友好,所以就有了这个文章,来理解下List 容量自适应的实现. List 继承于IList, ...

  2. OOP 面向对象 七大原则 (一)

    OOP 面向对象   七大原则 (一) 大家众所周知,面向对象有三大特征继承封装多态的同时,还具有这七大原则,三大特征上一篇已经详细说明,这一篇就为大家详解一下七大原则: 单一职责原则,开闭原则,里氏 ...

  3. 1、认识和安装MongoDB

    MongoDB简介:MongoDB是一个基于分布式文件存储的数据库,由C++语言编写.目的是为WEB应用提供扩展的高性能的数据存储解决方案.MongoDB是一个介于关系型数据库和非关系型数据库之间的产 ...

  4. redis 参数配置总结

    redis.conf 配置项说明如下 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程   daemonize no 2. 当Redis以守护进程方式运行时, ...

  5. hdu 2491 贪心

    #include<stdio.h> #include<stdlib.h> #define N 110000 struct node { int u,v,len,time; }m ...

  6. jquery---- 数组根据值进行删除

    var chooseAttr = []; //serviceId为值 chooseAttr.splice($.inArray(serviceId,chooseAttr),);

  7. 【转】C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素

    有时候,我们要对图层上的地物进行有选择性的显示,以此来满足实际的功能要求. 按下面介绍的方法可轻松实现图层属性过滤显示: 1.当图层已经加载时 private void ShowByFilter(Ax ...

  8. mac下安装配置java开发环境

    可以使用homebrew下载相关软件,以下具体讲一下环境的配置: mac下安装jdk vi .bash_profile 输入i,进入编辑模式 输入以下配置,其中JAVA_HOME是你的jdk安装目录 ...

  9. 《Python 源码剖析》之对象

    py一切皆对象的实现 Python中对象分为两类: 定长(int等), 非定长(list/dict等) 所有对象都有一些相同的东西, 源码中定义为PyObject和PyVarObject, 两个定义都 ...

  10. poi读取合并单元格

    poi读取合并单元格 学习了:http://blog.csdn.net/ycb1689/article/details/9764191 进行了列合并单元格的修正:原来是我自己找错了地方: import ...