# CustomPropertyDrawer on Scriptable Object

**URL:** <https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536>\
**Category:** C# & .NET\
**Tags:** Scripting\
**Created:** [March 26, 2017, 3:56pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536 "2017-03-26T15:56:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoughSpaghetti3211](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/roughspaghetti3211/32/165044_2.png) [@RoughSpaghetti3211](https://discussions.unity.com/u/RoughSpaghetti3211)\
**Post date:** [March 26, 2017, 3:56pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/1 "2017-03-26T15:56:43Z")

</div>

Hello,

Im testing to see if I can get a CustomPropertyDrawer to work on a Scriptable Object. I can get it working on everything but a Scriptable Object, the 2 image below show one on SC and one on a scene GO. Do I need to do something special for SOs. Please help

```csharp
using UnityEngine;
using System.Collections;

[System.Serializable]
public class GridData {

    [System.Serializable]
    public struct rowData{
        public int[] colum;
    }
    public static int numRows =3;
    public static int numColums =15;
    public rowData[] rows = new rowData[numRows];
}

```

```csharp
using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomPropertyDrawer(typeof(GridData))]
public class GridDataPropertyDrawer : PropertyDrawer {

    public override void OnGUI(Rect position,SerializedProperty property,GUIContent label){
        EditorGUI.PrefixLabel(position,label);
        Rect newposition = position;
        newposition.y += 18f;
        SerializedProperty data = property.FindPropertyRelative("rows");
        //data.rows[0][]
        for(int j=0;j< GridData.numRows;j++){
            SerializedProperty row = data.GetArrayElementAtIndex(j).FindPropertyRelative("colum");
            newposition.height = 18f;
            if(row.arraySize != GridData.numColums)
                row.arraySize = GridData.numColums;
            newposition.width = position.width/ GridData.numColums;
            for(int i=0;i< GridData.numColums;i++){
                EditorGUI.PropertyField(newposition,row.GetArrayElementAtIndex(i),GUIContent.none);
                newposition.x += newposition.width;
            }

            newposition.x = position.x;
            newposition.y += 18f;
        }
    }

    public override float GetPropertyHeight(SerializedProperty property,GUIContent label){
        return 18f * GridData.numRows + 18f;
    }
}

```

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CreateAssetMenu(menuName="ScriptableObject Assets/Packages/Biodome Package")]
public class BiodomePackageData : ScriptableObject {

    public float maxLengthM =50;
    [SerializeField]private BioTileData[] tiles;
    public BioTileData[] Tiles{get {return tiles;}}

    //
    // TEST
    public GridData gridData;
    //public GridData GridData{ get { return gridData; } }
    //[SerializeField]private BioPropsData[] props;
    //public BioPropsData[] Props{get {return props;}}

}

```

 ![3007876--224357--Screen Shot 2017-03-26 at 9.00.07 AM.png](https://europe1.discourse-cdn.com/unity/original/4X/d/9/e/d9e22d107f41a12af9741cdf43107dfd09d15803.png)  
 ![3007876--224358--Screen Shot 2017-03-26 at 8.59.38 AM.png](https://europe1.discourse-cdn.com/unity/original/3X/7/f/7f08fb3de35cbd4711b6eb0c97c4258a35494244.png)

---

<div class="post-metadata">

**Author:** ![TonyLi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyli/32/611093_2.png) [@TonyLi](https://discussions.unity.com/u/TonyLi)\
**Post date:** [March 27, 2017, 1:38pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/2 "2017-03-27T13:38:15Z")

</div>

Property drawers should work fine in SOs. In your SO, gridData’s type isn’t actually a subclass of GridData, is it? If so, you may need to change this:

```auto
[CustomPropertyDrawer(typeof(GridData))]

```

to this:

```auto
[CustomPropertyDrawer(typeof(GridData), true)]

