当前位置:网站首页>unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
2022-07-06 18:43:00 【吴梓穆】
我们经常会用到浮动面板的功能,比如鼠标移动到某个物体上,显示这个物体的名字等信息
为此我制作了一个可以根据文本内容自适应内容大小的面板
该面板的结构如下
一共有三个物体,InfoText名字不能该其余可以
物体1: RoomInfoPanel由四个关键组件,说明如下
1 Text 用于根据文字大小自适应尺寸,这个文本是透明的,不给用户看,且字号要比真正用于显示的InfoText稍大
2 FloatInfoPanel,用于设置显示的文字,代码如下,可根据需要修改
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
///房间信息的浮动面板
/// </summary>
public class FloatInfoPanel : MonoBehaviour
{
/// <summary>
/// 自身的Text组件,这个Text是完全透明的,用于控制自适应面板大小
/// </summary>
private Text textSelf;
/// <summary>
/// 显示信息用的文本框
/// </summary>
private Text infoText;
/// <summary>
/// 设置悬浮显示的监控摄像头信息
/// </summary>
public void SetRoomName(string roomName)
{
if (textSelf == null)
{
textSelf = transform.GetComponent<Text>();
infoText = transform.Find("InfoText").GetComponent<Text>();
}
textSelf.text = GameController.Instance.currentRoom_float;
infoText.text = GameController.Instance.currentRoom_float;
}
}
3 FloatWithMouse_Panel 面板跟随鼠标移动,挂上即可,代母如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 面板跟随鼠标浮动
/// </summary>
public class FloatWithMouse_Panel : MonoBehaviour
{
public float xoffset;
public float yoffset;
public float zoffset;
private void Update()
{
print(Input.mousePosition);
transform.position = Input.mousePosition + new Vector3(xoffset,yoffset,zoffset);
}
}
4ContentSizeFitter 自适应尺寸
需要注意,锚点要为点状,放在左下角,轴点可根据需要防止,建议靠左,或左下
物体2 BG,面板背景,四边和RoomInfoPanel对齐
物体3 InfoText 给用户看的文字
边栏推荐
- SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!
- [server data recovery] data recovery case of a Dell server crash caused by raid damage
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- Summer Challenge database Xueba notes (Part 2)~
- 企业中台建设新路径——低代码平台
- Why am I warned that the 'CMAKE_ TOOLCHAIN_ FILE' variable is not used by the project?
- 3D laser slam: time synchronization of livox lidar hardware
- Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
- Seconds understand the delay and timing function of wechat applet
- How do I dump SoapClient requests for debugging- How to dump SoapClient request for debug?
猜你喜欢
FLIR blackfly s usb3 industrial camera: white balance setting method
Stm32f4 --- general timer update interrupt
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting
张平安:加快云上数字创新,共建产业智慧生态
Integerset of PostgreSQL
Argo workflows source code analysis
How to build a 32core raspberry pie cluster from 0 to 1
FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
postgresql之整體查詢大致過程
随机推荐
新一代云原生消息队列(一)
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
使用Ceres进行slam必须要弄清楚的几个类和函数
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity
How to build a 32core raspberry pie cluster from 0 to 1
强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
【论文阅读|深读】RolNE: Improving the Quality of Network Embedding with Structural Role Proximity
The mega version model of dall-e MINI has been released and is open for download
A new path for enterprise mid Platform Construction -- low code platform
低代码平台中的数据连接方式(上)
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
[server data recovery] data recovery case of a Dell server crash caused by raid damage
String or binary data will be truncated
企业中台建设新路径——低代码平台
What to do when encountering slow SQL? (next)
真实项目,用微信小程序开门编码实现(完结)