当前位置:网站首页>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
边栏推荐
猜你喜欢

Application analysis of face recognition

C#/VB.NET 删除Word文檔中的水印

Pioneer of Web3: virtual human

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

Detailed explanation of line segment tree (including tested code implementation)

Data connection mode in low code platform (Part 1)
![[unity notes] screen coordinates to ugui coordinates](/img/e4/fc18dd9b4b0e36ec3e278e5fb3fd23.jpg)
[unity notes] screen coordinates to ugui coordinates

【论文阅读|深读】RolNE: Improving the Quality of Network Embedding with Structural Role Proximity

1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动

Lumion 11.0软件安装包下载及安装教程
随机推荐
Stm32f4 --- PWM output
Real project, realized by wechat applet opening code (end)
MySQL
Common fitting models and application methods of PCL
安全巡检的工作
leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
C#/VB.NET 删除Word文档中的水印
Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
一文读懂Faster RCNN
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
unity webgl自适应网页尺寸
【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
Why am I warned that the 'CMAKE_ TOOLCHAIN_ FILE' variable is not used by the project?
6-6漏洞利用-SSH安全防御
导数、偏导数、方向导数
leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
Lombok makes the pit of ⽤ @data and @builder at the same time
Web3的先锋兵:虚拟人
Argo workflows source code analysis