【效果】
用于解决文本显示地方有限的问题,当光标移到该文本控件上,额外悬浮显示全部文本内容。
【解决方案】
思路:额外增加一个通用的带背景内容显示面板,通过光标的停留来选定需要特殊显示的对象,然后控制显示和赋值内容面板,内容面板背景可随文字数量变大变小。
步骤:
1、增加一个Image对象,以及增加一个Text作为其子对象。
2、给Text对象添加ContentSizeFitter组件。用于设置水平或垂直内容的自动调整
3、给Image对象也添加ContentSizeFitter组件,并增加VerticalLayoutGroup组件用于自动调节背景的大小。
4、然后就是给需要特别显示内容的文本对象 写一个控制内容显示面板的脚本,后面其他有需要的对象,只需要将这个脚本直接绑上去就可以了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ZoomInShow : MonoBehaviour
{
//当前文本对象
private RectTransform rect;
private Text rect_txt;
//额外的内容显示面板(带背景)
private Transform objImage;
private Text content;
//用于update里单次进出
private bool isEnter;
void Awake()
{
rect = transform.GetComponent();
rect_txt = transform.GetComponent();
objImage = transform.Find("/Canvas/ObjImage");
content = objImage.GetChild(0).GetComponent();
}
void Update()
{
//判断鼠标是否悬浮在该UI上
if (RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition) && !isEnter)
{
print("进入");
isEnter = true;
//显示内容面板,设置面板位置,赋值文本内容
objImage.gameObject.SetActive(true);
objImage.localPosition = rect.localPosition;
content.text = rect_txt.text;
//更新背景大小
LayoutRebuilder.ForceRebuildLayoutImmediate(content.rectTransform);
LayoutRebuilder.ForceRebuildLayoutImmediate(content.transform.parent.transform as RectTransform);
}
if (RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition) == false && isEnter == true)
{
print("移出");
isEnter = false;
objImage.gameObject.SetActive(false);
}
}
}
===================以下为2月16日更新===================
【发现问题】
1、当绑定多个对象使用时会出现找不到objImage对象的情况,
原因:是因为第一个脚本将objImage对象给隐藏掉了,后面执行Awake()去查找的时候就会找不到。
解决方法:给objImage对象加一个空父对象,该父对象一直显示,同时用getChild(0)的形式去获取objImage对象,那么这样就不管objImage对象是否隐藏都可以找到了。
2、多层级UI下,内容面板显示的位置对不上。
原因:内容面板和鼠标指向对象都使用了本地位置作为相互赋值,当不在同级UI下且UI矩阵不一样时,使用本地位置是对不上滴。
解决方法:用鼠标位置来更新内容面板的世界坐标位置。
3、增加一个鼠标悬上后延时显示内容面板,这样不至于鼠标随便划过都显示了。
新脚本更新如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ZoomInShow : MonoBehaviour
{
//当前文本对象
private RectTransform rect;
private Text rect_txt;
//额外的内容显示面板(带背景)
private Transform contentPanel;
private Transform objImage;
private Text content;
//用于update里单次进出
private bool isEnter;
void Awake()
{
rect = transform.GetComponent();
rect_txt = transform.GetComponent();
contentPanel = transform.Find("/Canvas/ContentPanel");
objImage = contentPanel.GetChild(0);
content = objImage.GetChild(0).GetComponent();
objImage.gameObject.SetActive(false);
}
void Update()
{
//判断鼠标是否悬浮在该UI上
if (RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition) && !isEnter)
{
print("进入");
isEnter = true;
//光标停留延时显示内容面板
Invoke("ShowContentPanel", 0.3f);
}
if (RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition) == false && isEnter == true)
{
print("移出");
isEnter = false;
objImage.gameObject.SetActive(false);
}
}
///
/// 显示内容面板
///
void ShowContentPanel()
{
if (RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition))
{
//显示内容面板,设置面板位置,赋值文本内容
objImage.gameObject.SetActive(true);
//objImage.localPosition = rect.localPosition;
objImage.position = Input.mousePosition;
content.text = rect_txt.text;
//更新背景大小
LayoutRebuilder.ForceRebuildLayoutImmediate(content.rectTransform);
LayoutRebuilder.ForceRebuildLayoutImmediate(content.transform.parent.transform as RectTransform);
}
}
}



