当前位置:网站首页>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);
}
}
}
边栏推荐
- Under what conditions does the Z-index attribute expire?
- 力扣292周赛题解
- Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
- Does the tester need to analyze the cause of the bug?
- Force buckle 704 Binary search
- Win10 vs2015 compiling curaengine
- Chapter 7 vertex processing and drawing commands of OpenGL super classic (7th Edition)
- Unity3d learning notes-1 (C # learning)
- Redis cluster concept
- How to install win7 on AMD Ruilong CPU A320 series motherboard
猜你喜欢
随机推荐
QT connecting external libraries
pycharm 数据库工具
Network communication problem locating steps
Unity3d packaging and publishing APK process
Pytorch的安装以及入门使用
力扣27. 移除元素
力扣209. 长度最小的子数组
PWN Introduction (2) stack overflow Foundation
JPA复合主键使用
Unity multiple UI page turning left and right
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
LxC and LXD container summary
Nestjs配置静态资源,模板引擎以及Post示例
力扣2049:统计最高分的节点数目
Chapter 11 advanced data management of OpenGL super classic (version 7)
Untiy3d controls scene screenshots through external JSON files
Procedural animation -- inverse kinematics of tentacles
Chapter 9 of OpenGL super classic (version 7): fragment processing and frame buffering
Unity project hosting platform plasticscm (learn to use 1)
【VCS+Verdi聯合仿真】~ 以計數器為例