当前位置:网站首页>Unity ugui text value suspended enlarged display add text background
Unity ugui text value suspended enlarged display add text background
2022-06-30 05:08:00 【zjh_ three hundred and sixty-eight】
【 effect 】
Used to solve the problem of limited text display space , When the cursor moves over the text control , Extra hover displays all text content .

【 Solution 】
Ideas : Add a general content display panel with background , Select the objects that need special display by hovering the cursor , Then control the display and assignment content panel , The background of the content panel can be reduced as the number of words increases .
step :
1、 Add one more Image object , And add a Text As its child .

2、 to Text Object to add ContentSizeFitter Components . Used to set the automatic adjustment of horizontal or vertical content

3、 to Image Objects are also added ContentSizeFitter Components , And increase VerticalLayoutGroup Component is used to automatically adjust the size of the background .

4、 Then it is to give text objects that need special display content Write a script to control the content display panel , There are other objects in need later , Just bind the script directly .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ZoomInShow : MonoBehaviour
{
// Current text object
private RectTransform rect;
private Text rect_txt;
// Additional content display panels ( With background )
private Transform objImage;
private Text content;
// be used for update Single entry and exit
private bool isEnter;
void Awake()
{
rect = transform.GetComponent<RectTransform>();
rect_txt = transform.GetComponent<Text>();
objImage = transform.Find("/Canvas/ObjImage");
content = objImage.GetChild(0).GetComponent<Text>();
}
void Update()
{
// Determine whether the mouse hovers over the UI On
if (RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition) && !isEnter)
{
print(" Get into ");
isEnter = true;
// Cursor dwell delay display content panel
Invoke("ShowContentPanel", 0.2f);
}
if (RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition) == false && isEnter == true)
{
print(" Removed from the ");
isEnter = false;
objImage.gameObject.SetActive(false);
}
}
/// <summary>
/// Show content panel
/// </summary>
void ShowContentPanel()
{
if (RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition))
{
// Show content panel , Set panel position , Assignment text content
objImage.gameObject.SetActive(true);
objImage.localPosition = rect.localPosition;
content.text = rect_txt.text;
// Update background size
LayoutRebuilder.ForceRebuildLayoutImmediate(content.rectTransform);
LayoutRebuilder.ForceRebuildLayoutImmediate(content.transform.parent.transform as RectTransform);
}
}
}
边栏推荐
- 003-JS-DOM-Attr-innerText
- 力扣349. 两个数组的交集
- Force buckle 349 Intersection of two arrays
- GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;
- Generate a slice of mesh Foundation
- Ripple effect of mouse click (unity & shader)
- 力扣589:N 叉树的前序遍历
- Writing unityshader with sublimetext
- 力扣209. 长度最小的子数组
- Database base (Study & review for self use)
猜你喜欢

Oculus quest2 development: (I) basic environment construction and guide package

Unity3d packaging and publishing APK process
![[vcs+verdi joint simulation] ~ take the counter as an example](/img/fb/214a4e65c53503ecbc38a5e43523cf.png)
[vcs+verdi joint simulation] ~ take the counter as an example

一条命令运行rancher

Ugui uses its own function to realize reverse mask

Unreal 4 learning notes - set player birth point

Unity lens making

PWN入门(2)栈溢出基础

力扣589:N 叉树的前序遍历

Unity + hololens common basic functions
随机推荐
Unity scroll view element drag and drop to automatically adsorb centering and card effect
Modbus protocol register
一条命令运行rancher
Pit of smoothstep node in shadergraph
How does unity use mapbox to implement real maps in games?
力扣589:N 叉树的前序遍历
0 basic unity course. Bricklaying
Easyrecovery data recovery software recovers my photo and video data two years ago
力扣292周赛题解
Steamvr causes abnormal scene camera
Unity supports the platform # define instruction of script
Li Kou 2049: count the number of nodes with the highest score
Unity3d realizes Google Digital Earth
Sourcetree usage
Nestjs introduction and environment construction
Does the tester need to analyze the cause of the bug?
Chinese pycharm changed to English pycharm
【 VCS + Verdi joint simulation】 ~ Taking Counter as an Example
Unity script life cycle and execution sequence
力扣59. 螺旋矩阵 II