- Home /
Unity won't let me override the default way of drawing an array/list?
I'm trying to make a custom property drawer for arrays/lists to be able to draw them like this editor window:
[AttributeUsage(AttributeTargets.Field)]
public class AwesomeCollectionAttribute : PropertyAttribute
{
}
public class TestMB : MonoBehaviour
{
[AwesomeCollection][SerializeField]
private List<Transform> waypoints; // also tried Transform[] - same.
}
[CustomPropertyDrawer(typeof(AwesomeCollectionAttribute))]
public class AwesomeCollectionDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
}
}
No matter what, Unity will always draw the default array "things" (the fold, the size and the elements) - I don't want that.
If I tag the field with [HideInInspector]
then this will hide any GUI-related thing I do in the OnGUI
(GUI.Label, etc)
Again, what I'm trying to do is, see that editor window at the top? I'm trying to write a property drawer that would draw arrays/lists just like that window.
Any ideas how to override that default drawing and get what I want?
Thanks.
Answer by Statement · Dec 27, 2013 at 07:30 PM
I can't think of any way to make it work apart from making a new serializable class that wraps your items.
[Serializable]
public class Waypoints
{
public List<Transform> transforms;
/* Rest of impl. */
}
And then you can either add an attribute for where it is used:
public class Example : MonoBehaviour
{
[CompactedWaypoints]
public Waypoints waypoints;
}
Or you could possibly just make a specific drawer for that type if you don't have specific ways to draw a specific field:
public class Example : MonoBehaviour
{
public Waypoints waypoints;
}
[CustomPropertyDrawer (typeof(Waypoints))]
public class DefaultWaypointsDrawer : PropertyDrawer
{
/* Rest of impl. */
}
If you want to use it as if it was a list, consider implementing the IList interface.
Thanks Statement. Yes, wrapping the list in a class, and making a drawer for that class, is the only way I also thought this would be possible. But the thing is, I don't want a concrete list of a certain type (`List `) as that would break the point of the whole thing, which is to be able to draw List < T >
So... here's what I tried:
public abstract class SuperAwesomeList : Component { }
public class AwesomeList<T> : SuperAwesomeList
{
[SerializeField]
private List<T> internalList;
// wrappers for pretty much everything the List does...
}
and then just like you mentioned, a property drawer for the base type - and its children (which should include AwesomeList < T >
):
[CustomPropertyDrawer(typeof(SuperAwesomeList), true)]
public class AwesomeListDrawer : PropertyDrawer
{
override void OnGUI(...)
{
Debug.Log("OnGUI ran from awesome: " + property.name);
}
}
But... as with any attempt to make something cool, it just fails.
public class Test$$anonymous$$B : $$anonymous$$onoBehaviour
{
public AwesomeList<Transform> awesomeList;
}
I attach Test$$anonymous$$B to some GO, and the OnGUI of the list's drawer doesn't get called...
If I make something like this however:
[System.Serializable]
public class CoolList : SuperAwesomeList { }
And have a CoolList reference in my Test$$anonymous$$B, the OnGUI of the drawer gets called...
It seems like there's no support for generics?
Of course, the same thing happens when I try to make the drawer for an AwesomeCollectionAttribute
, and then tag my AwesomeList < T >
with that attribute - Same exact behavior. OnGUI
never gets called.
It seems that it just simply won't budge. Unity insists on not giving me the custom behavior I want, in an elegant way. The more time I'm spending on them custom editors/drawers, the more I see how the Unity editor design, is non-polymorphic and actually, stupid in some regards.
Answer by John-LZG · Feb 19, 2014 at 12:21 AM
This tutorial may help. It can draw a list held by a monobehaviour. but it only works in customInspectors, not CustomDrawers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124951im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to create bitmask flags per scene that are available in the editor? 0 Answers
EditorGUILayout.Foldout not working properly - results in argument exceptions 1 Answer
PropertyDrawer always errors when calling OnInspectorGUI 1 Answer
Why is my propertydrawer being automatically disabled? 1 Answer
Custom editor script for folders? 1 Answer