unity text实现鼠标光标
由于项目需求,需要在text上实现鼠标的cursor,并且随着点击位置cursor移动。实现方法:
1)新建一个光标的prefab(简单为之,直接在image中添加一个竖线spirte),增加脚本控制闪烁。
脚本如下:
public float cycleTime=0.2f;
public Vector2 size =new Vector2(,);
public Color oriColor = new Color(, , , );
public Color newColor = new Color(, , , ); // Use this for initialization
void Start () {
StartCoroutine(Shine());
} private void ShineAction(bool isColor)
{
gameObject.GetComponent<RectTransform>().sizeDelta= size;
gameObject.GetComponent<Image>().color = isColor ? oriColor : newColor;
}
IEnumerator Shine()
{
while (true)
{
ShineAction(false);
yield return new WaitForSeconds(cycleTime);
ShineAction(true);
yield return new WaitForSeconds(cycleTime);
}
}
2)获取text每个字符所在的位置(下代码中width即为每个字符对应的长度,characterInfo.advance为单个字符的长度)
public Text textComp;
Font font = textComp.font;// Resources.Load<Font>("FZCQJW");
int fontsize = textComp.fontSize;
string text = textComp.text;
font.RequestCharactersInTexture(text, fontsize, FontStyle.Normal);
CharacterInfo characterInfo;
float width = 0f;
for (int i = ; i < text.Length; i++)
{
font.GetCharacterInfo(text[i], out characterInfo, fontsize);
//width+=characterInfo.width; unity5.x提示此方法将来要废弃
width += characterInfo.advance;
}
3)获取鼠标位置(获取鼠标位置为全局坐标系下的,需要通过transform.InverseTransformPoint(eventData.position)转化为text局部坐标系下数据),继承接口IPointerDownHandler实现其OnPointerDown方法,即可通过eventData.position获取当前点击位置
public void OnPointerDown(PointerEventData eventData)
{ }
完整代码如下:
public class RowToggleEvent : MonoBehaviour,IPointerDownHandler
{
public Text textComp;
public GameObject cursor; private GameObject newCursor = null; public void OnPointerDown(PointerEventData eventData)
{
if (TransitData.cursor != null) Destroy(TransitData.cursor);
Vector2 pointerDownPos = transform.InverseTransformPoint(eventData.position);
Debug.Log(pointerDownPos);
TransitData.cursor = Instantiate(cursor, gameObject.transform);
float newCursorXpos = GetCursonXpos(pointerDownPos.x-);
Vector2 newCursorPos = new Vector2(newCursorXpos+, -gameObject.GetComponent<RectTransform>().sizeDelta.y / );
TransitData.cursor.GetComponent<RectTransform>().anchoredPosition = newCursorPos;
Debug.Log(newCursorPos);
} private float GetCursonXpos(float pointerDownXpos)
{
float xPos = ;
Font font = textComp.font;// Resources.Load<Font>("FZCQJW");
int fontsize = textComp.fontSize;
string text = textComp.text;
font.RequestCharactersInTexture(text, fontsize, FontStyle.Normal);
CharacterInfo characterInfo;
float width = 0f;
for (int i = ; i < text.Length; i++)
{ font.GetCharacterInfo(text[i], out characterInfo, fontsize);
width += characterInfo.advance;
if (pointerDownXpos <= width)
{
xPos = width;
break;
}
else
{
if (i == text.Length - ) xPos = width;
}
}
return xPos;
}
unity text实现鼠标光标的更多相关文章
- 自定义鼠标光标cursor
通过css属性 Cursor:url()自定义鼠标光标. {cursor:url('图标路径'),default;} url是自定义鼠标图标路径 default指的是定义默认的光标(通常是一个箭头), ...
- HTML中的鼠标光标属性
在网页中默认的鼠标指针只有两种,一种是最普通的箭头,另一种是当移动到链接上时出现的“小手”.但现在越来越多的网页都使用了CSS鼠标指针技术,当将鼠标移动到链接上时,可以看到多种不同的效果.CSS可以通 ...
- 可编辑DIV (contenteditable="true") 在鼠标光标处插入图片或者文字
近期需开发一个DIV做的编辑器,插入表情图片可直接预览效果,仔细参考了下百度贴吧的过滤粘贴过来文件的THML代码,自己整理了下.写出来只是和大家分享下,我自己也不大懂,经过努力,幸好搞定. 蛋疼的事情 ...
- 在.net桌面程序中自定义鼠标光标
有的时候,一个自定义的鼠标光标能给你的程序增色不少.本文这里介绍一下如何在.net桌面程序中自定义鼠标光标.由于.net的桌面程序分为WinForm和WPF两种,这里分别介绍一下. WinForm程序 ...
- 一些 Windows 系统不常见的 鼠标光标常数
一些 Windows 系统不常见的 鼠标光标常数 Private Declare Function SetCursor Lib "user32" (ByVal hCursor A ...
- WPF 自定义鼠标光标
在程序中使用自定义鼠标光标的三种方式: RadioButton senderButton = sender as RadioButton; 方式一: str ...
- WPF窗体隐藏鼠标光标的方法
原文:WPF窗体隐藏鼠标光标的方法 要引用 System.Windows.Input; Mouse.OverrideCursor = Cursors.None; 去掉 Override 则使用: ...
- C# 设置鼠标光标位置
C# 设置鼠标光标位置 using System.Drawing; using System.Runtime.InteropServices; namespace ZB.QueueSys.Common ...
- Windows 远程桌面鼠标光标不可见
一.问题描述 通过在云端的主机上部署 frp 服务,实现「使用Windows 远程桌面(RDP)从互联网侧访问内网的主机」.但是,使用 Windows 自带的远程桌面工具 RDP 连接到另一台计算机时 ...
随机推荐
- Java源码跟踪阅读技巧
转:https://www.jianshu.com/p/ab865109070c 本文基于Eclipse IDE 1.Quick Type Hierarchy 快速查看类继承体系. 快捷键:Ctrl ...
- FIT文件CRC校验
校验FIT文件CRC代码做个记录,分为两步先校验头部然后再校验整个FIT文件.校验头部不是必需的看个人需要吧.为了偷懒使用Okio库,还有计算CRC的时候用的Garmin的FitSDK. public ...
- HBase学习与实践
Photo by bealach verse on Unsplash 参考书籍:<HBase 权威指南> -- Lars George著. 文章为个人从零开始学习记录,如有错误,还请不吝赐 ...
- vue- Vue-Cli脚手架工具安装 -创建项目-页面开发流程-组件生命周期- -03
目录 Vue-Cli 项目环境搭建 与 python 基础环境对比 环境搭建 创建启动 vue 项目 命令创建项目(步骤小多) 启动 vue 项目(命令行方式) 启动 vue 项目(pycharm 方 ...
- 【SQLServer】 查询一个字段里不同值的最新一条记录
查询用户编号为1165的所有数据: ,,,,,) ' order by JianCeID desc 查询用户编号为1165且监测参数为(1,2,7,15,19,20)的最新数据: select * f ...
- Spring 源码阅读 二
程序入口: 接着上一篇博客中看完了在AnnotationConfigApplicationContext的构造函数中的register(annotatedClasses);将我们传递进来的主配置类添加 ...
- Spring5源码解析5-ConfigurationClassPostProcessor (上)
接上回,我们讲到了refresh()方法中的invokeBeanFactoryPostProcessors(beanFactory)方法主要在执行BeanFactoryPostProcessor和其子 ...
- 【源码解析】自动配置的这些细节不知道,别说你会 springboot
spring-boot 相对于 spring,很重要的一个特点就是自动配置,使约定大于配置思想成功落地.xxx-spring-boot-starter 一系列引导器能够开箱即用,或者只需要很少的配置( ...
- 1.InfluxDB-官方测试数据导入
本文翻译自官网,官方文档地址:https://docs.influxdata.com/influxdb/v1.7/query_language/data_download/ 1.下载官网示例数据 命令 ...
- 6.InfluxDB-InfluxQL基础语法教程--GROUP BY子句
本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/) GROUP BY子句 ...