Unity/Editor Inspector

상속받은 모노에 에디트 둘다 사용하기.

소나무꼴 2024. 9. 6. 12:02

상속받은 클래스 Hero의 인스펙트창.

아래와 같이 해도 버튼이 2개가 나오지 않는다.

CustomEditor(typeof(CharBase))]
public class CharBaseEditor : Editor
{
	public override void OnInspectorGUI()
	{
		base.OnInspectorGUI();
		
		CharBase heroObj = (CharBase)target;
		if (heroObj.Test)
		{
			GUILayout.Button("CHAR");
		}
	}
}

[CustomEditor(typeof(HeroObj))]
public class HeroObjEditor : Editor
{
	public override void OnInspectorGUI()
	{
		base.OnInspectorGUI();
		
		HeroObj heroObj = (HeroObj)target;
		if (heroObj.Test1)
		{
			GUILayout.Button("HERO");
		}
	}
}

class CharBase : MonoBehaviour
{
	public int Test;
}

class Hero : CharBase
{
	public int Test1;
}

 

해결방안

[CustomEditor(typeof(HeroObj))]
public class HeroObjEditor : Editor
{
    Editor charBaseEditor;

    public override void OnInspectorGUI()
    {
        // CharBase의 기본 Inspector UI 출력
        if (charBaseEditor == null)
        {
            charBaseEditor = CreateEditor((CharBase)target, typeof(CharBaseEditor));
        }

        if (charBaseEditor != null)
        {
            charBaseEditor.OnInspectorGUI();  // CharBaseEditor의 OnInspectorGUI 호출
        }

        // HeroObj의 추가적인 Inspector UI 출력
        HeroObj heroObj = (HeroObj)target;
        if (heroObj.Test1)
        {
            GUILayout.Button("HERO");
        }
    }
}