- Home /
How to get PropertyDrawers in a custom EditorWindow
I'm trying to get PropertyDrawer support in a custom EditorWindow. So, first I make a serializable class
using UnityEngine;
[System.Serializable]
public class FooBar
{
[TextArea(5, 5)]
public string someText = "Hello";
[Range(1.0f, 2.0f)]
public float someRange = 0.25f;
public int someInt;
}
It's got PropertyDrawer attributes on someText
and someRange
. If add that to a GameObject I see it works in the inspector
So then I make a custom editor and draw an instance of the same class using EditorGUILayout.PropertyField
using System;
using UnityEditor;
using UnityEngine;
public class FooWindow : EditorWindow {
public FooBarHolder foobarHolder;
private SerializedObject m_serializedObject;
private SerializedProperty m_serializedProperty ;
private Vector2 m_scrollPos = new Vector2(0, 0);
void Init()
{
if (foobarHolder == null) {
var tmp = Resources.FindObjectsOfTypeAll<FooBarHolder>();
if (tmp.Length > 0)
{
foobarHolder = tmp[0];
}
else
{
foobarHolder = ScriptableObject.CreateInstance<FooBarHolder>();
}
}
if (m_serializedObject == null) {
m_serializedObject = new UnityEditor.SerializedObject(foobarHolder);
m_serializedProperty = m_serializedObject.FindProperty("foobar");
}
}
void OnGUI()
{
// This is here because as we edit this source, when unity recompiles, code below
// will fail because everything will have been seralized, purged, an restored.
Init();
m_serializedObject.Update();
EditorGUILayout.BeginVertical();
m_scrollPos = EditorGUILayout.BeginScrollView(m_scrollPos, GUILayout.Width(EditorGUIUtility.currentViewWidth), GUILayout.Height (position.height));
EditorGUILayout.PropertyField(m_serializedProperty , true);
m_serializedObject.ApplyModifiedProperties();
EditorGUILayout.EndVertical();
EditorGUILayout.EndScrollView();
}
[MenuItem ("Window/FooWindow")]
static void ShowIt()
{
FooWindow w = EditorWindow.GetWindow<FooWindow>();
w.Init();
}
}
// The only reason this exists is becaue I don't know how to call m_serilziedObject.FindProperty
// without a property name. In other words, when I want a SeralizedProperty for the object itself,
// not one of its children. But putting FooBar in a FooBarHolder I can call FindProperty and
// pass in "foobar"
public class FooBarHolder : ScriptableObject{
public FooBar foobar;
void OnEnable() {
hideFlags = HideFlags.DontSave;
if (foobar == null) {
foobar = new FooBar();
}
}
}
All the fields are shown but none of the attributes for PropertyDrawers are applied.
Any idea how to get PropertyDrawers to work in a custom EditorWindow?
Apply$$anonymous$$odifiedProperties(); http://docs.unity3d.com/ScriptReference/SerializedObject.Apply$$anonymous$$odifiedProperties.html
I dont' see how that's supposed to help. First off I'm calling it at line 40 above. Second off the docs have nothing to do with propertydrawers getting used. Third, they aren't getting used even though I'm calling SerializedObject.Apply$$anonymous$$odifiedProperties
Your answer
![](https://koobas.hobune.stream/wayback/20220611234550im_/https://answers.unity.com/themes/thub/images/avi.jpg)