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. 玩家福音:10款最佳Linux免费游戏

    “我能在Linux平台上游戏吗?”这类疑问正困扰游戏玩家,那么答案就是“快去Linux平台吧!”.开源组织一直以来坚持不懈为Linux操作系统开发不同类型的游戏,在Linux平台下的游戏完全不亚于其他 ...

  2. Python学习笔记(2)操作符和数据类型

    2019-02-25 一: (1)常用操作符: ① 算数操作符:=.-.*./.%(求余).**(幂运算).//(地板除法:计算结果取比商小的最大整型) 注意:幂运算操作符比其左侧的一元运算符的优先级 ...

  3. A - Jungle Roads

    A - Jungle Roads 思路:并查集的板子,重点是字符的转换,不能忘了加上1. #include<cmath> #include<cstdio> #include&l ...

  4. [SQL]存储过程建表

    create PROC [dbo].CreateUserTable ( @name NVARCHAR(60) ) AS DECLARE @a NVARCHAR(max) SET @a='create ...

  5. iframe显示滚动栏

    子页面通过iframe载入.出现了竖向滚动栏 最后查出原因:文档申明 iframe有滚动栏的页面的文档申明 <!DOCTYPE html> 改成例如以下即可了 <!DOCTYPE H ...

  6. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  7. 【PLSQL】触发器trigger类型,状态,參数

    ************************************************************************   ****原文:blog.csdn.net/clar ...

  8. 积跬步,聚小流------java信息生成图片

    需求: 是在做证书的时候碰到的这个问题. 当时需求是能够进行在线打印证书,第一次进行的操作是直接打印html,并且已经排好版(用jqprint插件)进行打印.在打印时碰到了兼容的问题,另外因为背景图片 ...

  9. Android持久化保存cookie

    在解析网页信息的时候,需要登录后才能访问,所以使用httpclient模拟登录,然后把cookie保存下来,以供下一次访问使用,这时就需要持久化cookie中的内容. 在之前先科普一下基础知识: 什么 ...

  10. iOS~判断应用是否有定位权限

    在特定场景下我们需要判断用户是否允许应用获取定位权限 1.导入类库: #import <CoreLocation/CLLocationManager.h> 2.判断用户手机是否开启了定位服 ...