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();
}
}