[Unity] Display text when the mouse hovers over the UI interface (implemented with GUI.Box), and the correct usage of Font.GetCharacterInfo

  • Do you guys know what I found when I searched Bing for the keyword "Unity hover text"? Found my own article! ! ! And it's somewhere else! ! ! My blood pressure is really full, the world is going down, ah, the world is going down, copying other people's articles without my consent to increase traffic, I can go to you. I will mark it here first:My blog is published from CSDN, and my articles viewed in any other channel are copyright infringements reproduced without my consent, I don't want to deal with this kind of crap right now, because I'm busy, but it really annoys me, what kind of crap is this, I'm really convinced. I simply hang it out here:

The address of my CSDN article is: [Unity] Display text when the mouse hovers on the UI interface (implemented with GUI.Box), and the correct usage of Font.GetCharacterInfo







  • The following is the original blog content

With regard to the floating text (hint) function, I spent hours doing various searches, maybe my posture is wrong, I shouldn’t use Bing, I should learn from the giants and use Google gayhub and the like.
In order to save everyone's time, I put the key content first, and the content at the back of the blog is just personal complaints and has no effect.
Demonstration effect:
Please add a picture description

using UnityEngine;
using UnityEngine.EventSystems;

public class XJ_UIHint : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler {
    
    
    public Font font;
    public int fontSize = 10;
    [Multiline]//允许多行输入
    public string text = "显示的文本";

    private bool showText = false;
    private GUIStyle style;

    public void Start() {
    
    
        style = new GUIStyle("box");
    }

    public void OnPointerEnter(PointerEventData eventData){
    
    
        showText = true;
    }
    public void OnPointerExit(PointerEventData eventData) {
    
    
        showText = false;
    }
    public void OnGUI() {
    
    
        style.font = font;
        style.fontSize = fontSize;
        var vt =style.CalcSize(new GUIContent(text));
        if (showText)
            GUI.Box(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, vt.x,vt.y), text,style);
    }
}

[There is no title mentioned in the above codeFont.GetCharacterInfo, if you want to know this content, just pull down, and the complaints are below, or Ctrl+F for quick search]

You can add some timers to the above script code according to your personal needs. As for the style of the hint, how should I put it, I also want to change it, but I can't find a suitable method, so I put it on hold. The style of hint is directly linked to GUIStyle. I assign the value "Box" in the script. I won't list other styles. Anyway, it is unnecessary, because I found a GUIStyle style collection written by a giant: Unity View all GUI default styles, it is very simple to use , directly create a script in your own unity project and copy the code of the giant into it (the code of the giant is easy to use and can run without modification), and then view it in that way in the top menu bar of unity tools too.
insert image description here









The following are irrelevant personal complaints



1. Use Font.GetCharacterInfo

   I searched out a lot of rubbish (I won’t list a bunch of junk blogs, after all the useless tags are all closed), here and there are neat, the function closest to my purpose at that time was to useFont.GetCharacterInfoThis function, but because this function keeps sending me invalid data (the value is always 0), it makes my mind blow up. I was stuck for more than two hours to try how to use this evil function, and finally referred to a The blog only solves the problem: How does the Text component in unity get the size of the text in it?

Directly come to a sample code:

	Font font=new Font();//font:字体样式
	string text="123一二三";//text:文本
	int fontSize=24;//fontSize:字体大小
	FontStyle fontStyle=FontStyle.Bold;//fontStyle:字体样式(粗体斜体)

	font.RequestCharactersInTexture(text,fontSize,fontStyle);//第一个参数为渲染的文本,第二个为字体大小,第三个参数为字体样式
	font.GetCharacterInfo('1',out CharacterInfo info,fontSize,fontStyle);//第一个参数为目标字符,第二个为目标字符的尺寸信息,第三个参数为字体大小,第四个参数为字体样式
	//我当时使用的是info.glyphHeight、info.glyphWidth,觉得这两个已经够完成我功能了我就不费事去试别的参数

To sum up, when calling font.RequestCharactersInTexture and font.GetCharacterInfo, if:
    ①, the "target character" of font.GetCharacterInfo is not any character of the string passed in by font.RequestCharactersInTexture;
    ②, the characters passed in by the two functions When the "font size" is inconsistent;
    ③, when the "font style" passed in by the two functions is inconsistent;
then in the returned info, all the data are 0, yes, all are 0, all are 0, 0, 0, At that time, my debugging was really hard. I repeatedly debugged and searched for various things, and it took more than an hour to find out that it was this kind of stupid function call problem. in shortThe calls of these two functions are strictly matched, and the parameter values ​​​​must be exactly the same





2. Use GUIStyle. CalcSize

    At that time, it was a coincidence. I didn’t have any idea to pull down to see what else could be used for this GUIStyle (because the third parameter of GUI.Box is GUIStyle, which sets the style), and then I saw a full blood pressure. The thing, that’s right, is CalcSize . At that time, the inner os said, “It turns out that it has some, and some of it has its own. It can calculate the size of the rendered text, so I don’t want to add character by character and then calculate the width.”
insert image description here
    When I tried this function and found that this function can perfectly replace my first solution (using Font.GetCharacterInfo), I was numb. I did nothing for the first few hours, so I was not angry. Come on, just write a blog and spread some negative emotions

Guess you like

Origin blog.csdn.net/weixin_44733774/article/details/123288589