うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】CustomPropertyDrawer を利用している Type 一覧の取得

CustomPropertyDrawer を利用しているクラスは描画方法を変えたかったことがあったが
自分で取得する必要があったので、そのとき利用した方法を記述する。

CustomPropertyDrawer

github.com CustomPropertyDrawer のクラスは上記のようになっており、"m_Type"を取得できれば利用している型が分かる

が internal なので Reflection を用いて取得する

   private List<Type> cacheCustomDrawerTypes;
    private void OnEnable()
    {
        cacheCustomDrawerTypes = new List<Type>();
        foreach (var type in TypeCache.GetTypesDerivedFrom<GUIDrawer>())
        {
            foreach (CustomPropertyDrawer customAttribute in type.GetCustomAttributes(typeof(CustomPropertyDrawer), true))
            {
                var field = customAttribute.GetType().GetField("m_Type", BindingFlags.NonPublic | BindingFlags.Instance);
                var t = (Type) field.GetValue(customAttribute);
                cacheCustomDrawerTypes.Add(t);
            }
        }
    }
 
    private bool HasCustomDrawerType(Type type)
    {
        return cacheCustomDrawerTypes.Contains(type);
    }