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 11:44 AM · arraypropertydrawerserializedpropertypointer

SerializedProperty isn't being detected as an array?

I'm writing a property drawer for lists and arrays.

AwesomeCollectionAttribute.cs

 [AttributeUsage(AttributeTargets.Field)]
 public class AwesomeCollectionAttribute : PropertyAttribute { }

TestMB.cs

 public class TestMB : MonoBehaviour
 {
     [AwesomeCollection]
     public Transform[] waypoints;
 }


I have a few problems:

1- In the drawer's OnGUI it's not detecting the property as an array!

AwesomeCollectionDrawer.cs

 [CustomPropertyDrawer(typeof(AwesomeCollectionAttribute))]
 public class AwesomeCollectionDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         Debug.Log("OnGUI ran from awesome");
         if (property.isArray)
             Debug.Log("Size: " + property.arraySize);

         GUI.Label(position, property.name, EditorStyles.boldLabel);
     }
 }

The log isn't getting executed. And if I try to get the size immediately without checking if the property is an array, I get:

 Retrieving array size but no array was provided
 UnityEditor.SerializedProperty:get_arraySize()
 AwesomeCollectionDrawer:OnGUI(Rect, SerializedProperty, GUIContent) (at Assets/Editor/AwesomeCollectionDrawer.cs:13)
 UnityEditor.DockArea:OnGUI()

Here's an interesting thing though:

alt text

There's a few strange things here:

  1. The name is "data" where it should be "waypoints", right?

  2. The property path, is the path of the first element of the array, wtf?

  3. The isArray and arraySize couldn't be evaluated

  4. The objectReferenceTypeString is Transform, shouldn't it be Transform[]?

Here's what I think: All of these things, suggest that I don't have a hold of the actual array, but what I have is its first element? ... or, maybe its base pointer? (because, as we know from C++ the array name is nothing but a const pointer to its first element... but even in that case I could index it) What's going on here?!

2- The other problem, is that although I'm overriding the OnGUI for my property, I can still see some default stuff (the size, and the fold) I don't want any default thing to show up - you can also see this "data" thing - another proof that I don't have a hold of the actual array, but its individual elements:

alt text

Any idea what's going on?

  1. Why isn't the property picking up the array?

  2. Why am I getting some default stuff drawn for me? even though I overrode the OnGUI, and never did call base.OnGUI!

Thanks a lot.

inspector.png (10.2 kB)
watch.png (42.6 kB)
Comment
Add comment · Show 2
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 whydoidoit · Dec 27, 2013 at 12:58 PM 1
Share

Ooo I remember having a problem like this - I have a library of stuff that can find the parents of elements because of it I think. I don't have that code to hand, but I seem to remember getting "data" - which as you quite rightly say isn't the "Property Array" it's the sub element that the serialization system uses to hold the data.

The principle of the library stuff I wrote was to walk the property path towards the property you have from the serialized object root and then use the thing 2 levels above,

avatar image vexe · Dec 27, 2013 at 02:14 PM 0
Share

Thanks for mentioning that, I took the serializedObject from the property, and searched for the real name, and it seems I got it (it makes no sense to me):

 var realProp = property.serializedObject.FindProperty("waypoints"); // could this be it?
 bool isArray = realProp.isArray; // true

But unfortunately this eli$$anonymous$$ates the whole purpose of my property drawer idea - what's the point if I can't get the name right at runtime? - One thing I could do is, pass the name via the attribute:

  [AwesomeCollection("waypoints")]
 public Transform[] waypoints;

But that's just silly... what happens if I refactor and rename? I'd rather not think about it...

If you happen to remember how you got to the array, I'd be interested.

Thanks :)

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Jamora · Dec 27, 2013 at 03:01 PM

You can get the runtime name of the variable with the following code:

 string[] variableName = property.propertyPath.Split('.');
 SerializedProperty p = property.serializedObject.FindProperty(variableName[0]);
 Debug.Log(p.isArray); //Prints true

   

alternatively you could consider only geting a substring from index 0 to the first dot from the propertyPath, but that's a matter of taste.

You can also use reflection:

 fieldInfo.Name

will return the variable name.

Comment
Add comment · Show 2 · 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 27, 2013 at 03:04 PM 1
Share

alt text

1475811_10152100567853028_1250893876_n.jpg (6.7 kB)
avatar image yetyman · Apr 28, 2019 at 05:56 PM 0
Share

So the targetObject of the Serialized property given to us in the OnGui Event is the whole Behaviour class? not the individual item? and the propertyPath is the individual item? I don't think that makes sense, but does that mean we are intended to be able to easily affect a property drawer with other content from the same behaviour?

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

21 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 avatar image

Related Questions

Getting PropertyAttribute from PropertyDrawer.OnGUI 0 Answers

Display Custom Inspectors for each class in a List<> 1 Answer

Custom Editor/Custom Property Drawer combo, breaks Enable button 1 Answer

How do PropertyAttribute instances work in an array of structs? 1 Answer

Get SerializedProperty from outside class for use in popup (PropertyDrawer) 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