Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reflection to display custom types on the Inspector of Unity

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use reflection to display custom types on Unity's Inspector. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Recently, to debug and migrate FixedB2CSharp in Unity, the entire library is based on fixed-point numbers, so first of all, you need to serialize the fixed-point numbers and display them on Inspector. Unexpectedly, you stepped into the pit.

Generally speaking, in order to achieve this requirement, I can just use PropertyDrawer and change it according to Unity's API manual. However, my fixed point type is still a little different, and here are excerpts from the key parts of the code:

[Serializable] public struct Fix64 {public long RawValue {get;} / /... Public static explicit operator float (Fix64 value) {/ /...}}

I can say that I have tried my best to get the target value of the Fix64 type directly from the SerializedProperty type, so I think about it, since it is an editor script, try to directly reflect it.

Just do it. Take a look at the API manual first. How can we get a reference to the target value? Generally speaking, this kind of reference is object, so first check that there are no members of type object in SerializedProperty, confirm the look, no.

Think about it. What is SerializedProperty? He wants to display an attribute on Inspector, unlike MonoBehaviour,ScriptableObject and other types, he is a non-UnityEngine.Object type like Vector2, so we get an instance of his class, and then use reflection to find this attribute, and then get his value?

So we first got the UnityEngine.Object type we actually displayed on Inspector through property.serializedObject.targetObject, and then with the directory provided by property.propertyPath, we easily got an instance of his parent type through reflection. For testing, I wrote a MonoBehaviour script here:

Public class EditorGUITest: MonoBehaviour {public FixedDt typeafae21; public float xf = 1616;} [Serializable] public class FixedDt {public Fix64 afafafafa; public Fix64 f2wfsfaeaf;}

Finally we get an instance of typeafae21, a member of EditorGUITest, with the type FixedDt.

Finally, we use the property.name attribute to reflect to get the desired value.

The idea is very simple with the code, which goes directly to the complete code:

[CustomPropertyDrawer (typeof (Fix64))] public class Fix64Drawer: PropertyDrawer {/ / Draw the property inside the given rect public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {using (new EditorGUI.PropertyScope (position, label, property)) {var parent = GetParentObjectOfProperty (property.propertyPath, property.serializedObject.targetObject); var type = parent.GetType (); var pi = type.GetField (property.name); var o = pi.GetValue (parent) Var value = (Fix64) o; value = EditorGUI.FloatField (position, label, (float) value); pi.SetValue (parent,value);}} private object GetParentObjectOfProperty (string path, object obj) {while (true) {var fields = path.Split ('.'); if (fields.Length = = 1) {return obj } var fi = obj.GetType () .GetField (fields [0], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); obj = fi.GetValue (obj); path = string.Join (".", fields, 1, fields.Length-1) } the above is how to use reflection to display custom types on the Inspector of Unity. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report