Tuesday, August 16, 2022
HomeGame Developmentc# - How you can get personal information from Unity class by...

c# – How you can 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 might be created for instance with:

utilizing UnityEngine.UIElements;

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

(To make use of this you have to go Sport Object > UI Doc, then proper click on in property, create clean UI Doc, 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 inside as this space continues to be topic to vary and we’re not prepared to show it publicly. In case you want the information you can entry it via reflection, and it’s situated 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.Occasion)) { //provides uitkTextHandle
            Debug.Log("PROPERTIES " + propertyInfo.Identify);
        }

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

And whereas I can get the uitkTextHandle this manner, 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.Occasion).GetValue(testLabel, null);

Am I doing one thing incorrect maybe, or may the Unity Admin have given me the incorrect info? That is my first time utilizing Reflection so I’m not certain.

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

Thanks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments