做UI的时候,序列化的属性,代码写好后,每次都需要手动拖动相应的GameObject到Inspector上。通过下面的方法,我们可以实现自动赋值,省去手动拖动的体力劳动。
效果如下,序列化的属性的 变量名 跟 子物体的名称保持一致,比如下图中的 _weapon 和 _bag .
![f2e36b10964c9b9c3ca6ea2de224a147.gif](https://img-blog.csdnimg.cn/img_convert/f2e36b10964c9b9c3ca6ea2de224a147.gif)
实现如下:
创建脚本 AutohookAttribute.cs
// NOTE DONT put in an editor folder!
using UnityEngine;
public class AutohookAttribute : PropertyAttribute
{
}
创建 AutohookPropertyDrawer.cs 放到Editor目录下
// NOTE put in a Editor folder
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(AutohookAttribute))]
public class AutohookPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUICont