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
1
Question by EvanTreborn · May 12, 2015 at 11:53 AM · arrayinspectorpropertydrawerfieldinfo

PropertyDrawer.fieldInfo returns array instead of eleme

Hi, I have written a PropertyDrawer for an custom class. For this class I have several Defaults (like Vector3 has up, right,..) The PropertyDrawer shows a Popuplist of all those defaults.

 object targetObj = fieldInfo.GetValue(prop.serializedObject.targetObject);
 MyClass target = targetObj as MyClass;
     
 //find index in defaults list
 //show popup
 //if changed -> apply
 
 target = MyClass.GetDefault(selectedIndex);

This works like a charm if there is a single MyClass Object. When there is an array of MyClass the targetObj becomes an array too.

 [SerializeField] MyClass test = new MyClass(); //works
 [SerializeField] MyClass[] test2; //Elements are not initialized when size is set in inspector
 [SerializeField] MyClass[] test3 = {  //only access to whole array
     new MyClass(),
     new MyClass(),
     new MyClass()
 };

So why is that? Is there a way to determine the actual Element's index? (targetObj can be cast to MyClass[] ) Or is there a simpler way I don't know of?

Comment
Add comment · Show 1
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 EvanTreborn · May 14, 2015 at 01:25 PM 0
Share

I have done some further tests. Could fetch the actual array index by dismantling the propertypath like this:

 object targetObj = fieldInfo.GetValue(prop.serializedObject.targetObject);
 $$anonymous$$yClass target = null;
 int targetArrayIndex = -1;
 $$anonymous$$yClass[] targetArray = null;
 if (targetObj.GetType().ToString() == "$$anonymous$$yClass[]") {
   targetArrayIndex = System.Convert.ToInt32(prop.propertyPath.Substring(prop.propertyPath.IndexOf("[")+1).Replace("]",""));
   targetArray = targetObj as $$anonymous$$yClass[];
   if (targetArrayIndex < targetArray.Length) {
     target = targetArray[targetArrayIndex];
   } else return; //array size changed, elements will be initialized by inspector
 }
 //find selectedIndex in Defaults ..

 //show value popup and apply changes
 int newSelectedIndex = EditorGUI.Popup(pos, selectedIndex, $$anonymous$$yClass.DefaultsStr);
 if (newSelectedIndex != selectedIndex) {
   if (targetArrayIndex == -1)
     target = $$anonymous$$yClass.GetDefault($$anonymous$$yClass.DefaultsStr[newSelectedIndex]);
   else
     targetArray[targetArrayIndex] = $$anonymous$$yClass.GetDefault($$anonymous$$yClass.DefaultsStr[newSelectedIndex ]);
   prop.serializedObject.Apply$$anonymous$$odifiedProperties();
 }


This does show existing values and applies change correctly.

Unfortunately all elements will be replaced with the last element when the array's size is altered in inspector.

So why is there no target property like editor has for propertydrawer?

2 Replies

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

Answer by EvanTreborn · May 30, 2015 at 02:34 PM

I found a workaround for it.

I added an ID attribute to MyClass and wrote a serializeable wraperclass which holds the private object and the [SerializeField] int ID. The wraperclass has a getter which checks if the object is set and the object.ID is equal to the stored ID if not it will fetch the object from the defaults array by the stored ID.

I wrote a property drawer for the warperclass which only sets the ID using a popuplist of all the defined defaults. I can even add new defaults at runtime to the static defaults array. This has to be done before any other object tries to read a MyClassWrapper object though.

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

Answer by llamagod · Sep 07, 2016 at 12:48 PM

The reason why the array fieldInfo is provided is because array elements aren't fields so they have no FieldInfo. You can set the elements by getting the value of the array and then setting the element as you have done. I would probably have gotten the index using Regex instead, but whatever floats your boat.

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

EditorGUI.PrefixLabel returns a rect that doesn't line up with other content when a custom property drawer is used in an array. 2 Answers

Renaming Inspector 2D Array Element 0 , Element 1 1 Answer

Field in PropertyDrawer isn't remembered 1 Answer

Drawing Order of DecoratorDrawer doesn't work? 1 Answer

EditorGUI.IntField always 0 after GameStart 0 Answers


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