LoginSignup
0
1

More than 5 years have passed since last update.

FindPropertyRelativeで取得出来ない

Posted at

FindPropertyRelativeで取得出来ない

SerializedPropertyから子のメンバを取得しようとして出来なかったので迂回方法を模索した。
スマートではないのでもっと良い方法あったら知りたい。

参考
https://answers.unity.com/questions/543010/odd-behavior-of-findpropertyrelative.html

実際のコード

B.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class B : MonoBehaviour {
    public string name;
}
A.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class A : MonoBehaviour {
    public B b;
}
AEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor (typeof(A))]
public class AEditor : Editor {
    private A a;
    private SerializedProperty serializedPropertyB;
    void OnEnable()
    {
        this.a = target as A;
        this.serializedPropertyB = this.serializedObject.FindProperty("b");
    }

    public override void OnInspectorGUI()
    {
        this.serializedObject.Update();

        // 取得できない.なんでなん?
        //var propertyBName = this.propertyB.FindPropertyRelative("name");

        EditorGUILayout.PropertyField(this.serializedPropertyB);
        // Bがある場合のみBのメンバ変数を表示する.
        if(null != this.serializedPropertyB.objectReferenceValue)
        {
            var serializedObjectB = new SerializedObject(this.serializedPropertyB.objectReferenceValue);
            // Bのメンバを更新する為ちゃんと実行しておきます.
            serializedObjectB.Update();

            var SerializedPropertyName = serializedObjectB.FindProperty("name");            
            EditorGUILayout.PropertyField(SerializedPropertyName);

            // Bのメンバを更新する為ちゃんと実行しておきます.
            serializedObjectB.ApplyModifiedProperties();
        }
        this.serializedObject.ApplyModifiedProperties();
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1