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 连接到另一台计算机时 ...
随机推荐
- CF #579 (Div. 3) A.Circle of Students
A. Circle of Students time limit per test2 seconds memory limit per test256 megabytes inputstandard ...
- spring bean的生命周期与springmvc的生命周期
配置在Spring中的Bean在Spring容器中从加载到销毁会经历那些过程呢?如果实现一些特定的Spring接口,这些特定接口的方法会在什么时候被调用呢?本文简单介绍一下这些过程. Bean在Spr ...
- Flask基础(14)-->自定义过滤器
Flask基础(13)-->自定义过滤器 什么是过滤器? 过滤器的本质就是函数.有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化.运算等等,而在模板中是不能直接调用 P ...
- [一]基本sqlplus命令
基本sqlplus命令: 1: sqlplus scott/tiger ; #简化连接数据库 2:show user; #想知道当前登陆的用户是哪一位 3:conn 用户名[/密码] [AS SYSD ...
- LeetCode 题解汇总
前言 现如今,对于技术人员(软开.算法等)求职过程中笔试都是必不可少的(免笔试的除外,大部分人都需要笔试),而笔试一般组成都是选择.填空.简答题.编程题(这部分很重要),所以刷题是必不可少的:对于应届 ...
- dede tag标签静态化
看回那2个文件夹即可,txt说明书我已经修改过. 下面说一下tag标签静态化之后在内容页.列表页中如何使用. 内容页中沿用之前的方法即可: {dede:tag sort='new' getall='0 ...
- redis 漏洞造成服务器被入侵-CPU飙升
前言 前几天在自己服务器上搭了redis,准备想着大展身手一番,昨天使用redis-cli命令的时候,10s后,显示进程已杀死.然后又试了几次,都是一样的结果,10s时间,进程被杀死.这个时候我还 ...
- Angular2+之使用FormGroup、FormBuilder和Validators对象控制表单(取值、赋值、校验和是否可编辑等)
1.要使用Angular自带的表单控制需要先引入相关模块(.ts文件): import { FormGroup, //表单对象类 FormBuilder, //表单生成工具类 Validators} ...
- ping本地局域网
#!/bin/bash for i in `seq 1 254` do ping -c 1 192.168.192.$i > /dev/null if [ $? -eq 0 ];then ech ...
- 基于动态代理的WebAPI/RPC/webSocket框架,一套接口定义,多个通讯方式
API/RPC/webSocket三个看起来好像没啥相同的地方,在开发时,服务端,客户端实现代码也大不一样 最近整理了一下,通过动态代理的形式,整合了这些开发,都通过统一的接口约束,服务端实现和客户端 ...