当前位置:网站首页>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 给用户看的文字
边栏推荐
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- MFC Windows 程序设计[147]之ODBC数据库连接(附源码)
- Lidar: introduction and usage of ouster OS
- ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
- [C # notes] use file stream to copy files
- [paper reading | deep reading] graphsage:inductive representation learning on large graphs
- 张平安:加快云上数字创新,共建产业智慧生态
- FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting
- Robot team learning method to achieve 8.8 times human return
- This week's hot open source project!
猜你喜欢
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
How can reinforcement learning be used in medical imaging? A review of Emory University's latest "reinforcement learning medical image analysis", which expounds the latest RL medical image analysis co
Douban average 9 x. Five God books in the distributed field!
Time synchronization of livox lidar hardware -- PPS method
FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting
【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
豆瓣平均 9.x,分布式领域的 5 本神书!
Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
Word wrap when flex exceeds width
随机推荐
真实项目,用微信小程序开门编码实现(完结)
[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]
[unity notes] screen coordinates to ugui coordinates
解密函数计算异步任务能力之「任务的状态及生命周期管理」
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
Use of fiddler
pgpool-II和pgpoolAdmin的使用
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
老板被隔离了
Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
Redis configuration class redisconfig
The mega version model of dall-e MINI has been released and is open for download
How can reinforcement learning be used in medical imaging? A review of Emory University's latest "reinforcement learning medical image analysis", which expounds the latest RL medical image analysis co
Data connection mode in low code platform (Part 1)
Stm32f4 --- PWM output
How do I dump SoapClient requests for debugging- How to dump SoapClient request for debug?
Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption
Pgadmin4 of PostgreSQL graphical interface tool
强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向