[Unity] the UI interface displays text when the mouse hovers, and font Correct usage of getcharacterinfo

Posted by thumrith on Fri, 04 Mar 2022 23:02:28 +0100

For this hint function, I did all kinds of searches for a few hours. Maybe my posture is wrong. I shouldn't use Bing. I should learn from giant to use Google gayhub and so on.
In order to save everyone's time, I put the key content in front. The content behind the blog is no use for personal complaints anyway.
Demonstration effect:

using UnityEngine;
using UnityEngine.EventSystems;

public class XJ_UIHint : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler {
    public Font font;
    public int fontSize = 10;
    public string text = "Displayed 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 Font.GetCharacterInfo mentioned in the title in the above code. If you want to know this content, please pull down and complain below, or Ctrl+F to find it quickly]

You can add some timers to the above script code according to your personal needs. As for the hint style, how to say, I also want to change it, but I can't find a suitable method, so I put it aside. Hint's style is directly linked to GUIStyle. I assign the value "Box" in my script, and I won't list other styles. Anyway, it's not necessary, because I found a complete collection of GUIStyle styles written by a giant: Unity view all GUI default styles , it's easy to use. Directly create a script in your own unit project and copy in the giant's code (the giant's code is easy to use and can run without modification), and then there is the style viewing tool in the top menu bar of unit.

The following are unimportant personal complaints

1, Use font GetCharacterInfo

I found a lot of garbage (I won't list and put a pile of garbage blogs. After all, the useless labels are closed). It's a whole thing here and there. At that time, the function closest to my purpose was to use font Getcharacterinfo is a function, but because this function always sends me invalid data (the value is always 0), my mentality is blown. I stuck for more than two hours to try how to use this Tiansha function. Finally, I referred to a blog to solve the problem: How does the Text component in unity get the size of the Text

Just a sample code:

	Font font=new Font();//Font: font style
	string text="123 one two three";//text: text
	int fontSize=24;//fontSize: font size
	FontStyle fontStyle=FontStyle.Bold;//fontStyle: font style (bold italic)

	font.RequestCharactersInTexture(text,fontSize,fontStyle);//The first parameter is the rendered text, the second is the font size, and the third parameter is the font style
	font.GetCharacterInfo('1',out CharacterInfo info,fontSize,fontStyle);//The first parameter is the target character, the second is the size information of the target character, the third parameter is the font size, and the fourth parameter is the font style
	//I used info glyphHeight,info.glyphWidth, I think these two are enough to complete my functions, so I don't bother to try other parameters

To summarize, after calling font Requestcharactersintexture and font When getcharacterinfo, if:
    ①,font.GetCharacterInfo's target character is not font Requestcharactersintexture any character of the string passed in;
②. When the "font size" passed in by the two functions is inconsistent;
③ when the "font style" passed in by the two functions is inconsistent;
In the returned info, all the data is 0. Yes, it's all 0. It's all 0, 0, 0. At that time, I was really hard to debug. I debugged and searched all kinds of things again and again. It took me more than an hour to find this stupid function call problem. In short, the calls of the two functions are strictly matched, and the parameter values should be exactly the same

2, Use guistyle CalcSize

At that time, it was a miracle. I didn't have any idea to pull down to see what else could be used in this GUIStyle (because the third parameter of GUI.Box is GUIStyle, which sets the style), and then I saw a thing with full blood pressure, yes, CaleSize
When I tried this function and made it clear that this function can perfectly replace my first scheme (using Font.GetCharacterInfo), I was numb. I did nothing for the past few hours, so I couldn't get angry, so I wrote a blog and spread some negative emotions

Topics: Unity Visual Studio