Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by vexe · Dec 27, 2013 at 05:04 PM · editor-scriptingonguipropertydrawercustom-editor

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:

alt text

 [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.

alt text

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.

default.png (8.2 kB)
editor.png (22.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Dec 28, 2013 at 04:13 AM 0
Share

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?

avatar image vexe · Dec 28, 2013 at 04:19 AM 0
Share

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.

avatar image vexe · Dec 28, 2013 at 06:07 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges