当前位置:网站首页>The panel floating with the mouse in unity can adapt to the size of text content
The panel floating with the mouse in unity can adapt to the size of text content
2022-07-07 02:35:00 【Wu Zimu】
We often use the function of floating panel , For example, the mouse moves to an object , Display the name of the object and other information
To this end, I made a panel that can adapt the content size according to the text content
The structure of the panel is as follows
There are three objects ,InfoText The name can't be the rest can
object 1: RoomInfoPanel Consists of four key components , The explanation is as follows
1 Text Used to adapt the size according to the text size , This text is transparent , Don't show users , And the font size is larger than that really used for display InfoText A little bigger
2 FloatInfoPanel, Used to set the displayed text , The code is as follows , It can be modified as needed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Floating panel for room information
/// </summary>
public class FloatInfoPanel : MonoBehaviour
{
/// <summary>
/// Self Text Components , This Text It's completely transparent , Used to control the adaptive panel size
/// </summary>
private Text textSelf;
/// <summary>
/// Text box for displaying information
/// </summary>
private Text infoText;
/// <summary>
/// Set the surveillance camera information displayed in suspension
/// </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 The panel moves with the mouse , Just hang it up , Surrogate mothers are as follows
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// The panel floats with the mouse
/// </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 Adaptive size
We need to pay attention to , Anchor points should be point shaped , Put it in the lower left corner , The pivot point can prevent , It is recommended to keep to the left , Or lower left
object 2 BG, Panel background , Four sides and RoomInfoPanel alignment
object 3 InfoText Text for users
边栏推荐
- 6-6 vulnerability exploitation SSH security defense
- CSDN summer camp course project analysis
- The boss is quarantined
- [leetcode]Search for a Range
- 你不可不知道的Selenium 8种元素定位方法,简单且实用
- Web3's need for law
- MySQL
- Lombok同时使⽤@Data和@Builder 的坑
- Linear list --- circular linked list
- Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
猜你喜欢
数论 --- 快速幂、快速幂求逆元
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
Compress JS code with terser
一文读懂Faster RCNN
dotConnect for DB2数据提供者
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
This week's hot open source project!
KYSL 海康摄像头 8247 h9 isapi测试
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
随机推荐
Draco - glTF模型压缩利器
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
【Node学习笔记】chokidar模块实现文件监听
[leetcode]Search for a Range
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
fiddler的使用
Common fitting models and application methods of PCL
How to build a 32core raspberry pie cluster from 0 to 1
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
人脸识别应用解析
Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
[unity notes] screen coordinates to ugui coordinates
[server data recovery] data recovery case of a Dell server crash caused by raid damage
如何从0到1构建32Core树莓派集群
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity
Untiy文本框的代码换行问题
Yyds dry goods inventory # solve the real problem of famous enterprises: maximum difference
Stm32f4 --- PWM output
Lidar: introduction and usage of ouster OS
The cities research center of New York University recruits master of science and postdoctoral students