Unity・3DCG技術ブログ

TAもどきによるUnity・3DCGに関する記事をアップします。

C#リフレクションでComponentのSerializeFieldをゲットする

// SerializeFieldをゲットする。componentはなんらかのComponentを指定する。

var fields = component.GetType()
                .GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
                .SelectMany(x => x.CustomAttributes.Where(t => t.AttributeType == typeof(SerializeField)).Select(_ => x));