```

to allow the custom property drawer to apply to subclasses, too.

BTW, rather than using 18f, you may want to use EditorGuiUtility.singleLineHeight. This helps future-proof your code in case Unity changes the height of its editor font at some point.

---

<div class="post-metadata">

**Author:** ![RoughSpaghetti3211](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/roughspaghetti3211/32/165044_2.png) [@RoughSpaghetti3211](https://discussions.unity.com/u/RoughSpaghetti3211)\
**Post date:** [March 27, 2017, 2:15pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/3 "2017-03-27T14:15:36Z")

</div>

> [@TonyLi](#):
>
> Property drawers should work fine in SOs. In your SO, gridData’s type isn’t actually a subclass of GridData, is it? If so, you may need to change this:
> 
> ```auto
> [CustomPropertyDrawer(typeof(GridData))]
> 
> ```
> 
> to this:
> 
> ```auto
> [CustomPropertyDrawer(typeof(GridData), true)]
> 
> ```
> 
> to allow the custom property drawer to apply to subclasses, too.
> 
> BTW, rather than using 18f, you may want to use EditorGuiUtility.singleLineHeight. This helps future-proof your code in case Unity changes the height of its editor font at some point.

Still no luck , the code above is everything no other inheritance. Also strange that EditorGuiUtility.singleLineHeight is not found, is this from using UnityEditor ?.

Maybe Im not understanding something. So when I create my SO from the menu and select it , I should see the  
CustomPropertyDrawer implemented or do I have to implement my SO through momobehaviour to see it.

Thanks for the help appreciate it

---

<div class="post-metadata">

**Author:** ![TonyLi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyli/32/611093_2.png) [@TonyLi](https://discussions.unity.com/u/TonyLi)\
**Post date:** [March 27, 2017, 3:50pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/4 "2017-03-27T15:50:22Z")

</div>

Sorry, typo. It’s [EditorGUIUtility.singleLineHeight](https://docs.unity3d.com/ScriptReference/EditorGUIUtility-singleLineHeight.html).

When you create your SO, it should automatically use the custom property drawer. Sorry, I don’t know why it’s not. Maybe try a simpler example with just one field, and pare it down until it works?

---

<div class="post-metadata">

**Author:** ![RoughSpaghetti3211](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/roughspaghetti3211/32/165044_2.png) [@RoughSpaghetti3211](https://discussions.unity.com/u/RoughSpaghetti3211)\
**Post date:** [March 28, 2017, 4:01am UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/5 "2017-03-28T04:01:05Z")

</div>

Ok tried a simple example, same thing lol (going crazy here) . Work on scene GO not on SO. What am I missing here?

BTW thanks for the [EditorGUIUtility.singleLineHeight](https://docs.unity3d.com/ScriptReference/EditorGUIUtility-singleLineHeight.html) tip , works well

---

<div class="post-metadata">

**Author:** ![TonyLi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyli/32/611093_2.png) [@TonyLi](https://discussions.unity.com/u/TonyLi)\
**Post date:** [March 28, 2017, 12:39pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/6 "2017-03-28T12:39:59Z")

</div>

Could it be a bug in the specific version of Unity you’re using?

I’m attaching an example I just tested in Unity 5.3.4:

**MyData.cs**

```auto
using System;

[Serializable]
public class MyData
{
    public string dataValue;
}

```

**MyDataPropertyDrawer.cs**

```auto
using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(MyData), true)]
public class MyDataPropertyDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.PropertyField(position, property.FindPropertyRelative("dataValue"), new GUIContent("DATA (Custom Drawer)"), true);
    }
}

```

**MyMB.cs**

```auto
using UnityEngine;

public class MyMB : MonoBehaviour
{
    public MyData myData;
}

```

**MySO.cs**

```auto
using UnityEngine;

[CreateAssetMenu]
public class MySO : ScriptableObject
{
    public MyData myData;
}

```

[3009958–224558–CustomPropertyDrawerOnScriptableObject\_2017-03-28.unitypackage](https://discussions.unity.com/uploads/short-url/6PkKibcUx94f29WXXztEuhUued3.unitypacka) (2.94 KB)

---

<div class="post-metadata">

**Author:** ![RoughSpaghetti3211](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/roughspaghetti3211/32/165044_2.png) [@RoughSpaghetti3211](https://discussions.unity.com/u/RoughSpaghetti3211)\
**Post date:** [March 29, 2017, 1:49pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/7 "2017-03-29T13:49:15Z")

</div>

Thanks so much for this, ok I tested it and im not sure what to make of it.  
When I recreated your script it behaved the same way (works on GO not on SC)

When I imported your test into my project it works. I eliminate folder structure by moving your equivalent script to match mine. This didn’t change anything.

I removed all the code from MySO and added in public GridData gridData; and it works ☹

OK so then I replace the name and all the code with BiodomePackageData and it worked.

I remove the test you gave me leaving my new but the same BiodomePackageData and it worked

So I dont know why this is working but essentially all I have now is your imported mySO script renamed to BiodomePackageData with a copy past from the existing BiodomePackageData SO and removing my old BiodomePackageData SO … and it works … lol

What do you think is happening ?

---

<div class="post-metadata">

**Author:** ![TonyLi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tonyli/32/611093_2.png) [@TonyLi](https://discussions.unity.com/u/TonyLi)\
**Post date:** [March 29, 2017, 2:09pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/8 "2017-03-29T14:09:48Z")

</div>

Sorry, I haven’t a clue! That’s really weird.

---

<div class="post-metadata">

**Author:** ![RoughSpaghetti3211](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/roughspaghetti3211/32/165044_2.png) [@RoughSpaghetti3211](https://discussions.unity.com/u/RoughSpaghetti3211)\
**Post date:** [March 29, 2017, 2:31pm UTC](https://discussions.unity.com/t/custompropertydrawer-on-scriptable-object/659536/9 "2017-03-29T14:31:24Z")

</div>

thanks for the help, I’ll keep copy pasting the SO you provided for now.
