Tuesday, August 16, 2022
HomeGame Developmentc# - How to get personal information from Unity class by reflection?

c# – How to get personal information from Unity class by reflection?

I’m making an attempt to entry the per letter vertices array within the UIElements (UI Toolkit) Label Unity class.

A Label of this kind could be created for instance with:

utilizing UnityEngine.UIElements;

Label label = new Label();
label.textual content = "This is a check";
label.type.unityFontDefinition = FontDefinition.FromFont(Resources.Load<Font>("font-path"));
label.type.fontSize = 40;
VisualElement rootVE = recreationObject.GetComponent<UIDocument>().rootVisualElement;

(To use this you could go Game Object > UI Document, then proper click on in belongings, create clean UI Document, and drag it onto recreation object’s “supply asset discipline” with this script.)

I’m making an attempt to get the per letter vertices of a Label of this nature. I used to be instructed by a Unity developer:

“The vertices of letters are at present inner as this space continues to be topic to vary and we’re not prepared to reveal it publicly. If you want the information you possibly can entry it by means of reflection, and it’s positioned right here (as of 2022.2) : m_Label.uitkTextHandle.textInfo.”

I can get uitkTextHandle however cannot discover textInfo inside it. I’ve tried:

foreach (var propertyInfo in typeof(Label).GetProperties(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)) { //provides uitkTextHandle
            Debug.Log("PROPERTIES " + propertyInfo.Name);

foreach (var propertyInfo in (typeof(Label).GetProperty("uitkTextHandle", System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Instance)).GetType().GetMembers(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)) { //NO TEXTINFO FOUND
            Debug.Log("PROPERTIES " + propertyInfo.Name);

And whereas I can get the uitkTextHandle this fashion, I can not solid it to entry textInfo inside it (as it’s a protected class) and I in any other case cannot discover any textInfo inside it.

        var uitkTextHandle = typeof(Label).GetProperty("uitkTextHandle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(checkLabel, null);

Am I doing one thing flawed maybe, or would possibly the Unity Admin have given me the flawed data? This is my first time utilizing Reflection so I’m not positive.

How would I get the vertices array that they are saying I ought to be capable of this fashion in a format I can use? What am I nonetheless lacking?




Please enter your comment!
Please enter your name here

Most Popular

Recent Comments