万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

  • ・資産運用おすすめ
    10万円は1000円くらい利益
    資産運用ブログ アセマネ
    • ・寄付お願いします
      YENTEN:YYzNPzdsZWqr5THWAdMrKDj7GT8ietDc2W
      BitZenny:ZfpUbVya8MWQkjjGJMjA7P9pPkqaLnwPWH
      c0ban:8KG95GXdEquNpPW8xJAJf7nn5kbimQ5wj1
      Skycoin:KMqcn7x8REwwzMHPi9fV9fbNwdofYAWKRo

    C# Unity EditorのInspector拡張で自前のクラスの変換

    Hogeクラスは何の変哲もないMonoBehaviorの継承でもないふつーのクラス。 シリアライズのため[System.Serializable]

    [System.Serializable]
    class Hoge
    {
        string bar;
    }

    そしてInsppectorを表示していたクラス

    public class MyMono : MonoBehaviour
    {
        [SerializeField]
        Hoge[] hoges;
    }

    と定義したとすると、 カスタムエディタ。

     [CustomEditor(typeof(MyMono))]
        public class MyMonoEditor : Editor
        {
            public override void OnInspectorGUI()
            {
                    serializedObject.Update();
                    SerializedProperty hogesSP = serializedObject.FindProperty("hoges");
                var hoges = hogesSP.objectReferenceValue as Hoge[];
            }
        }

    以下のエラー。

    error CS0039: Cannot convert type `UnityEngine.Object' to `Hoge[]' via a built-in conversion

    ワンクッション置くぜ!

    var hoges = hogesSP.objectReferenceValue as System.Object as Hoge[];

    実行はできる・・。 だが・・・ランタイム時にエラー。

    type is not a supported pptr value
    UnityEditor.DockArea:OnGUI()

    これはList型だがこれを自分に合わせて使う。

    http://answers.unity3d.com/questions/682932/using-generic-list-with-serializedproperty-inspect.html

    無理?